Getting started with the Jam Hat

Getting started with the Jam Hat

 

 

 

Getting started with the Jam Hat

     

    Adding the board to the Raspberry Pi

    1. The hat comes with 4 brass posts, 8 screws and 2 button caps.

     

    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.

     

     

     

    Using GPIOZero with the JamHat

    Once you've added the Hat onto your Raspberry Pi, power up your Pi.

     

     

    How to update GPIO Zero to version 1.5

    Code to use the JamHat is available in GPIOZero version 1.5 and newer. To update your version of GPIOZero, open a terminal on your Pi and type:

     

     

    sudo apt-get update
    sudo apt-get install python3-gpiozero python-gpiozero
    Once these commands have run you should have the most up to date version of GPIO Zero.

     

     

    How is the board setup in GPIOZero?

    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.

        Turning on the lights

        There are 6 LEDs on the Jam Hat, which can be turned on and off using the standard .on(), .off() and .toggle() functions along with the familiar GPIO Zero blink() and pulse() functions.

         

         

            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()
        

        Using the buttons

        There are two push to make buttons on the Jam Hat which can be assigned to make the lights and buzzer turn on and off or start a sequence of lights or buzzes.

         

         

            from gpiozero import JamHat
            
            jamhat = JamHat()
            
            jamhat.button_1.when_pressed = jamhat.on
            jamhat.button_2.when_pressed = jamhat.off
        

        Making the Buzzer play a tune

        The buzzer on the Jam Hat is capable of playing different tones. You can either give the Jam Hat keyboard notes like 'C4', midi notes like '70' or a hertz value like '220.0'

         

         

            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()
        

        Using the lights, buttons and buzzer together

        Using an 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()
        

        Further Examples

        There are more interesting examples available on our 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!

         

         

         

        Leave a comment

        All comments are moderated before being published.

        This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.