The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python. It’s capable of doing everything you’d expect a desktop computer to do, from browsing the internet and playing high-definition video, to making spreadsheets, word-processing, and playing games.
What’s more, the Raspberry Pi has the ability to interact with the outside world, and has been used in a wide array of digital maker projects, from music machines and parent detectors to weather stations and tweeting birdhouses with infra-red cameras. We want to see the Raspberry Pi being used by kids all over the world to learn to program and understand how computers work.
0.0 – THE HARDWARE
1.0 – THE SOFTWARE
1.1 – CHOOSING THE OPERATIVE SYSTEM
First, you need to go to the Raspberry download’s page and choose the OS flavour you’d like to use. If you’re a noobie we’d recommend you the Raspbpian OS.
Download it to your computer.
1.1 – FLASHING THE SD CARD
Now insert the SD Card that came with your Raspberry Pi onto your computer and open Disks.
Return the card to its factory condition by:
- removing all the partitions and
- formatting a new full size partition to FAT format
If you take a look at the Device label, your SD card will be listed as something like
/dev/sdd1. This location information will be needed for flashing the SD Card with the Raspberry OS, so memorize it, except for the last part (
1 respectively) which is the partition number. The reason you’d want to forget it it’s because you want to flash the OS image to the whole SD card, not just one partition.
Now that you are ready, use the following command to flash the card:
sudo dd bs=4M if=/location/to/your/image/raspberrypi-os-flavour.img of=/dev/mmcblk0
Don’t forget to change the
if variable with the current location of your image file and
of with the correct SD Card file system location.
If you want to check the installation progress run
pkill -USR1 -n -x dd on another terminal window (Ctrl+Shift+T)
After the installation is complete you are ready to run your OS on the Raspberry pi.
2.0 – RUNNING
2.1 – GPIO PORTS
GPIO is the General Purpose Input / Output system that enables the Pi to interact with the physical world. The GPIO pins are the bank of 40 pins (or 26 in case you have a RaspberryPi model A or B) that runs along the side of the Pi. On these pins you can connect all sorts of electronic components like LED’s, motors, connect buttons, sensors and control a lot of stuff like a car, an irrigation system, surveillance camera, open and close doors, etc. You’re only limited by your own imagination! 🙂
This is a short tutorial on how to use the GPIO on RaspberryPi, using Scratch and Python.
2.1.1 – REQUIREMENTS
Before going through this tutorial make sure you have:
- A working RaspberryPi
- A breadboard
- 1 LED
- 1 330Ω resistor
- 1 push button
- Female-Male jumper wires (to connect the GPIO pins to the breadboard)
2.1.2 – PINOUT CORRESPONDENCE
On the image below you can find the pins layout. Inside the circles there’s a number that corresponds to the physical pin, however these are not the ones you’ll use inside your scripts. The labels on the side are the ones you’re going to use.
As an example, if you want to use pin 7, what you’ll be using is the GPIO4 label inside your application.
2.1.3 – THE CIRCUIT
Before starting programming with the GPIO and some LED’s, let’s make a simple test just to make sure that everything is working properly.
With your RaspberryPi on, connect an LED to the GPIO pins with a resistor in between just like shown on the picture below. In this case we’re using pin 1 (3.3 Volts) and pin 6 (Ground).
After completing this circuit the LED should light up.
GPIO with Scratch
Let’s open Scratch! You can find it under Menu > Programming > Scratch. The Scratch window can be split into the following areas:
- Grid output
- Stages and sprites
In the following examples you’ll only be working with the areas 1 and 2.
The first step inside Scratch is to start the GPIO server so we can make changes to it. Go to Edit > Start GPIO server.
Now you can start using the broadcast blocks inside Control group to change the state of the GPIO pins. These are the messages that the GPIO server understands:
- configures pin X as an ouput (e.g. config17output sets the GPIO17 as an ouput)
- configures pin Y as an input
- switch on the pin X (e.g. gpio17on switches on the GPIO17 pin)
- switch off the pin X
- reads the value of pin Y (e.g. gpio4 reads the state of the GPIO4 pin)
Note: gpioXon and gpioXoff messages only work if the pin X is configured as an output. Likewise, the gpioY only works if the Y pin is configured as an input.
Using the blocks from Control group create the following script:
After that, click on the green flag on the top right corner to set your script in action, and you should see your LED blinking with 1 second interval. Don’t forget to change the wire that was connected to the pin 1 of GPIO and change it to the right pin. In this example we’re using GPIO17 which corresponds to the pin 11.
Let’s see now how we can work with both an output and an input. First, let’s add the push button to the circuit:
In the following example we are going to insert a conditional block (using an if-else statement) that will only light up the LED if the button is pressed. This requires the use of blocks from Control, Operators and Sensing groups.
But before using a sensing block (in this case we’ll be using the sensor value block), you need to configure your input pin and run the script once to assign it to the blocks. This is what you have to do:
After this, on the sensor value block, you’ll be able to choose the pin and check its value. Here’s the script that will lid up the LED when you press the button:
GPIO with Python
Let’s do the last script you did with Scratch but this time using Python.
Open the Python 3 IDE under Menu > Programming > Python 3 and on the shell window click on File > New File. This will open a new window and here is where will insert your code, but first save the file as something suitable like raspi_gpio.py.
After that insert the code below:
from gpiozero import LED, Button led = LED(17) button = Button(24) while True: if button.is_pressed: led.on() else: led.off()
Basically what you’re doing here is importing two classes (LED and Button) from the module gpiozero. Both receive one argument that corresponds to the GPIO label number, i.e. LED(17) will assign to the variable led a way to interact with the GPIO17 pin, and configure it as an output. The same happens for the button, but in this case the pin will be configured as an input.
Inside the while loop we’re checking the button is_pressed attribute, that returns a boolean value, and everytime this attribute is true it switches the LED on. Otherwise, turns it off.
Now save your file and run it by pressing F5 or selecting from the menu Run > Run Module and check that the LED only lights up if you click the button. To stop the script, on the shell window press Ctrl + F6 or select from the menu Shell > Restart Shell.
Instead of using a while loop you can use event handlers to achieve the same goal. In the following script we set the action led.on() and led.off to the button’s events when_pressed and when_released, respectively.
from gpiozero import LED, Button led = LED(17) button = Button(24) button.when_pressed = led.on button.when_released = led.off
In case you’re interested in the other attributes and functions available inside the gpiozero module you can check the documentation.
That’s it! If you have any question or if you would like to see more about GPIO please leave a comment.JOIN US