Sense HAT Random Sparkles
Let your Sense HAT shine with pride!
Start by attaching the Sense HAT to your Raspberry Pi.
If you do not have a Sense HAT, you could create the project in a web browser using the Sense HAT emulator.
First, we'll think up some random numbers and use the
set_pixel function to place a random colour on a random location on the Sense HAT display.
Start by opening Python 3 (IDLE) from the main menu:
Create a new file by clicking
File > New file.
Save the new file as
In the new file, start by importing the Sense HAT module:
from sense_hat import SenseHat
Next, create a connection to your Sense HAT by adding:
sense = SenseHat()
Now think of a random number between 0 and 7 and assign it to the variable
x, for example:
x = 4
Think of another random number between 0 and 7, then assign it to
y = 5
Think of three random numbers between 0 and 255, then assign them to
r = 19 g = 180 b = 230
Now use the
set_pixelfunction to place your random colour at your random location on the display:
sense.set_pixel(x, y, r, g, b)
Check over your code. It should look like this, with your own random numbers assigned to the variables:
from sense_hat import SenseHat sense = SenseHat() x = 4 y = 5 r = 19 g = 180 b = 230 sense.set_pixel(x, y, r, g, b)
Now run your code by pressing
F5. You should see a single pixel light up.
You might have guessed where the pixel would appear, and maybe you had an idea what colour it was going to be. The
yare coordinates - the top-left corner pixel is
(0, 0)and the bottom-right corner is
(7, 7). The
bnumbers are the red, green and blue parts of the colour.
(255, 0, 0)would be pure red,
(0, 255, 0)would be pure green, and
(255, 255, 0)would be yellow.
- Now pick some new random numbers - change them all - and run the program again. A second pixel should appear on the display!
Using the random module
So far you picked your own random numbers, but you can let the computer choose them instead.
importline at the top of your program, below
from sense_hat import SenseHat:
from random import randint
Now change your
y =lines to automatically select a random position:
x = randint(0, 7) y = randint(0, 7)
randintfunction (random integer) chooses a random number between the two given numbers, in this case 0 and 7.
Run your program again, and you should see another random pixel being placed on the display. It will be the same colour you chose previously.
Now change your colour value lines to:
r = randint(0, 255) g = randint(0, 255) b = randint(0, 255)
Now your program will automatically select a random colour.
Run it again, and you should see another pixel appear in a random location with a random colour.
- Run it a few more times, and you should see more of the grid fill up with random pixels.
Add a loop
Rather than have to keep running your program, you can add a loop so that it will keep going.
First, add an
importto the top of your file:
from time import sleep
You'll use this to pause the program between pixels.
while True:to your code so that the random lines,
sleepare all within the loop:
while True: x = randint(0, 7) y = randint(0, 7) r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) sense.set_pixel(x, y, r, g, b)
sleepinto your program, after
set_pixelbut still inside the loop:
Run the code and you should see random sparkles in action!
- Try reducing your
0.01so it runs faster
- Try removing the
sleepline so there's no delay at all
- Try limiting the colour numbers to
randint(100, 255)to reduce the intensity of the colours
- Try fixing one of the colour values to
0to bias the colour mix - for example,
r, g, 0will only make red/green/yellow colours
- What else can you draw on your Sense HAT display? Try making an animated Pixel Pet!