What’s a hat?
Hats are the term that the Raspberry PI community uses to identify bits of hardware that are made to plug into the Raspberry Pi’s GPIOs (General Purpose IO) – that row of pins along one side of your pi. There are lots of these out there to make interacting with the world easy. In this lesson we are going to make use of the the Sense Hat.
The Sense Hat
This little puppy is neat as it has a collection of devices we can easily get started with that will help us try out some of the skills you learned about in the earlier lessons. It has a pile of LEDs we’ll play with, a joy stick, and some other interesting measurement devices.
Adafruit.com is a great place to find accessories for your pi. Here is a couple of mini videos of fun hats:
Installing the Sense Hat…
First – lets install the Sense Hat. The Sense Hat doesn’t fit well with the official Raspberry PI Case – but you don’t want to expose the pins on the bottom of the board to anything conductive by accident, so we’ll just use the bottom half as a tray.
First – power down the pi, with:
sudo shutdown -h now
Then – remove the SD card. One of the biggest problems when playing with the case is accidentally ripping the SD card holder off of the circuit board. So take this out for safety:
Finally, just remove the top two pieces of the case so you are left with something like this (Note: this is the pi with the sense hat installed):
Be gentle when installing the hat. The screw holes should align, and the pins of the PI should align and easily slip into the socket on the bottom of the hat.
Once you have it installed. Re-insert your SD card, and plug the power back in. If everything went well – you should be rewarded with:
Those lights will go out after a moment of running… but don’t worry – you’ll soon be able to turn that on whenever you like.
Trying it out…
The Sense Hat has great software associated with it. Go ahead and SSH into your PI, or open a terminal… like we did in Lesson 1.
Lets grab that using apt-get. Remember we saw that in Lesson 2. So lets use it again here:
sudo apt-get install sense-hat
Then lets also get the example source using the other thing we learned in Lesson 2, git:
cd ~ git clone https://github.com/RPi-Distro/python-sense-hat.git
Now you should have both the software installed AND the source code that built that software. So, lets cd (change directory, from Lesson 3) into the examples that we get for free from the GIT repository we just downloaded.
cd python-sense-hat cd examples
Now lets list the examples files:
You should see something like this:
There are some fun bits in there! Lets try out a couple of these treasures:
When you are done staring at the pretty colors – hit CTRL-C (Control and the ‘C’ key at the same time.) Then go ahead and try out the other programs – they all exercise different ideas for the LEDs and they start to play with the sensors.
When you are done, come back and we’ll take a deeper look at text_scroll.py
Edit some code…
A good first programming task is always, “Hello World” – every professional programmer has written numerous versions of this. In fact they’ve probably done it in every language and environment they’ve ever played with. You should now continue this tradition.
Edit the text_scroll.py file:
A little bit on VIM
VIM, the program you just launched, is a very powerful text editor. It doesn’t look like much, but moving around files efficiently is something programmers must be able to do efficiently.
Anyway – Vim has two main modes. The normal mode, which the editor starts in and which allows the programmer to move around the file, and the insert mode, where the programmer is typing in new text.
While in normal mode, which you can switch to with the ESC key on the upper left, you move around the file with the keyboard. The keys: j moves down, k moves up, h moves left, and l moves right.
Editing the scroll text with VIM…
See if you can get the cursor to the line and character that contains “One small step for PI!”
If you make it change that line by first hitting x repeatedly to delete the text you don’t want. Then enter insert mode by hitting i. Now, when you type, the characters should show up. When you are done, jump back into normal mode by hitting ESC.
Great! Now lets save that out. Hit the colon key… its the one that looks like this : and its found above the semi-colon ; on the right-side of the keyboard. You’ll have to hold shift to get it. This drops you into an entirely new mode called the command mode. Once you’re there you’ll give it the command to write the file and exit: wq
Poof! Well done! You just did a little programming. Lets see if it worked:
Did you see your new message? Great! If you received an error message. Make sure you didn’t delete too much. Re-open the file using:
Navigate back to the line in the file and make sure it has the quotes around the text still:
sense.show_message("One small step for Pi!", text_colour=red)
Check out this sheet put together by the PI folks. You’ve just scratched the surface – this will take you a lot deeper.