Difference between revisions of "Raspberry Pi: Sense Hat"

From Embedded Lab Vienna for IoT & Security
Jump to navigation Jump to search
(Created page with "== Summary == Description what this documentation is about == Requirements == * Operating system: Ubuntu 18.04 bionic amd64 * Packages: git emacs In order to complete the...")
 
 
(24 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Summary ==  
== Summary ==  


Description what this documentation is about
This documentation deals with the Raspberry Sense Hat.


== Requirements ==
== Requirements ==


* Operating system: Ubuntu 18.04 bionic amd64
* Hardware: Raspberry Pi
* Packages: git emacs
* Operating system: Rasbian
* Interpreter: Python or Python3
* Packages: sense-hat


In order to complete these steps, you must have followed [[Some Other Documentation]] before.
== Description ==
 
This  add-on board has a is packed with three sensor chips as wall as a 8 by 8 RGB Led matrix and a joystick. The Sensors are a barometer, a hygrometer and an IMU (Inertial Measurement Unit). The barometer measures the air pressure and the temperature. The hygrometer measures the humidity and the temperature as well. The used IMU holds a gyroscope, an accelerometer and a magnetometer for an accurate determination of rotation, movement and detection of magnetic fields.
 
=== Package Installation ===
 
sudo apt-get install sense-hat
 
=== Python Programming ===
 
The Sense Hat is mainly used via python programming. The developers also developed a sense hat programming simulation program on [https://trinket.io/sense-hat trinket.io] where you are able to test your code without owning the Sense Hat.
 
The Project comes with two main libraries the sense_emu which is needed when using the Emulator and the sense_hat for the device. For detailed information about the library use the [https://pythonhosted.org/sense-hat/api/ documentation].
 
==== Importing the Libary  ====
 
# emulator libary
from sense_emu import SenseHat
sense = SenseHat()
 
# sensehat libary
from sense_hat import SenseHat
sense = SenseHat()
 
==== Using the barometer ====
 
The barometer delivers two functions the <code>get_pressure()</code> and the <code>get_temperature_from_pressure()</code>.Both of them return a float value of the corresponding unit.
 
pressure = sense.get_pressure()
print("Pressure: %s Millibars" % pressure)
 
temp = sense.get_temperature_from_pressure()
print("Temperature from pressure sensor: %s C" % temp)
 
==== Using the hygrometer ====
 
The hygrometer delivers also two functions the <code>get_humidity()</code> and the <code>get_temperature_from_humidity()</code>, Both of them return a float value of the corresponding unit.
 
humidity = sense.get_humidity()
print("Humidity: %s %%" % humidity)
 
temp = sense.get_temperature_from_humidity()
print("Temperature from pressure humidity: %s C" % temp)
 
====  Using the Inertial Measurement Unit ====
 
The separte measurement units have their own functions the returns a preformatted dictionary object. The library also delivers a raw funtion that returns unformatted values for the maximum flexibility at after processing
 
'''1. Using the Gyroscope'''
 
The <code>get_gyroscope()</code> function returns a dictionary object with the  pitch, roll, yaw measured in degrees. The raw output function <code>get_gyroscope_raw()</code> returns a dictionary object with the x, y, z axis representing the rotational intensity of the axis in radians per second.
 
These three rotation axis can determine the exect headed direction in the three-dimensional space. The three axis are used like in the image below.
 
[[File:Orientation.png|400px]]
 
 
gyroscope = sense.get_gyroscope()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**gyroscope))
 
# Returned dictionary object
{
  "pitch": Float,
  "roll": Float,
  "yaw": Float
}
 
gyroscope_raw = sense.get_gyroscope_raw()
print("x: {x}, y: {y}, z: {z}".format(**gyroscope_raw))


== Description ==
# Returned dictionary object
{
  "x": Float,
  "y": Float,
  "z": Float
}
 
'''2. Using the Accelerometer  '''
 
The accelerometer  returns the g-force on the corresponding  axis. Therefore are also two functions useable to retrieve the data. <code>get_accelerometer()</code> which returns  a dictionary object with pitch, roll, yaw measured in degrees. And the <code>get_accelerometer_raw()</code> which returns  a dictionary object with x, y, z  representing the acceleration intensity of the axis in Gs.
 
accelerometer = sense.get_accelerometer()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**accelerometer))
 
# Returned dictionary object
{
  "pitch": Float,
  "roll": Float,
  "yaw": Float
}
 
