element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Raspberry Pi Sense Hat - Enabling The Joystick
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 23 Jan 2017 5:00 PM Date Created
  • Views 2943 views
  • Likes 9 likes
  • Comments 8 comments
Related
Recommended
  • sense hat
  • sense_hat
  • raspberry_pi sense_hat
  • raspberry_pi
  • raspberry pi sense hat

Raspberry Pi Sense Hat - Enabling The Joystick

Former Member
Former Member
23 Jan 2017

Having had some fun playing with the display on the Sense Hat I moved on to trying to utilise the Joystick. However copying the examples set out in the Sense Hat API results in the following error

 

AttributeError: 'SenseHat' object has no attribute 'stick'

 

Upon further investigation it turns out that the sense_hat.py library doesn't include any functionality for the joystick, trying to run sudo apt remove --purge sense-hat then sudo apt-get update and sudo apt-get install sense-hat only installs the same version without the joystick functions.

 

Navigating to the github repositories shows that there is an updated version that does include the joystick functions, to get the updates were going to have to manually install them.

 

Time to implement the update!!

 

Step 1.

 

Open up a terminal on the Raspberry Pi (or if your going over ssh just use the terminal your already in).

 

Navigate to the sense_hat python directory

cd /usr/lib/python2.7/dist-packages/sense_hat

 

delete the existing files

sudo rm __init__.py sense_hat.py __init__.pyc sense_hat.pyc

 

Step 2.

 

Download the new files straight from github

 

sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/__init__.py 
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/sense_hat.py 
sudo wget https://raw.githubusercontent.com/RPi-Distro/python-sense-hat/master/sense_hat/stick.py

 

Step 3.

 

We just need to make a slight alteration to the sense_hat.py file that was downloaded, open the file using nano:

sudo nano sense_hat.py

 

 

Line 17 (highlighted in the picture below) reads

 

from .stick import SenseStick

 

the .  needs deleting from in front of the work stick so it reads:

from stick import SenseStick

 

Press ctrl+o then enter to save changes and ctrl+x to exit nano.

 

image

 

Thats it! happy playing!!

 

Here's an example code and video showing how to move a pixel around using the joystick:

 

from sense_hat import SenseHat
sense = SenseHat()

x = 3
y = 3

def draw_dot():
 sense.clear()
 sense.set_pixel(x,y,255,90,90)

def pushed_up():
 global y
 y = y - 1
 if y < 0:
  y = 0
 draw_dot()

def pushed_down():
 global y
 y = y + 1
 if y > 7:
  y = 7
 draw_dot()

def pushed_left():
 global x
 x = x - 1
 if x < 0:
  x = 0
 draw_dot()

def pushed_right():
 global x
 x = x + 1
 if x > 7:
  x = 7
 draw_dot()

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right

draw_dot()

 

 

This video is unavailable.
You don't have permission to edit metadata of this video.

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 8 years ago in reply to Jan Cumps +3
    Music
  • Former Member
    Former Member over 8 years ago in reply to spannerspencer +2
    When I found that wall paper I immediately went looking for emulator roms, by far the best is the arcade version all round. The early computers couldn't compete with the graphics, rich sound and responsiveness…
  • spannerspencer
    spannerspencer over 8 years ago +1
    Excellent work Lucie -- really useful. Thanks for this! And check out that Metro Cross wallpaper!! I LOVE that game -- not played it in soooo long Had it on the Speccy and Amiga!
  • owenevans00
    owenevans00 over 7 years ago in reply to tonygo

    It's a bit late for this thread but I can confirm that the same issue exists in 2018, and the same fix works identically for python 3

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 8 years ago in reply to tonygo

    For other versions of Python you could always download the libraries straight to your scripts directory and the interpreter will look there first. Maybe also theres a dist-packages folder in /usr/lib/python3.0 to wget them to or alternatively start your Python 3 script with:

     

    import sys
    sys.path.insert(0, '/usr/lib/python2.7/dist-packages/sense_hat')
    from sense_hat import SenseHat

     

    Its maybe not the best solution but it adds the python2.7 versions sense_hat directory to your python3 imports search directories. Im not home to test it at the minute but provided all of the other import dependencies used by the sense_hat library are met then it should work just fine.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • tonygo
    tonygo over 8 years ago

    Very useful blog - thanks.

    How do you make it work in Python 3?

    Why has the update not been included in the official update list?

     

    I used pygame to get it to work

    """

    # Modified from Astro Pi Guide for Teachers
    #      raspberrypi.org/resources
    # Tony Goodhew 26 Sept 2015
    import pygame

    from pygame.locals import *
    from sense_hat import SenseHat

    pygame.init()
    pygame.display.set_mode((90,5))#Big enough

    sense = SenseHat()
    sense.clear()

    print("Steer the BLOB with the joystick")
    print("  * Up, Down, Left or Right *\n")
    print("Quit with Joystick Centre Button, RETURN or ESC\n")

    running = True

    x = 0
    y = 0
    sense.set_pixel(x, y, 255, 255, 255)

    while running:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                sense.set_pixel(x, y, 0, 0, 0)  # Black 0,0,0 means OFF

                if event.key == pygame.K_DOWN and y < 7:
                    y = y + 1
                elif event.key == pygame.K_UP and y > 0:
                    y = y - 1
                elif event.key == pygame.K_RIGHT and x < 7:
                    x = x + 1
                elif event.key == pygame.K_LEFT and x > 0:
                    x = x - 1
                elif event.key == pygame.K_RETURN:
                    running = False
                    sense.clear()
                    print("** BYE from Joystick **")
                if running: sense.set_pixel(x, y, 255, 255, 255)
               
            if event.type == QUIT:
                running = False
                sense.clear()
                print(" ++ BYE from mouse QUIT ++")

    """

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • spannerspencer
    spannerspencer over 8 years ago in reply to Former Member

    And such a bizarre game, based around crushing tin cans (or being crushed by them). I think that's what I liked about it.

     

    Just checked out the coin-op version, and you're right. Plus, I like how it starts with "Ready Yourself Player One" image

     

    My favourite book image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 8 years ago in reply to spannerspencer

    When I found that wall paper I immediately went looking for emulator roms, by far the best is the arcade version all round. The early computers couldn't compete with the graphics, rich sound and responsiveness of the input and the later versions like the Amiga seemed like a half hearted attempt hah!! I also love the way the posters were purely artistic and had zero representation of in game footage!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube