Getting started with the Jam Hat
Getting started with the Jam Hat
- Adding the board to the Raspberry Pi
- Using GPIO Zero with the Jam Hat
- Further examples
2. Add the four posts onto the Jam Hat at the four corner holes using a small phillips head screwdriver.
3. Once these are in place, push the HAT into place on to the Raspberry Pi making sure that the four posts line up with the four corner holes.
4. Fix the Hat securely into place using the remaining four screws in the bottom of the posts.
5. If they aren't attached already, add the button caps onto the push to make buttons if they are not attached already. We have put our blue cap on the button at the bottom left of the board and the red cap on the button at the bottom middle of the board.
sudo apt-get update sudo apt-get install python3-gpiozero python-gpiozeroOnce these commands have run you should have the most up to date version of GPIO Zero.
The lights, buttons and buzzer are available to use through GPIO Zero which we're going to use in the examples below. Each component is an object in the Jam Hat class:
- jamhat.lights_1: The top row of lights closest to the header. Each coloured light can be used by adding .red, .yellow or .green onto jamhat.lights_1.
- jamhat.lights_2: The bottom row of lights furthest from the header. Again, each coloured light can be used by adding .red, .yellow or .green onto jamhat.lights_2.
- jamhat.button_1: The button on the left of the pair. Ours has a blue cap on.
- jamhat.button_2: The button on the right of the pair. The one with the red cap in the picture.
- jamhat.buzzer: The buzzer at the bottom right of the board.
from gpiozero import JamHat from time import sleep jamhat = JamHat() jamhat.lights_1.blink() sleep(0.5) jamhat.lights_2.blink()And we can turn on individual lights by addressing them individually using their colour.
from gpiozero import JamHat from time import sleep jamhat = JamHat() # Turn the red lights on jamhat.lights_1.red.on() jamhat.lights_2.red.on() sleep(0.5) jamhat.off() # Turn the yellow lights on jamhat.lights_1.yellow.on() jamhat.lights_2.yellow.on() sleep(0.5) jamhat.off() # Turn the green lights on jamhat.lights_1.green.on() jamhat.lights_2.green.on() sleep(0.5) jamhat.off()
from gpiozero import JamHat jamhat = JamHat() jamhat.button_1.when_pressed = jamhat.on jamhat.button_2.when_pressed = jamhat.off
from gpiozero import JamHat from time import sleep jamhat = JamHat() jamhat.buzzer.play('C4') sleep(0.5) jamhat.off() jamhat.buzzer.play(70) sleep(0.5) jamhat.off() jamhat.buzzer.play(220.0) sleep(0.5) jamhat.off()example script in our GitHub, we can use all three components on our board. The following script uses button 1 to play the buzzer, button 2 decreases the pitch of the buzzer and the LEDs indicate where you are on the scale of notes.
from gpiozero import JamHat from time import sleep jh = JamHat() NOTES = ['A4', 'G4', 'F4', 'E4', 'D4', 'C4'] i = 0 j = 0 note = 0 try: while True: if(jh.button_2.is_pressed): # If the red button is pressed, move the LED and make the buzzer note lower note = (note + 1) % 6 if(j == 2): i = (i + 1) % 2 j = (j + 1) % 3 sleep(0.1) jh.off() jh[i][j].on() if(jh.button_1.is_pressed): # If the blue button is pressed, play the note we're currently on. jh.buzzer.play(NOTES[note]) sleep(0.1) except KeyboardInterrupt: jh.close()
GitHub page. Also there is full documentation available for how the Hat has been implemented in GPIO Zero available so you can hack away to your hearts content!