element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [Pi IoT] Alarm Clock #07: Sense HAT
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: fvan
  • Date Created: 28 Jun 2016 4:28 PM Date Created
  • Views 2601 views
  • Likes 4 likes
  • Comments 17 comments
  • fvan_piiot
  • sense hat
  • piiot
  • feature_tutorial
Related
Recommended

[Pi IoT] Alarm Clock #07: Sense HAT

fvan
fvan
28 Jun 2016

  • Sense HAT
    • Sensor Data
      • Temperature, Humidity & Pressure
      • Accelerometer, Gyroscope & Magnetometer
    • Joystick
    • LED Matrix
      • Text
      • Shape
  • Issue ?

 

As explained in my unboxing post, I wasn't originally planning to use the Sense HAT in my project. Though the more I read about it, the more I wanted to experiment with it anyway. And since all my other electronics gear is in boxes, waiting for next week's house move, now's the perfect time to see what this HAT has to offer!image

 

Sense HAT

 

The Sense HAT, is as the name implies, loaded with sensors. In this paragraph I'll cover which sensors are available and how to fetch their data using the Python reference API. The HAT also has display capabilities using LEDs and input using a joystick.

 

The Python module for the Sense HAT seemed to be installed by default in my case. If it isn't, you can install it using following command:

 

pi@raspberrypi:~ $ sudo apt-get install sense-hat

 

Also, make sure I2C is enabled (can be done using "raspi-config").

 

Sensor Data

 

The Sense HAT has a lot of sensors on board. Let's see how to retrieve this data

 

Temperature, Humidity & Pressure

 

By default, the temperature is retrieved from the humidity sensor. It is however possible to retrieve it from the pressure sensor instead.

 

The example below illustrates how to retrieve the data from the humidity and pressor sensor:

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sense_hat import SenseHat
from time import sleep

sh = SenseHat()

try:
    while True:
        th = sh.get_temperature()
        tp = sh.get_temperature_from_pressure()
        p = sh.get_pressure()
        h = sh.get_humidity()

        th = round( th, 1 )
        tp = round( tp, 1 )
        p = round( p, 1 )
        h = round( h, 1 )

        print( "Temp (H) = %s°C    Temp (P) = %s°C    Prsr = %smb   Hmdt = %s%%" %(th,tp,p,h) )
        sleep( 1 )

except KeyboardInterrupt:
    print( "Exiting..." );

 

And the output should be similar to this:

image

 