accelerometer_raw = sense.get_accelerometer_raw()
print("x: {x}, y: {y}, z: {z}".format(**accelerometer_raw))
 
# Returned dictionary object
{
  "x": Float,
  "y": Float,
  "z": Float
}
 
'''3. Using the Magnetometer '''
The Magnetometer measures the magnetic force and can idenftify the location of the magnetix north pole of the earth. The <code>get_compass()</code> function returns the diffrence if the rotation between the sensor and the magnetix north pole in degrees. The <code>get_ compass_raw()</code> function returns a dictionary object with x, y, z  representing  the magnetic intensity of the axis in microteslas (µT). 
 
 
compass = sense.get_compass()
print("North: %s" % north)
 
compass_raw = sense.get_compass_raw()
print("x: {x}, y: {y}, z: {z}".format(**compass_raw))
 
# Returned dictionary object
{
  "x": Float,
  "y": Float,
  "z": Float
}
 
====  Using the 8 by 8 LED RGB Matrix ====
 
'''Setting the orientation '''
 
The chip of the sense hat allows us to flip and the rotate the addresses of the LEDs as we want. Therefore got <code>flip_h()</code>, <code>flip_v()</code> and <code>set_rotation(180)</code> implemented by the API developers. The code is as follows:
 
#rotate 180 degrees
sense.set_rotation(180)
 
#flip vertically
sense.flip_v()
 
#flip horizontally
sense.flip_h()
 
'''Displaying Text '''
The API also implements functions to projects letters and moving messages on the LED matrix:
 
#display message
sense.show_message("Hello World!", text_colour = [255, 0, 0], back_colour = [0, 0, 0])


=== Step 1 ===
#display letter
sense.show_letter('s', text_colour = [255, 0, 0], back_colour = [0, 0, 0])


Enter these commands in the shell
'''Painting Pixels '''


echo foo
The every single LED can be addressed and set with RGB values and it is also possible to set the Matrix at once:
echo bar


=== Step 2 ===
#setting a single pixel
sense.set_pixel(x, y, (0, 0, 255))
#setting the LED matrix
g = (0, 255, 0)
b = (0, 0, 255)


Make sure to read
pixels_array = [
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, b, b, g, g, b, b, g,
g, b, b, g, g, b, b, g,
g, g, g, b, b, g, g, g,
g, g, b, b, b, b, g, g,
g, g, b, b, b, b, g, g,
g, g, b, g, g, b, g, g
]


* War and Peace
sense.set_pixels(pixels_array)
* Lord of the Rings
* The Baroque Cycle


== Used Hardware ==
== Used Hardware ==


[[Device to be used with this documentation]]
[[Raspberry Pi 3 Model B+]]
[[Maybe another device to be used with this documentation]]


== Courses ==
[[Raspberry Pi Sense Hat]]


* [[A course where this documentation was used]] (2017, 2018)
== References ==
* [[Another one]] (2018)


== References ==
* https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat
* https://pythonhosted.org/sense-hat/api/#sense-hat-api-reference
* https://trinket.io/sense-hat


* https://wikipedia.org
[[Category:Documentation]]
* https://google.com

Latest revision as of 13:17, 12 October 2020

Summary

This documentation deals with the Raspberry Sense Hat.

Requirements

  • Hardware: Raspberry Pi
  • Operating system: Rasbian
  • Interpreter: Python or Python3
  • Packages: sense-hat

Description

This add-on board has a is packed with three sensor chips as wall as a 8 by 8 RGB Led matrix and a joystick. The Sensors are a barometer, a hygrometer and an IMU (Inertial Measurement Unit). The barometer measures the air pressure and the temperature. The hygrometer measures the humidity and the temperature as well. The used IMU holds a gyroscope, an accelerometer and a magnetometer for an accurate determination of rotation, movement and detection of magnetic fields.

Package Installation

sudo apt-get install sense-hat

Python Programming

The Sense Hat is mainly used via python programming. The developers also developed a sense hat programming simulation program on trinket.io where you are able to test your code without owning the Sense Hat.

The Project comes with two main libraries the sense_emu which is needed when using the Emulator and the sense_hat for the device. For detailed information about the library use the documentation.

Importing the Libary

# emulator libary
from sense_emu import SenseHat
sense = SenseHat()
# sensehat libary
from sense_hat import SenseHat
sense = SenseHat()

Using the barometer

