Difference between revisions of "RaspberryPi GPIO Tutorial: LEDs and buttons"

From Catrobat
Jump to: navigation, search
 
(35 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Guides|Back to the Guideoverview]]
 +
 +
----
 
Link to this page: [http://www.catrob.at/RaspberryPi catrob.at/RaspberryPi]
 
Link to this page: [http://www.catrob.at/RaspberryPi catrob.at/RaspberryPi]
  
Line 14: Line 17:
 
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 [https://www.raspberrypi.org/documentation/remote-access/ssh/ connect to your Raspberry Pi via SSH] from your computer or phone.
 
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 [https://www.raspberrypi.org/documentation/remote-access/ssh/ connect to your Raspberry Pi via SSH] from your computer or phone.
 
# Download the installer
 
# Download the installer
#* <code>wget http://catrob.at/installraspberino -O install-raspberino.sh</code>
+
#: <code>wget http://catrob.at/installraspberino -O install-raspberino.sh</code>
# Unzip the archive using the "unzip" command in the Raspberry Pi.
+
# Run the installer as root
# Give execute permissions to the "installer" file with "chmod +x installer"
+
#: <code>sudo sh install-raspberino.sh</code>
# Run the installer as root "sudo ./installer".
+
#: Enter "y" to start the installation
## Optional: If this step fails, give execute permissions to the "compile.sh" file, install the necessary dependancies (sudo apt-get install libncurses5-dev libcurl4-nss-dev) and run the compile file./compile.sh.
+
#: 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.
## Now try to run "sudo ./installer" again. If this does not work either, please feel free to contact us!
+
 
# Press Enter to select "Jetzt installieren!"
+
: To update the server, just run the installer again (Step 2).
# Optional: Enter a port number (Default is 10000)
+
 
# Optional: Enter a username that the server will run on (Default is pi)
+
== Enable and Configure Raspberry Pi in Pocket Code ==
# The installer downloads the Server code, sets up an init.d script and creates necessary config files automatically.
+
 
# Start the server with "sudo /etc/init.d/RaspberInoServer start"
+
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.
# Check the status of the server with "sudo /etc/init.d/RaspberInoServer status". If it's running, everything worked fine. The Server will now start everytime you start the Raspberry Pi,
+
# '''tick''' the check-box to enable Raspberry Pi bricks
# Optional: Run the installer again and select "Update" from time to time. We will always improve our Server application andw ant you to benefit from it!
+
# change the settings if necessary:
 +
## <u>enter IP-/Host-Address</u>
 +
### very often "'''raspberrypi'''" works as default host address
 +
### if your RPi has a keyboard + monitor: find out the IP-address from a terminal [https://learn.adafruit.com/adafruits-raspberry-pi-lesson-3-network-setup/finding-your-pis-ip-address via ifconfig]
 +
### your router might list all connected computers within the network
 +
### if all of that fails: find out the IP-address [http://raspberrypi.stackexchange.com/questions/13936/find-raspberry-pi-address-on-local-network via nmap]
 +
## <u>Port</u>
 +
### the default is '''10000'''
 +
###:(don't change it unless you changed the script on the Raspberry Pi)
 +
## <u>GPIO version</u>
 +
### select the Raspberry Pi model you are using.
 +
 
 +
 
 +
[[File:Device-2016-02-02-120459.png|border|300px]] [[File:Device-2016-02-02-120547.png|border|300px]]
 +
 
 +
Now you can add Raspberry Pi bricks in your Projects. [[File:Smile.png]]
 +
 
 +
== The Raspberry Pi Bricks and Sensor in a Nutshell ==
 +
This section shortly explains the Raspberry Pi bricks.
 +
 
 +
'''Pin-Numbers:'''
 +
 
 +
Your Raspberry Pi offers a huge number of GPIO (general purpose input/output) pins. The number of pins available depends on your Raspberry Pi version. Pocket Code uses the pin numbering of the board layout (the top left pin in pin 1). Note that not all pin numbers correspond to a GPIO pin.
 +
 
 +
To find out more about the available pins and their corresponding numbers, have a look here: [https://pinout.xyz/ Raspberry Pi Pinout]
 +
 
 +
'''Bricks:'''
 +
 
 +
[[File:RaspberryPiBricks.png|left|210px]]<u>When Raspberry Pi pin <pin> is True/False:</u>
 +
 
 +
:: *  this is a script-brick, meaning that is the first one (no predecessor)
 +
:: *  the bricks placed under such a script-brick get executed, whenever the value of a  given input-pin changes to True/False
 +
 
 +
<u>Set Raspberry Pi pin <pin> to <value>:</u>
 +
 
 +
:: *  sets the output of a given <pin> to a given <value>
 +
:: *  allowed values: 0 or 1 (otherwise the brick has no effect)
 +
 
 +
<u>Set Raspberry Pi PWM pin <pin> to <duty_cycle> %, <percentage> Hz:</u>
 +
 
 +
:: *  PWM (pulse width modulation) can be used to obtain an analogue behaviour with a digital output
 +
:: *  this can be used to dim LEDs, control servos, etc.
 +
:: *  here you can find an [https://www.arduino.cc/en/Tutorial/PWM explanation about how PWM works].
 +
:: *  see also our [http://catrob.at/RaspberryPiLEDStripe Raspberry Pi LED stripe demo/tutorial]
 +
 
 +
<u>If Raspberry Pi pin <pin> is true, then:</u>
 +
 
 +
:: *  this brick works similar to the standard If - Then - else brick
 +
:: *  if the <pin> input is 1 (aka True or High), the <u>then-part</u> is executed, otherwise the <u>else-part</u> is executed
 +
 
 +
 
 +
 
 +
'''Sensor:'''
 +
 
 +
[[File:Screenshot 2016-08-12-21-01-48.jpg|left|210px]]<u>The raspberry_pi_pin( <pin> ) sensor:</u>
 +
 
 +
is another way besides the <u>If Raspberry Pi pin</u>-Brick to use digital inputs.
 +
 
 +
:: *  can be used to retrieve the input value of a given <pin>
 +
:: *  available in the <u>Formula Editor</u> via the <u>Device</u> button
 +
:: *  the resulting input value is either 0 or 1
 +
::: **  (for invalid pin numbers the returned value is 0)
 +
 
 +
 +
<<<< The example sets the GPIO-output pin 3 to the value of the GPIO-input pin 40.
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
  
== Preparation 2: Enable and Configure RaspberryPi in Pocket Code ==
 
text
 
  
  
== The RaspberryPi Bricks and Sensor in a Nutshell ==
 
test
 
  
[[File:Device-2016-02-02-120459.png|border|200px]]
 
  
 
== Tutorial 1: Blinking LED (Hello World) ==
 
== Tutorial 1: Blinking LED (Hello World) ==
 +
Everyone starts with a simple Hello World program when learning a new programming language. When using a Raspberry Pi, "Hello World" simply means a blinking LED.
 +
===Step1: connect the circuit===
 +
 +
What you need:
 +
 +
* LED
 +
* Resistor (around 220 - 330 Ohm)
 +
* Bread board + wires
 +
 +
The LED typically has a longer and a shorter leg. The shorter leg has to be connected to GND on a Raspberry Pi (black wire).
 +
 +
The other end of the leg is connected via a resistor (here 330 Ohm) to a GPIO pin (here: pin 40 using the yellow wire).
 +
 +
Note that the holes in one line (marked green) are linked together.
 +
 +
[[File:LED.png|border|300px]]
 +
 +
===Step2: Create your first Program using Raspberry Pi bricks in Pocket Code===
 +
[[File:1_new_project.png|border|300px]] [[File:2_background.png|border|300px]] [[File:3_code.png|border|300px]]
 +
 +
# create new '''empty program'''
 +
# go to Background -> Scripts
 +
# place the code bricks as in the image
 +
 +
Finally, press the '''Play''' button. If your code is correct and everything is wired correctly, your LED should blink every second. Well done! [[File:Smile.png]]
 +
 +
== Tutorial 2: Dimming a LED (PWM) ==
 +
The previous tutorial turned a LED on and off periodically. If you have done the previous tutorial, you can reuse the circuit for this tutorial.
 +
 +
PWM (pulse width modulation) can be used to obtain an analogue behavior with a digital output. This can be used to dim LEDs. If you want to know more about PWM, you can find an [https://www.arduino.cc/en/Tutorial/PWM explanation about how PWM works here].
 +
 +
===Step1: connect the circuit===
 +
the same as in Tutorial 1 (blinking light).
 +
 +
[[File:LED.png|border|300px]]
 +
 +
===Step2: Create your first Program using Raspberry Pi bricks in Pocket Code===
 +
[[File:1_new_project.png|border|300px]] [[File:2_background.png|border|300px]] [[File:Pwm.png|border|300px]]
 +
 +
#create new '''empty program'''
 +
#go to Background -> Scripts
 +
#place the code bricks as in the image
 +
 +
 +
 +
Now start the program by pressing '''Play'''. You can try out different percentages (instead of 50%).
 +
 +
The Raspberry Pi dims the led by switching on and off the LED very fast. A higher percentage means that the time "on" is longer, whereas a lower percentage means that the LED is "off" most of the time. If this happens very fast (here the frequency is 100Hz, meaning that the LED is turned on and off 100 times per second), then the LED is dimmed.
 +
 +
If you set the PWM pin to 50% and 0.5 Hz, you'll get the same result as with the code in Tutorial 1  [[File:Smile.png]]
 +
 +
== 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===
 +
[[File:Input-pulldown.png|border|300px]]
 +
 +
===Step2: create the Program in Pocket Code===
 +
[[File:1_s.png|border|300px]] [[File:2s.png|border|300px]] [[File:3s.png|border|300px]]
 +
 +
 +
#add a new "Speak" brick
 +
#click into the speak text to open the formula editor
 +
#delete the text and click "Device"
 +
#scroll down to the RASPBERRY PI section and choose the raspberry_pi_pin() sensor
 +
#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===
 +
[[File:Input-pulldown.png|border|300px]] [[File:Input-pullup.png|border|300px]]
 +
 +
There are two possible ways how to use a button as input on a Raspberry Pi:
 +
 +
#using a pull-down resistor (first image): when the button is pressed, the input value is high (otherwise low).
 +
#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. [[File:Smile.png]]
 +
 +
 +
===Step2: create the program in Pocket Code===
 +
[[File:B1_new_project.png|border|300px]] [[File:B2_code.png|border|300px]]
 +
 +
#create new '''example program'''
 +
#go to Bird -> scripts
 +
#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. [[File: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: [http://catrob.at/RaspberryPiLEDStripe Raspberry Pi LED stripe demo/tutorial]
 +
 +
If you have created your own exciting projects, please let us know! [[File:Smile.png]]
 +
== Related articles ==
 +
[https://confluence.catrob.at/display/CATARDUINO/RaspberryPi+GPIO+Tutorial%3A+LEDs+and+buttons RaspberryPi GPIO Tutorial: LEDs and buttons]
 +
 +
[https://confluence.catrob.at/display/CATARDUINO/RaspberryPi+LED-Demo+HowTo-Tutorial RaspberryPi LED-Demo HowTo-Tutorial]
 +
 +
[https://confluence.catrob.at/display/CATARDUINO/Arduino+LED-Demo+How-to-Tutorial Arduino LED-Demo How-to-Tutorial]

Latest revision as of 18:55, 19 August 2016

Back to the Guideoverview


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.

Pin-Numbers:

Your Raspberry Pi offers a huge number of GPIO (general purpose input/output) pins. The number of pins available depends on your Raspberry Pi version. Pocket Code uses the pin numbering of the board layout (the top left pin in pin 1). Note that not all pin numbers correspond to a GPIO pin.

To find out more about the available pins and their corresponding numbers, have a look here: Raspberry Pi Pinout

Bricks:

RaspberryPiBricks.png
When Raspberry Pi pin <pin> is True/False:
* this is a script-brick, meaning that is the first one (no predecessor)
* the bricks placed under such a script-brick get executed, whenever the value of a given input-pin changes to True/False

Set Raspberry Pi pin <pin> to <value>:

* sets the output of a given <pin> to a given <value>
* allowed values: 0 or 1 (otherwise the brick has no effect)

Set Raspberry Pi PWM pin <pin> to <duty_cycle> %, <percentage> Hz:

* PWM (pulse width modulation) can be used to obtain an analogue behaviour with a digital output
* this can be used to dim LEDs, control servos, etc.
* here you can find an explanation about how PWM works.
* see also our Raspberry Pi LED stripe demo/tutorial

If Raspberry Pi pin <pin> is true, then:

* this brick works similar to the standard If - Then - else brick
* if the <pin> input is 1 (aka True or High), the then-part is executed, otherwise the else-part is executed


Sensor:

Screenshot 2016-08-12-21-01-48.jpg
The raspberry_pi_pin( <pin> ) sensor:

is another way besides the If Raspberry Pi pin-Brick to use digital inputs.

* can be used to retrieve the input value of a given <pin>
* available in the Formula Editor via the Device button
* the resulting input value is either 0 or 1
** (for invalid pin numbers the returned value is 0)


<<<< The example sets the GPIO-output pin 3 to the value of the GPIO-input pin 40.







Tutorial 1: Blinking LED (Hello World)

Everyone starts with a simple Hello World program when learning a new programming language. When using a Raspberry Pi, "Hello World" simply means a blinking LED.

Step1: connect the circuit

What you need:

  • LED
  • Resistor (around 220 - 330 Ohm)
  • Bread board + wires

The LED typically has a longer and a shorter leg. The shorter leg has to be connected to GND on a Raspberry Pi (black wire).

The other end of the leg is connected via a resistor (here 330 Ohm) to a GPIO pin (here: pin 40 using the yellow wire).

Note that the holes in one line (marked green) are linked together.

LED.png

Step2: Create your first Program using Raspberry Pi bricks in Pocket Code

1 new project.png 2 background.png 3 code.png

  1. create new empty program
  2. go to Background -> Scripts
  3. place the code bricks as in the image

Finally, press the Play button. If your code is correct and everything is wired correctly, your LED should blink every second. Well done! Smile.png

Tutorial 2: Dimming a LED (PWM)

The previous tutorial turned a LED on and off periodically. If you have done the previous tutorial, you can reuse the circuit for this tutorial.

PWM (pulse width modulation) can be used to obtain an analogue behavior with a digital output. This can be used to dim LEDs. If you want to know more about PWM, you can find an explanation about how PWM works here.

Step1: connect the circuit

the same as in Tutorial 1 (blinking light).

LED.png

Step2: Create your first Program using Raspberry Pi bricks in Pocket Code

1 new project.png 2 background.png Pwm.png

  1. create new empty program
  2. go to Background -> Scripts
  3. place the code bricks as in the image


Now start the program by pressing Play. You can try out different percentages (instead of 50%).

The Raspberry Pi dims the led by switching on and off the LED very fast. A higher percentage means that the time "on" is longer, whereas a lower percentage means that the LED is "off" most of the time. If this happens very fast (here the frequency is 100Hz, meaning that the LED is turned on and off 100 times per second), then the LED is dimmed.

If you set the PWM pin to 50% and 0.5 Hz, you'll get the same result as with the code in Tutorial 1 Smile.png

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

Input-pulldown.png

Step2: create the Program in Pocket Code

1 s.png 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