Cambridge Raspberry Jam – CamJam – Cambridge-based …



Welcome to the Pibrella workshop. In this workshop you will be using the Pibrella from Pimoroni () and Cyntech ().

The Pibrella is an addon board for the Raspberry Pi that plugs into the GPIO port of the Pi. The Pibrella comes pre-made with 3 LEDs (Red,Green and Yellow), a button and a simple buzzer. It also has 4 input and outputs that can be used for other projects.

For this workshop you will be using Python on the Raspberry Pi to control the LEDs, buttons and buzzer. You will also be using the official Pimoroni Python library for controlling the Pibrella. It is possible to control the Pibrella without using the official Python library by accessing the GPIO directly in Python. However the official library does a lot of the work for you in setting up and accessing the GPIO ports.

Now turn over the page to start playing with the LEDs on the Pibrella…

Playing with the LED’s

The Pibrella has 3 LEDs on it, Red, Yellow and Green. In this activity you will be learning how to switch the LEDs on and off using Python.

Project 1

In your first Python program for the Pibrella you will be switching the red LED on and off.

Using the GPIO ports on the Raspberry Pi requires that your Python program is run as a superuser. You will be using Idle in this session which does not normally run in this mode. However it is possible to run Idle as a superuser so when it runs your Python code accessing the GPIO there will be no permissions problems (hopefully).

To run Idle in superuser mode you need to open up a terminal window and at the command line prompt type the following:

sudo idle

Once Idle has loaded, go to File and select New Window. This will open a new window for you to type your Python program into.

In this new window type the code from Code 1 box below:

Once you have typed this in press F5 to run your Python program. You will be prompted to save your code. Give your code a filename and save in your home directory. Once done the red LED on the Pibrella will light up for 2 seconds.

Well done on completing your first Python program for the Pibrella.

CHALLENGE: Can you change your program so that it lights up the Green or Amber LEDs instead?

Question: What line would you change in your program to increase or decrease the amount of time that the LED is on?

Project 2

Apart from switching on individual LEDs by name, the Pibrella Python library also allows you to also perform actions on all of the LEDs at once.

You can either edit your existing program or create a new one. So either enter all the Python code in Code 2 box below as a new program or change your existing one to look like it.

Once you have a program that looks like the above, press F5 to run it. When the program runs you should see all of the three LEDs come on at once.

Congratulations you can now turn on all of the LEDs at once.

CHALLENGE: Get the LEDs on the Pibrella to mimic a set of traffic lights

GOING FURTHER: The Pibrella Python library also has commands that you can use to make the LEDs blink, fade and pulse. How could you use these? Look at the Pimoroni GitHub page (link on last page) to find out more.

Playing with the button

In this activity you are going to learn how to use the button on the Pibrella.

Project 3

Create a new window in Idle for this project, and enter the code from Code 3 box below:

In the code above you are using a while loop to keep checking whether the button on the Pibrella has been pressed. A loop allows you to repeat a section of code a number of times. Which you are doing here.

This project is an illustration of “polling”. There are two main disadvantages to “polling”. The first is that while you are checking for something to happen (in this case the pressing of a button) you cannot do anything else.

The other disadvantage is that it is possible to miss the button being pressed.

Press F5 (if you haven’t done so already) and you will see that when you press the button the text changes to Button Pressed and then back.

So now that you have tried “polling” let’s look at turning your program for looking for a button press in to an event driven program! Onto project 4 on page 5.

Project 4

In this project you will create a program that when you press the button makes a noise, turns on the red LED, while counting to 100! To do all this you are going to create an event driven program.

So as usual by now start a new window for your program, and enter the code from the Code 4 box below:

Ok in the program you have just entered you have created a function called button_pressed. This function will get called everytime the button is pressed. ‘Suppose I better explain what a function is. A function allows you to reuse code in multiple places in your program without repeating it everytime. You write it once (as a function) and then call that function each time you want to use that code in your program.

When you press F5 you will notice that the program starts counting from 1 to 100. When you press the button a message appears, the red LED comes on and the buzzer plays an alarm! And the counting continues during all this!

For the counting you are using another type of loop called a for loop.

