Raspberry Pi Learning Resources

# 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.

## Using set_pixel

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.

1. Start by opening Python 3 (IDLE) from the main menu:

2. Create a new file by clicking `File > New file`.

3. Save the new file as `sparkles.py`.

4. In the new file, start by importing the Sense HAT module:

``from sense_hat import SenseHat``

``sense = SenseHat()``
6. Now think of a random number between 0 and 7 and assign it to the variable `x`, for example:

``x = 4``
7. Think of another random number between 0 and 7, then assign it to `y`:

``y = 5``
8. Think of three random numbers between 0 and 255, then assign them to `r`, `g`, and `b`:

``````r = 19
g = 180
b = 230``````
9. Now use the `set_pixel` function to place your random colour at your random location on the display:

``sense.set_pixel(x, y, r, g, b)``
10. 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)``````
11. 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 `x` and `y` are coordinates - the top-left corner pixel is `(0, 0)` and the bottom-right corner is `(7, 7)`. The `r`, `g` and `b` numbers 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.

12. 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.

1. Add another `import` line at the top of your program, below `from sense_hat import SenseHat`:

``from random import randint``
2. Now change your `x =` and `y =` lines to automatically select a random position:

``````x = randint(0, 7)
y = randint(0, 7)``````

The `randint` function (random integer) chooses a random number between the two given numbers, in this case 0 and 7.

3. 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.

4. 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.

5. Run it again, and you should see another pixel appear in a random location with a random colour.

6. Run it a few more times, and you should see more of the grid fill up with random pixels.

Rather than have to keep running your program, you can add a loop so that it will keep going.

1. First, add an `import` to the top of your file:

``from time import sleep``

You'll use this to pause the program between pixels.

2. Add a `while True:` to your code so that the random lines, `set_pixel` and `sleep` are 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)``````
3. Add a `sleep` into your program, after `set_pixel` but still inside the loop:

``sleep(0.1)``
4. Run the code and you should see random sparkles in action!

## What next?

• Try reducing your `sleep` down to `0.01` so it runs faster
• Try removing the `sleep` line 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 `0` to bias the colour mix - for example, `r, g, 0` will only make red/green/yellow colours
• What else can you draw on your Sense HAT display? Try making an animated Pixel Pet!