In this project you will use the temperature and humidity sensors on the Sense HAT to predict when there’s a good chance of spotting a rainbow. When the right conditions are detected you will display a rainbow on the Sense HAT LED Matrix.
You will be writing code in the Python programming language, which you may have learnt in the Python module.
In the Trinket emulator you can move the sliders to change the temperature and humidity, they look like this:
You’ll get a rainbow if the temperature is over 20 degrees C and the humidity is over 80 percent. Try experimenting to find the weather conditions for sunshine (yellow) and snow (white).
Step 1: Drawing a rainbow
First let’s draw a rainbow using the LED Matrix on the Sense HAT. The colours are Red, Orange, Yellow, Green, Blue, Indigo and Violet.
To set the colour of an individual LED we need to say how much red, green and blue it should have from 0 to 255.
Open the Rainbow Predictor Starter Trinket: jumpto.cc/rainbow-go.
The code to set up the Sense HAT has been included for you.
Add the highlighted code to set up a variable for the colour Red and then turn all the pixels red using
Make sure you use a capital letter
Orange is next. Orange is red mixed with green. You can adjust the numbers until you get an orange that you like. Make sure you use a capital letter
Onot a number zero. Use
sense.clear(O)this time to test the new colour.
Now add variables
Vso that you have the seven colours of the rainbow. You can look up RGB colours at jumpto.cc/colours
You can test your colours using
Add a variable
Xfor setting pixels to off (no red, green or blue):
Now it’s time to draw a rainbow. You need to set up a list containing the colour of each pixel and then call
set_pixelswith the list of colours. To save typing you can copy the rainbow from
snippets.pyin your project.
Step 2: Reading the sensor data
The Sense HAT has a range of sensors that provide real world data on a Raspberry Pi computer. The Sense HAT Emulator in Trinket allows you write and test projects for the Sense HAT in your web browser.
The humidity sensor reports the amount of moisture in the air. Humidity is high when it rains.
Let’s read from the humidity sensor and print out the result. Add the highlighted code to the bottom of your script.
Test your program by moving the humidity slider to different values.
Notice that the value you get back from the humidity sensor isn’t exactly the same as the value on the slider. This is because the sensors aren’t perfectly accurate.
Step 3: Predicting a rainbow
Rainbows happen when the sun shines through water droplets at the right angle (usually in the afternoon.) If it’s warm and the humidity is high then it’s worth checking to see if there’s a rainbow.
Now let’s only show the rainbow if the conditions are right. Change your code to look like this:
You can’t be sure there’s a rainbow when these conditions are met, but it’s worth a look.
Try changing the values on the slider until you see the rainbow.
Remember that the values reported won’t be exactly the same as those shown on the sliders.
A threshold is a number that indicates an important change. 20 degrees C and 80% humidity are thresholds for the rainbow detector.
Try changing the thresholds and then moving the sliders to trigger a rainbow.
If you’re working with a physical Sense HAT then you can test your code by setting the thresholds to low values.
Challenge: More Weather
Can you display a sun image when the temperature is above 20 and the humidity is below 80%.
elif to check for other kinds of weather. For each kind of weather you’ll need to include a condition to check for a kind of weather and then code to set the display on the Sense HAT.
Tip: You can create a simple sun by setting all the pixels to yellow with
clear(). Or you can try creating a pixel image like you did with the rainbow.
How about a snow image if the humidity is above 80% and the temperature is below zero.
Tip: Set red, green and blue to the maximum of 255 to create white.