The barometer delivers two functions the get_pressure() and the get_temperature_from_pressure().Both of them return a float value of the corresponding unit.

pressure = sense.get_pressure()
print("Pressure: %s Millibars" % pressure)
temp = sense.get_temperature_from_pressure()
print("Temperature from pressure sensor: %s C" % temp)

Using the hygrometer

The hygrometer delivers also two functions the get_humidity() and the get_temperature_from_humidity(), Both of them return a float value of the corresponding unit.

humidity = sense.get_humidity()
print("Humidity: %s %%" % humidity)
temp = sense.get_temperature_from_humidity()
print("Temperature from pressure humidity: %s C" % temp)

Using the Inertial Measurement Unit

The separte measurement units have their own functions the returns a preformatted dictionary object. The library also delivers a raw funtion that returns unformatted values for the maximum flexibility at after processing

1. Using the Gyroscope

The get_gyroscope() function returns a dictionary object with the pitch, roll, yaw measured in degrees. The raw output function get_gyroscope_raw() returns a dictionary object with the x, y, z axis representing the rotational intensity of the axis in radians per second.

These three rotation axis can determine the exect headed direction in the three-dimensional space. The three axis are used like in the image below.

Orientation.png


gyroscope = sense.get_gyroscope()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**gyroscope))
# Returned dictionary object
{
  "pitch": Float,
  "roll": Float,
  "yaw": Float
}
gyroscope_raw = sense.get_gyroscope_raw()
print("x: {x}, y: {y}, z: {z}".format(**gyroscope_raw))
# Returned dictionary object
{
  "x": Float,
  "y": Float,
  "z": Float
}

2. Using the Accelerometer

The accelerometer returns the g-force on the corresponding axis. Therefore are also two functions useable to retrieve the data. get_accelerometer() which returns a dictionary object with pitch, roll, yaw measured in degrees. And the get_accelerometer_raw() which returns a dictionary object with x, y, z representing the acceleration intensity of the axis in Gs.

accelerometer = sense.get_accelerometer()
print("p: {pitch}, r: {roll}, y: {yaw}".format(**accelerometer))
# Returned dictionary object
{
  "pitch": Float,
  "roll": Float,
  "yaw": Float
}
accelerometer_raw = sense.get_accelerometer_raw()
print("x: {x}, y: {y}, z: {z}".format(**accelerometer_raw))
# Returned dictionary object
{
  "x": Float,
  "y": Float,
  "z": Float
}

3. Using the Magnetometer The Magnetometer measures the magnetic force and can idenftify the location of the magnetix north pole of the earth. The get_compass() function returns the diffrence if the rotation between the sensor and the magnetix north pole in degrees. The get_ compass_raw() function returns a dictionary object with x, y, z representing the magnetic intensity of the axis in microteslas (µT).


compass = sense.get_compass()
print("North: %s" % north)
compass_raw = sense.get_compass_raw() 
print("x: {x}, y: {y}, z: {z}".format(**compass_raw))
# Returned dictionary object
{
  "x": Float,
  "y": Float,
  "z": Float
}

Using the 8 by 8 LED RGB Matrix

Setting the orientation

The chip of the sense hat allows us to flip and the rotate the addresses of the LEDs as we want. Therefore got flip_h(), flip_v() and set_rotation(180) implemented by the API developers. The code is as follows:

#rotate 180 degrees
sense.set_rotation(180)
#flip vertically
sense.flip_v()
#flip horizontally
sense.flip_h()

Displaying Text The API also implements functions to projects letters and moving messages on the LED matrix:

#display message
sense.show_message("Hello World!", text_colour = [255, 0, 0], back_colour = [0, 0, 0])
#display letter
sense.show_letter('s', text_colour = [255, 0, 0], back_colour = [0, 0, 0])

Painting Pixels

The every single LED can be addressed and set with RGB values and it is also possible to set the Matrix at once:

#setting a single pixel
sense.set_pixel(x, y, (0, 0, 255))

#setting the LED matrix
g = (0, 255, 0)
b = (0, 0, 255)
pixels_array = [
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, b, b, g, g, b, b, g,
g, b, b, g, g, b, b, g,
g, g, g, b, b, g, g, g,
g, g, b, b, b, b, g, g,
g, g, b, b, b, b, g, g,
g, g, b, g, g, b, g, g
]
sense.set_pixels(pixels_array)

Used Hardware

Raspberry Pi 3 Model B+

Raspberry Pi Sense Hat

References