You are able to do all of the above because the function that was created was attached to the button pressed event. Which means the function is only called when the button is pressed, so your program is free to go off and do other stuff like count to 100.

CHALLENGE: Change the for loop so it counts from 30 to 300.

Playing with the buzzer

In this activity you are going to get the buzzer on the Pibrella to work. Ok I know technically you have already with Project 4.

Project 5

The buzzer on the Pibrella has 3 inbuilt sounds that you can call. These are fail, success and alarm.

Once more create a new window in Idle for this project, and enter the code from the Code 5 box.

Once you have done that press F5 to run and you will hear all three of the inbuilt sounds on the Pibrella.

Remember when using the button to use the stop command to stop the buzzer playing.

So that was easy wasn’t it? Ready to try something a little more complicated?

Project 6

In this project you will play a short musical scale. I’ll apologise now this is the sum of my musical ability, otherwise you would have some fancy tune.

You know what to do now to at the start of a project

In the code above you use a dictionary to define the frequencies of the various notes that can be played. A list is then used to hold the notes that make up the tune, in this example a simple scale.

After that a for loop is used to loop through each of the notes in the tune to play them.

Press F5 to hear the tune being played on the buzzer.

QUESTION: Where else have you used a for loop in this workshop?

CHALLENGE: Create a new tune to be played by your program.

WOW! Congratulations you have completed the workshop and found out how to use the LEDs,button and buzzer on the Pibrella.

Finally one to try at home with the Pibrella and Minecraft. Save this code in the ~/mcpi/api/python folder for it to work. This is a variation of Project 4 that also sends a message to the screen in Minecraft on the Pi.

-----------------------

Figure 1 The Pibrella. Can you label the parts of the Pibrella?

import brings in Python code from other files. In your program you are getting code to access the Pibrella and code that allows you to do stuff with time.

import pibrella

import time

pibrella.light.red.on()

time.sleep(2)

pibrella.light.red.off()

Code 1 Turning the red LED on and off

Code 2 Python program to switch on all of the LEDs at once

import pibrella

import time

pibrella.light.on()

time.sleep(2)

pibrella.light.off()

import pibrella

import time

while True:

if pibrella.button.read():

print("Button pressed")

else:

print("Button not pressed")

time.sleep(0.1)

Code 3 Python code to look for a button press

Code 4 An example event driven program using the button

import pibrella

import time

def button_pressed(pin):

print("You pressed the button!")

pibrella.light.red.on()

pibrella.buzzer.alarm()

time.sleep(1)

pibrella.light.red.off()

pibrella.buzzer.stop()

pibrella.button.pressed(button_pressed)

for x in range(1,100):

print (x);

import pibrella

import time

pibrella.buzzer.fail()

time.sleep(2)

pibrella.buzzer.success()

time.sleep(2)

pibrella.buzzer.alarm()

time.sleep(2)

pibrella.buzzer.stop()

Code 5 Inbuilt buzzer sounds on the Pibrella

Code 6 Playing a scale on the buzzer

import pibrella

import time

notes = {

'c':3830,

'd':3400,

'e':3038,

'f':2864,

'g':2550,

'a':2272,

'b':2028,

'C':1912}

tune = [notes['c'],notes['d'],notes['e'],notes['f'],notes['g']]

for x in tune:

pibrella.buzzer.buzz(x)

time.sleep(0.5)

pibrella.buzzer.stop()

Useful Pibrella Links

The Official Pibrella website is

You can get the official Pimoroni Python library for the Pibrella from GitHub at the following web address .

The Pibrella can also be programmed using ScratchGPIO on the Raspberry Pi. You can find out more about ScratchGPIO here

You can find a digital version of this worksheet and examples of accessing the Pibrella directly via the GPIO on my blog Manic Coding at

import pibrella

import mcpi.minecraft as minecraft

import time

mc = minecraft.Minecraft.create()

def button_pressed(pin):

print("You pressed the button!")

pibrella.light.red.on()

pibrella.buzzer.alarm()

time.sleep(1)

pibrella.light.red.off()

pibrella.buzzer.stop()

msg = "Button Pressed"

mc.postToChat(msg)

pibrella.button.pressed(button_pressed)

for x in range(1,100):

print (x);

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download