Loading...
Hello, my name's Mrs. Jones, and I'm really pleased you decided to join this lesson today.
In this lesson, we will look at using the Sense HAT add-on board for the Raspberry Pi, and using the emulator to learn to programme with it even if you do not have the hardware.
So let's get started.
Welcome to today's lesson.
Today's lesson is called, "Programming Project - Sense HAT I," from the unit, "Programming: Strings and lists." And by the end of this lesson you'll be able to use a Sense HAT to display text and pixel animations.
There are two keywords to today's lesson.
Sense HAT.
Sense HAT is hardware that attaches to the top of a Raspberry Pi computer and allows you to write programmes that collect data from sensors.
It also has an LED matrix for displaying output.
Sense HAT emulator.
The Sense HAT emulator is a digital representation of the physical Sense HAT device.
There are two parts to today's lesson.
The first is use a Sense HAT to display text, and the second, use a list to display pixel animations.
So let's start with use a Sense HAT display text.
What is a Sense HAT? The Sense HAT is an add-on board for the Raspberry Pi computer that was designed for the Astro Pi space mission.
There are currently two Raspberry Pi computers with Sense HATs orbiting the earth on the International Space Station.
Sofia asks, "Why is it called a HAT?" Really good question.
HAT stands for Hardware Attached on Top.
You can see there the H-A-T is from each of those words.
The Sense HAT sits on top of the Raspberry Pi computer and allows you to write programmes that interact with the sensors and the LED matrix.
You can see the Sense HAT on top of the Raspberry Pi computer in this photograph.
Let's see some features of the Sense HAT.
We have an 8x8 RGB LED matrix display, gyroscope, accelerometer, and magnetometer, temperature and humidity sensor, barometric pressure sensor, and a mini joystick.
Sofia says, "But I don't have a Raspberry Pi computer or a Sense HAT." The Sense HAT emulator has been designed for the Sense HAT so that anyone can learn to programme with it even if they don't have the physical device.
You can run the Sense HAT emulator in the Raspberry Pi code editor.
You can see here on the left you have the coding area, and on the right in the top area is the emulator with the sensors underneath there for temperature, pressure, humidity, and you've got colour, motion, and timer.
In the visualisation provided in the Raspberry Pi code editor, the Sense HAT is shown on the Astro Pi computer.
You can see that here.
An Astro Pi computer is a Raspberry Pi computer equipped with sensors and housed in a special case.
This code imports the Sense HAT library.
You will need these two lines of code whenever you wish to use a Sense HAT.
You can see their line one, from sense_hat import SenseHat, and two, sense = SenseHat ().
Make note of the capitals and lowercase letters because those are important too.
This is a function that takes a message.
Formats it so that it can be shown on an LED display, and scrolls it smoothly across the LED matrix.
And that's here on line three, sense.
show_message("Hello world!").
Let's have a quick check.
Fill in the blanks to complete the sentence.
The blank has been designed for the blank so that anyone can learn to programme with it even if they don't have the blank device.
You have the words "physical," "Sense HAT emulator," and "Sense HAT." Pause the video to consider which word goes where within that sentence, and then we'll go through it.
Let's check your answer.
The "Sense HAT emulator" has been designed for the "Sense HAT" so that anyone can learn to programme with it even if they don't have the "physical" device.
Well done if you've got that correct.
The show_message function can take more than one argument.
You can also specify the text_colour, back_colour, and the scroll_speed.
In this example, the text_colour is being specified.
So you can see on line three, sense.
show_message ("Hello world!", text_colour equals = (0, 0, 255)).
Let's have a quick check.
What did the three numbers 0, 0, 255 signify in the show_message function? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
RGB.
The numbers represent the RGB values.
The first value is for the red, the second is for green, and the third is for blue.
This would display the text in blue, and the values can go between 0 and 255.
Let's do the activity.
There are three parts.
One, open the Raspberry Pi code editor and import the Sense HAT library.
Two, use the show_message function to display the text "Hello World!" in a colour of your choice.
Three, can you add some code to change the background colour of the message as well? Note, if you're not sure of the colour codes, visit oak.
link/colour-picker to investigate different colour options.
Pause the video, go back through the slides, and have a go at completing that activity for each of those steps, and then we'll go through a solution.
Let's have a look at the solution.
Here, we have one and two completed.
We have the "Open the Sense HAT library" on lines one and two, the two lines of code that must be there.
And then on line three, we have "Hello World!" in a colour of choice.
So on the RGB code there we have 153, 255, 153.
And then having a go at the background colour as well, you can see now after we have text_colour there is a comma, and we have back_colour = (0, 0, 102).
Well done for completing that activity.
Let's move to the second part of today's lesson, use a list to display pixel animations.
Sofia says, "I managed to display my message, but now my LEDs won't switch off." To clear the screen, you need to use sense.
clear (), sense.
clear ().
This code snippet will switch off all of the LEDs on the matrix.
You could also add a colour as an argument to fill the whole display with a single colour, sense.
clear(red).
Let's have a quick check.
What do you think the output of this programme will be when it's executed? You can see there on line one and two the importing and setting it all up.
On line four, we have yellow = (255, 255, 0), blue = (0, 0, 255), and then on line seven we have sense.
set_pixel(0, 0, (yellow)).
And the same on the next line on line eight, but inside the brackets (7, 7, (blue)).
Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The code snippet sets a pixel at a specified location to a specific colour.
In this case, yellow and blue.
Well done if you got that correct.
This is the codes snippet that sets a pixel at a specified location to a specific colour.
So you can see there sense.
set_pixel(0, 0, (yellow)).
This set the pixel at this X coordinate, and this Y coordinate to this colour.
X goes across and Y goes down.
Locations begin at 0 and end at 7.
You can see there across the top, X, 0, 1, 2, 3, 4, 5, 6, 7, and then Y going down, 0 to 7.
Setting a pixel is a useful tool.
However, it can take a lot of thought to create a full graphic.
You can see the code here for creating that image on the Sense HAT display there.
It's quite a lot of lines of code just for that one image.
This is where a list can become a useful tool.
A list can be useful to set all of the pixels on the matrix in one structure.
Let's have a look here.
So we've got the lines of code at the top, one and two, that we have to have there.
You see we have r and b set up, that's going to set the colours, and we've got a comment at the end of each.
So setting R to blue.
So it's setting R to red and B to blue, the RGB code therefore.
Then we've got smile =, and within the list there you can see the way it's structured is the same as the display.
So you have the 8 across and 8 down from 0 to 7.
And then at the end on line 18 you have sense.
set_pixels(smile).
The list can be presented in the code to match the layout of the matrix.
The list is then passed as an argument to the set_pixels function.
The output is displayed on the matrix, and you can see there the red and the blue displayed.
Let's have a quick check.
I want to create an animation on my Sense HAT, so I will need to have more than display on the matrix.
How can I do that? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
"Sofia will need to create multiple lists that will hold the frames of her animation.
The lists will then need to be passed as arguments to the set_pixels function on separate lines in the programme." Well done if you got that correct.
Sofia says, "I've created multiple lists and passed them to the set_pixels function, but they move so quickly that I only ever see the final list.
What should I do?" Pause the video to consider your answer and then we'll go through it.
Let's check your answer.
"Sofia should use the sleep() function to slow the programme down.
She would need to remember the import statement for time and sleep though." Well done if you got that correct.
Let's do the activity.
Use the pixel-art.
xls sheet to plan out three pixel frames for your animation.
You can see there frame 1.
Also have the colour palette there with the letters and the different RGB values for each of those.
Pause the video to complete the activity and then we'll go through it.
Let's have a look at the solution.
So here you can see one possible solution here.
So we have white, which is I, all right, we've got the green, which is B, and then we've got F twice there, it's like little eyes.
Well done if you did that.
Then let's look at the rest of the activity.
Two, create three lists that will hold your three frames for your animated character.
Hint, make sure that you create the variables for each colour.
Use the RGB codes to help with this.
Three, complete your programme so that the Sense HAT correctly displays your three animated frames.
Four, adapt your programme so the animation continually loops.
Pause the video, go back through the slides, use the RGB code colours to look at, and then we'll go through a possible solution.
Let's have a look at the solution.
So this is the start of the code.
So we've got frame 1 there.
So we've set up the white, green, and black.
So the I, B, and F variables are set up with the right RGB codes, and you can see frame 1 there.
And then you've got frame 2 and frame 3 would be there.
So your solution will contain the complete list for the frame here.
And then at the bottom we've got the "while True," and we've got each of those frames being called and we've also got a sleep(0.
2) in between each one.
Well done if you completed that activity.
In summary, the Sense HAT is an add-on board for the Raspberry Pi computer.
HAT stands for Hardware Attached on Top.
The Sense HAT sits on top of the Raspberry Pi computer and allows you to write programmes that interact with the sensors and the LED matrix.
You can use lists to set all of the pixels on the matrix in one structure.
Well done for completing this lesson, "Programming project: Sense HAT I.".