RaspberryPi GPIO Tutorial: LEDs and buttons

From Catrobat
Revision as of 17:33, 19 August 2016 by Administrator (Talk | contribs)

Jump to: navigation, search

Link to this page: catrob.at/RaspberryPi

First steps with Raspberry Pi in Pocket Code: create blinking LEDs, use buttons as input

What you will learn:

  • set up your Raspberry Pi
  • control LEDs/outputs from Pocket Code
  • use buttons/inputs from a Raspberry Pi in Pocket Code

If you encounter any bugs, please let us know and file a report. Thank you!

Install RaspberinoServer on your Raspberry Pi

For the following steps, you either have to enter the following commands in the terminal of your Raspberry Pi using a keyboard and a display, or you can connect to your Raspberry Pi via SSH from your computer or phone.

  1. Download the installer
    wget http://catrob.at/installraspberino -O install-raspberino.sh
  2. Run the installer as root
    sudo sh install-raspberino.sh
    Enter "y" to start the installation
    The installer downloads the server and installs everything. From now on the RaspberinoServer should be ready to use and start automatically when the Raspberry Pi is powered on.
To update the server, just run the installer again (Step 2).

Enable and Configure Raspberry Pi in Pocket Code

If you haven't already done so, you'll have to enable and configure your Raspberry Pi under Settings in "Raspberry Pi bricks". Otherwise you can skip this section and move straight to the tutorials.

  1. tick the check-box to enable Raspberry Pi bricks
  2. change the settings if necessary:
    1. enter IP-/Host-Address
      1. very often "raspberrypi" works as default host address
      2. if your RPi has a keyboard + monitor: find out the IP-address from a terminal via ifconfig
      3. your router might list all connected computers within the network
      4. if all of that fails: find out the IP-address via nmap
    2. Port
      1. the default is 10000
        (don't change it unless you changed the script on the Raspberry Pi)
    3. GPIO version
      1. select the Raspberry Pi model you are using.

Device-2016-02-02-120459.png Device-2016-02-02-120547.png

Now you can add Raspberry Pi bricks in your Projects. Smile.png

The Raspberry Pi Bricks and Sensor in a Nutshell

This section shortly explains the Raspberry Pi bricks.

Tutorial 1: Blinking LED (Hello World)

Tutorial 2: Dimming a LED (PWM)

Tutorial 3: read input values

This tutorial shows how to read the pin value of a Raspberry Pi. When starting the following example, your phone speaks either the value "1" or "0", depending on the button state

Step1: connect the circuit


Step2: create the Program in Pocket Code

300px 2s.png 3s.png

  1. add a new "Speak" brick
  2. click into the speak text to open the formula editor
  3. delete the text and click "Device"
  4. scroll down to the RASPBERRY PI section and choose the raspberry_pi_pin() sensor
  5. select the pin number where you wired the button. (Here pin 38)

Another method to read an input value would be to use the Raspberry Pi "If pin .. is set" brick

Tutorial 4: recognize input events (The invisible bird)

In this tutorial, we are going to remix the default Pocket Code program. Whenever a button on the raspberry Pi is pressed, the bird should be invisible, and when the button is released, the bird should be visible again.

What you need:

  • push-button
  • Resistor (10kOhm and more)
  • BreadBoard

Step1: connect the circuit

Input-pulldown.png Input-pullup.png

There are two possible ways how to use a button as input on a Raspberry Pi:

  1. using a pull-down resistor (first image): when the button is pressed, the input value is high (otherwise low).
  2. using a pull-up resistor (second image): when the button is pressed, the input value is low (otherwise high).

You can try out both variants if you want and see what the difference is. Smile.png

Step2: create the program in Pocket Code

B1 new project.png B2 code.png

  1. create new example program
  2. go to Bird -> scripts
  3. add the two "When Raspberry Pi pin changed" bricks as shown in the tutorial

Start the program by pushing the Play button. Try out pushing and releasing the button.In one situation, the bird will be invisible, in the other it will be visible again, depending on if you have built the circuit with a pull-up or pull-down resistor. Smile.png

Further project ideas

Now you should already know how to use the pins of your Pi with Pocket Code. If you are looking for a new project, check out our tutorial for a LED strip light where you set the color with your phone: Raspberry Pi LED stripe demo/tutorial

If you have created your own exciting projects, please let us know! Smile.png

Related articles

RaspberryPi GPIO Tutorial: LEDs and buttons

RaspberryPi LED-Demo HowTo-Tutorial

Arduino LED-Demo How-to-Tutorial