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.





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.


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

disks_raspberry piIf you take a look at the Device label, your SD card will be listed as something like /dev/mmcblk0p1 or /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 (p1 or 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.



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.


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)

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.


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:

  1. Blocks
  2. Scripts
  3. Grid output
  4. Stages and sprites

2016-03-28-175352_1920x1080_scrot copy

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:

  • configXoutput
    • configures pin X as an ouput (e.g. config17output sets the GPIO17 as an ouput)
  • configYinput
    • configures pin Y as an input
  • gpioXon
    • switch on the pin X (e.g. gpio17on switches on the GPIO17 pin)
  • gpioXoff
    • switch off the pin X
  • gpioY
    • 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 ControlOperators 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

After that insert the code below:


from gpiozero import LED, Button

led = LED(17)
button = Button(24)

while True:
    if button.is_pressed:


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 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 =


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.





Leave a reply


We're not around right now. But you can send us an email and we'll get back to you, asap.

©2017 KLEO Template a premium and multipurpose theme from Seventh Queen


Log in with your credentials


Forgot your details?


Create Account