Note that my HAT seems to be suffering from an issue, resulting in low humidity and even negative temperatures (it's really not that cold in Belgium ...). More about this in the last paragraph.

 

Accelerometer, Gyroscope & Magnetometer

 

Getting the orientation for all 3 axis can be done with a single call: "get_orientation". The data is returned in degrees. Note that no delays should be added in the function, as the sensor relies on multiple measurements to calculate the values. Adding delay will result in incorrect values.

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sense_hat import SenseHat
from time import sleep

sh = SenseHat()

try:
    while True:
        pitch, roll, yaw = sh.get_orientation().values()

        pitch = round( pitch, 1 )
        roll = round( roll, 1 )
        yaw = round( yaw, 1 )

        print( "Pitch = %s°    Roll = %s°    Yaw = %s°" %(pitch, roll, yaw) )

except KeyboardInterrupt:
    print( "Exiting..." );

 

The output:

image

 

The magnetometer can be used for compass applications:

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sense_hat import SenseHat
from time import sleep

sh = SenseHat()

try:
    while True:
        north = sh.get_compass()
        north = round( north, 1 )

        print( "North = %s°" %(north) )

except KeyboardInterrupt:
    print( "Exiting..." );

 

Unfortunately, those results seemed unreliable (or I was testing it incorrectly), as when turning the board around it's Z axis, the north would remain between 120° and 150° while I would expect it to go from 0° to 360°.

image

 

Turns out a calibration file is provided by default, which should cover most cases. Not working as expected, I deleted it, and ran the calibration steps described here.

 

The result was better, as I was able to cover the entire range of values:

 

North = 49.2°
North = 49.2°
...
North = 358.7°
North = 358.8°
...
North = 12.0°
North = 12.1°

 

Finally, the acceleration data can be retrieved as follows:

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sense_hat import SenseHat
from time import sleep

sh = SenseHat()

try:
    while True:
        x, y, z = sh.get_accelerometer_raw().values()

        x = round( x, 0 )
        y = round( y, 0 )
        z = round( z, 0 )

        print( "X = %s    Y = %s    Z = %s" %(x, y, z) )

except KeyboardInterrupt:
    print( "Exiting..." );

 

In below example, I shook the Pi in the Y direction. You can see the values (in Gs) go from positive, to negative and back to positive, representing the shaking motion:

image

 

Joystick

 

To be able to scroll between menus and trigger actions, a 5 button joystick is foreseen.

 

The buttons of the joystick are mapped to keyboard keys: cursors and return key. Using code, it is possible to catch those events and trigger actions accordingly.

 

#!/usr/bin/env python

import pygame

pygame.init()
pygame.display.set_mode((640, 480))

try:
    while True:
        for event in pygame.event.get():
            print(event.key)

except KeyboardInterrupt:
    print( "Exiting..." );

 

Pressing the joystick's buttons results in the following output.

image

 

There are 5 different numbers, repeated twice. The repetition represent the "press" and "release" actions. The actual numbers are key codes. A listing can be found here, the sequence above is then decoded as "left", "up", "right", "down", "enter".

 

LED Matrix

 

The Sense HAT has an onboard 8x8 RGB LED matrix which can be used to display images or text.

 

Text

 

In the code below, I created an example program demonstrating various attributes of the "show_message" function. It is possible to define the colour of the text and background, the scroll speed and the rotation.

 

#!/usr/bin/env python

from sense_hat import SenseHat
from time import sleep

sh = SenseHat()

try:
    sh.set_rotation(0)
    sh.show_message("test")
    sleep( 1 )
    sh.set_rotation(90)
    sh.show_message("test", text_colour=[255, 0, 0])
    sleep( 1 )
    sh.set_rotation(180)
    sh.show_message("test", text_colour=[255, 255, 0], back_colour=[0, 0, 255])
    sleep( 1 )
    sh.set_rotation(270)
    sh.show_message("test", scroll_speed=0.5)
    sleep( 1 )

except KeyboardInterrupt:
    print( "Exiting..." );

 

The code above results in following animation:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Shape

 

Drawing shapes on the LED matrix, is done by defining an array of color values, representing each LED. In below example, the "wink" array defines a winking smiley face. The background is defined by the array "O" as black (no color), the drawing by X as blue.

 

#!/usr/bin/env python

from sense_hat import SenseHat
from time import sleep

sh = SenseHat()

X = [0, 0, 255]
O = [0, 0, 0]

wink = [
O, X, O, O, O, O, O, O,
X, O, X, O, O, X, X, X,
O, X, O, O, O, O, O, O,
O, O, O, O, X, O, O, O,
O, O, O, X, X, O, O, O,
O, O, O, O, O, O, O, O,
O, X, O, O, O, O, X, O,
O, O, X, X, X, X, O, O
]

try:
    sh.set_pixels(wink)

except KeyboardInterrupt:
    print( "Exiting..." );

 

It's possible to define multiple arrays of shapes and colour and combine them, as demonstrated below:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Issue ?

 

There seems to be an issue with some of the Sense HATs used in this challenge.

 

The problem first surfaced when vish created a discussion about his HAT reporting negative temperature and humidity. Using the same code, mine reported correct values. The next day though, I was suddenly suffering from the same negative temperatures. Doing some searching online, I came across this thread on the official Raspberry Pi forums, in which it is described as a hardware issue, which has been resolved in the factory in the mean time. Some faulty units could still be in stock at some distributors though.

 

Could this be the same issue?

image

 

In case the humidity is not important for your application, there is no blocking issue, as the temperature can be retrieved from the pressure sensor instead.

 


image

 


Navigate to the next or previous post using the arrows.

image
  • Sign in to reply

Top Comments

  • element14Dave
    element14Dave over 9 years ago in reply to fvan +5
    I am getting 15 "Tested Good" SenseHats dispatched to my attention. From there I will send out via UPS to all Pi IoT competitors. Dave
  • fvan
    fvan over 9 years ago +4
    New library was released, providing native use of the joystick! [Pi IoT] Sense HAT: New Release Python Library
  • element14Dave
    element14Dave over 9 years ago in reply to gpolder +3
    I received the working parts this afternoon. Once I find some appropriately sized boxed, they will be on their way (along with any other missing parts). Dave
  • element14Dave
    element14Dave over 9 years ago in reply to gpolder

    Hello gpolder,

    The packages that were sent included the various components from each member that was needed because of the part being faulty or missing. I am working to resolve this before the weekend, and report back to the competitors an update before close of business tomorrow. This will be communicated as a discussion post on Pi IoT.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • gpolder
    gpolder over 9 years ago in reply to element14Dave

    Hi element14Dave,

     

    would you be so kind to send me the missing WiPi Dongle and the PiFace digital also together with the SenseHat. (see previous discussions)

    Although the deadline is approaching fast, hopefully I will get the time to include them in my application.

     

    thanks a lot,

    Gerrit.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • element14Dave
    element14Dave over 9 years ago in reply to vish

    Thanks for letting me know that these have not been received. I am working on determining when they will arrive to you and the other competitors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • vish
    vish over 9 years ago in reply to element14Dave

    Hi element14Dave, could you please tell what is happening with the Sense Hat problem. I'm still waiting for mine.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • element14Dave
    element14Dave over 9 years ago in reply to gpolder

    I received the working parts this afternoon. Once I find some appropriately sized boxed, they will be on their way (along with any other missing parts).

     

    Dave

    • Cancel
    • Vote Up +3 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