0% found this document useful (0 votes)
99 views2 pages

Sense HAT Code Reference Guide

This document provides code examples for using various sensors and functions of the Raspberry Pi Sense HAT including: getting pixel positions, clearing pixels, rotating and flipping the LED matrix, scrolling messages, displaying letters, getting humidity, temperature, pressure, orientation, and compass readings. It includes 18 code examples ranging from 1 to 4 lines each that demonstrate common operations with the Sense HAT such as setting and displaying pixels, loading images, and accessing sensor data.

Uploaded by

Rachel Tao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views2 pages

Sense HAT Code Reference Guide

This document provides code examples for using various sensors and functions of the Raspberry Pi Sense HAT including: getting pixel positions, clearing pixels, rotating and flipping the LED matrix, scrolling messages, displaying letters, getting humidity, temperature, pressure, orientation, and compass readings. It includes 18 code examples ranging from 1 to 4 lines each that demonstrate common operations with the Sense HAT such as setting and displaying pixels, loading images, and accessing sensor data.

Uploaded by

Rachel Tao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3. Get Pixel Position 17.

Get Gyroscope Reading


from sense_hat import SenseHat from sense_hat import SenseHat
sense = SenseHat() sense = SenseHat()

pixel_list = sense.get_pixels() gyro_only = sense.get_gyroscope()

4. Clear all Pixels print("p: {pitch}, r: {roll}, y:


{yaw}".format(**gyro_only))
from sense_hat import SenseHat
from time import sleep
18. Get Acceleration
sense = SenseHat()

red = (255, 0, 0) from sense_hat import SenseHat

[Link]() # no arguments defaults to off sense = SenseHat()


sleep(1)
[Link](red) # passing in an RGB tuple accel_only = sense.get_accelerometer()
sleep(1)
print("p: {pitch}, r: {roll}, y:
[Link](255, 255, 255) # passing in r, g and {yaw}".format(**accel_only))
b values of a colour

[Link] the LEDs


from sense_hat import SenseHat

sense = SenseHat()

sense.set_rotation(180)

6. Flip the LED Horizontally


from sense_hat import SenseHat

sense = SenseHat()
sense.flip_h() FURTHER DETAILS Sense HAT
[Link]

7. Flip the LED Verically cheat SHEET


Produced by TeCoEd
from sense_hat import SenseHat
V.1 SEPTEMBER 2015
sense = SenseHat()
sense.flip_v()
Usage: Code reference for the Sense HAT. Import the 8. Scroll a Message 13. Get Orientation in Radians
module and instantiate an object:
Scrolls a text message from right to left across the LED from sense_hat import SenseHat
from sense_hat import SenseHat matrix and at a specified speed, colour and background
sense = SenseHat() colour. (scroll_speed, back_colour) sense = SenseHat()

from sense_hat import SenseHat orientation_rad = sense.get_orientation_radians()


sense = SenseHat()
sense.show_message("One small step for Pi!",
text_colour=[255, 0, 0]) print("p: {pitch}, r: {roll}, y:
{yaw}".format(**orientation_rad))

9. Show a Single Letter


Displays a single text character on the LED matrix. 14. Get Orientation in Degrees
from sense_hat import SenseHat
import time
from sense_hat import SenseHat sense = SenseHat()
1. Set LED Pixels to Create an Image orientation = sense.get_orientation_degrees()
sense = SenseHat()
from sense_hat import SenseHat print("p: {pitch}, r: {roll}, y:
for i in reversed(range(0,10)):
sense.show_letter(str(i)) {yaw}".format(**orientation))
sense = SenseHat()
[Link](1)
X = [255, 0, 0] # Red 15. Get Orientation
O = [255, 255, 255] # White
10. Get the Humidity Reading from sense_hat import SenseHat
question_mark = [
from sense_hat import SenseHat
O, O, O, X, X, O, O, O, sense = SenseHat()
O, O, X, O, O, X, O, O,
sense = SenseHat()
O, O, O, O, O, X, O, O, orientation = sense.get_orientation()
humidity = sense.get_humidity()
O, O, O, O, X, O, O, O,
O, O, O, X, O, O, O, O,
O, O, O, X, O, O, O, O, print("Humidity: %s %%rH" % humidity) print("p: {pitch}, r: {roll}, y:
O, O, O, O, O, O, O, O, {yaw}".format(**orientation))
O, O, O, X, O, O, O, O
] 11. Get the Current Temperature
from sense_hat import SenseHat 16. Get Compass Reading
sense.set_pixels(question_mark)
sense = SenseHat() from sense_hat import SenseHat
temp = sense.get_temperature()
2. Load an Image sense = SenseHat()
print("Temperature: %s C" % temp) north = sense.get_compass()
Loads an image file, converts it to RGB format and dis-
plays it on the LED matrix. The image must be 8 x 8
pixels in size. 12. Get the Current Pressure print("North: %s" % north)

from sense_hat import SenseHat from sense_hat import SenseHat Note:


sense = SenseHat() Some of the above lines of code are underlined,
sense = SenseHat() this indicates that the code is written on one
pressure = sense.get_pressure() single line but has been edited to fit the
publication.
sense.load_image("space_invader.png") print("Pressure: %s Millibars" % pressure)

You might also like