Introduction: Weather Display With Sense Hat

With the new Sense Hat, you no longer need extra parts, all sensors are included! Simply attach the Sense Hat atop your Raspberry Pi 3 and you are all set!

- Gyroscope - Temperature

- Accelerometer - Barometric Pressure

- Magnetometer - Humidity

Follow along with this simple tutorial and discover the weather around you.

We will be using the Temperature, Humidity and Pressure sensors for this project.

Step 1: Gather Components

The following components are used for this project:

Raspberry Pi 3

Sense Hat

Sense Hat case New! See-through enclosure protects your LED screen without obstructing visibility.

HDMI Cable

Monitor

Keyboard

Mouse

Looking for a great introduction to Raspberry Pi? Try the Zagros Raspberry Pi 3 Starter Kit which has everything you need to begin discovering Raspberry Pi.

Step 2: Update and Upgrade

Make sure your system is updated and upgraded with the latest version by running the following commands in the Terminal. (Terminal is the computer icon on the Raspberry Pi menu bar)

1.) Type this command and press enter.

sudo apt-get update

2.) Type this command and press enter.

sudo apt-get dist-upgrade

Step 3: Open Python Editor.

1.) Press Menu button.

2.) Navigate to Programming >> Python-3 (IDLE).

You have now opened the Python Shell.

3.) Click File.

4.) Click New File.

Step 4: Import Modules for Your Project.

Type or copy into editor:

from sense_hat import SenseHat

import time

Step 5: Set Variables.

Pi was created in England, so the temperature output is in Celsius. The conversion to Fahrenheit is included below.

Type or copy into editor:

sense = SenseHat()

temp = round(sense.get_temperature()*1.8 +32)

humidity = round(sense.get_humidity())

pressure = round(sense.get_pressure())

message = 'Temperature is %d F Humidity is %d percent Pressure is %d mbars' %(temp,humidity,pressure)

Step 6: Display Weather on Sense Hat!

The show_message() command displays your message like a banner across the LED screen. Scroll_speed allows you to control how fast the message rolls across. I've set the text_colour to pink and the back_colour to blue. Find your favorite contrast and change your colors.

Type or copy into editor:

sense.show_message(message, scroll_speed=(0.08),text_colour=[200,0,200], back_colour= [0,0,200])

To clear the screen after the message is complete. Type:

sense.clear()

To run your program, press F5.

(The Python file for the project is attached)

Step 7: Keep a Weather Log.

Great! You created a weather banner!

Now you can amend your program to save your weather data in a text file.

At the very top of the file add this module:

from time import asctime


Step 8: Create Text File.

At the very bottom of the file add a while statement:

while True:

log = open("weather.txt", "a")

The second argument indicates how the file is to be opened "a"= append and "w" will write over anything that was was already in the file.

now = str(asctime())

Asctime() outputs a time-stamp in an easy to read form.

log.write(now + " " + message + "\n")

print(message)

log.close()

sleep(30)

Waits 30 seconds before continuing with the loop.

You can adjust this time however you like. If you have a portable battery pack you can take samples of weather in different rooms, the basement, garage, inside your car, and outside!

To run your program press F5 while the cursor is in your program file.

Step 9: Alternate Loop.

Your program will continue to run without end unless you kill it in the Shell by typing Ctrl + C.

You can set the amount of times it will run by using a for loop to set the range:

for i in range(10):

log = open("weather.txt", "a")

now = str(asctime())

log.write(now + " " + message + "\n")

print(message)

sleep(30)

log.close()

Step 10: Weather.txt Sample.

Open the weather.txt file and it should look something like this.

With the current setting, each time you run the program the new data will be appended after the last line.

Step 11: ## Commenting Tip.

As a note, the message will display completely before the weather is logged to the text file.

If you would like to skip the message displaying on your Sense Hat, make it a comment:

1.) Highlight text.

2.) Click Format.

3.) Click Comment Out Region.

or

1.) Highlight text.

2.) Alt + 3.