Home RASPBERRY PI Python Programming with Sense HAT in Raspberry Pi

Python Programming with Sense HAT in Raspberry Pi

Once you know how the exciting Raspberry Pi Sense HAT Sensor Add-on is very easy to use. Then we will tell you how. In this Raspberry Pi Sense Hat article, we will first look at the impressive Raspberry Pi Sense Hat add-on board.

With this add-on board, we can easily sense temperature, humidity, and pressure. It also has an embedded gyroscope, accelerometer and a joystick, etc … let’s get started without any further delay! If You don’t what is raspberry and what is Sense Hat then I will suggest you please have a look at my Previous Articles.

How to Setup Noobs in Raspberry Pi?

What is Sense Hat and what can we do with it?

I developed a very simple python program to print the command line temperature, humidity and pressure. The program’s file name is test.py, on the desktop. The code of the program is very simple.

What we did was import the SenseHat library, create the SenseHat object and we’re ready to use the hat. To get the temperature, we use the following command: temperature = sense.get_temperature (). This is easy! Then we use the print command to print the value to the command line!

If we want to print the temperature on the LED matrix, we can simply convert the value to a string variable and output it with the following command: sense.show_message (text). The sensor cap’s library is very easy to use. Sense hat is plug and play.

So let’ Get Started

Step 1

 Install Rasbian and update

How to install Rasbian Please have a look at this article so then you can come to know how can you install it in Raspberry Pi

Update the Rasbian with command mention below

sudo apt-get update

sudo apt-get upgrade

Step 2

Install Library

The Sense HAT library should be installed already, but it’s always good to check:

sudo apt-get install sense-hat

If you see a message that says “sense-hat is already the newest version,” that means you’re all set and you have already had sense hat library. Let’s move on.

After the installation, you came to know there are many examples of sense Hat copied in your raspberry pi so example code can be found here just to the raspberry pi location and check the example of python programming

/usr/src/sense-hat/examples

So if you want to copy these files so here is the command you put in your terminal so then you can easily copy the examples

 

cp /usr/src/sense-hat/examples ~/ -a

These Examples can be compiled with C/C++ Language for this just write the command in Terminal window as “make”

Step 3 Start Coding

We’re going to create a new Python file just create it with terminal:

nano first program.py

Put the code in the Editor so in this program you are able to send scrolling name in Sense hat.

from sense_hat import SenseHat

sense = SenseHat()

blue = (0, 0, 255)

yellow = (255, 255, 0)

red =(255,0,0)

white =(255,255,255)

while True:

sense.show_message(“TECHNICAL USTAAD “, text_colour=red, scroll_speed=0.2)

As the shown in the video below

Python Programming with Sense HAT in Raspberry pi

 

Displaying a single character

Display the letter “A” on your Sense HAT’s LED display.

from sense_hat import SenseHat

sense = SenseHat()

sense.show_letter(“Z”)

Displaying images
from sense_hat import SenseHat

 

sense = SenseHat()

Define some colours

g = (0, 255, 0) # Green

b = (0, 0, 0) # Black

Set up where each colour will display

creeper_pixels = [

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

]

Display these colours on the LED matrix

sense.set_pixels(creeper_pixels)

Using Joystick

Depending on which way the joystick was pressed, display one of the letters U, D, L, R or M on the LED matrix.

from sense_hat import SenseHat

from time import sleep

sense = SenseHat()

 

e = (0, 0, 0)

w = (255, 255, 255)

 

sense.clear()

while True:

for event in sense.stick.get_events():

Check if the joystick was pressed

if event.action == “pressed”:

Check which direction

if event.direction == “up”:

sense.show_letter(“U”)      # Up arrow

elif event.direction == “down”:

sense.show_letter(“D”)      # Down arrow

elif event.direction == “left”:

sense.show_letter(“L”)      # Left arrow

elif event.direction == “right”:

sense.show_letter(“R”)      # Right arrow

elif event.direction == “middle”:

sense.show_letter(“M”)      # Enter key

Wait a while and then clear the screen

sleep(0.5)

sense.clear()

if you want to learn more stuff about sense then please visit the official website of Raspberry Pi

Visit similar tutorials on Raspberry pi

How to Build your own Super Computer with Raspberry Pi 3 Cluster

Build Super Computer with 5$ Raspberry pi zero using Cluster HAT

How to make a Raspberry Pi bitcoin mining rig

Convert Any Printer into a Wireless Printer with a Raspberry Pi zero (10$ Cost)
Build AYI Google Voice Home with Raspberry Pi Under 35$

Best Raspberry Pi Alternatives ( Updated 2018 List)