deutsch     english    français     Print

 

11.7 RASPBERRY PI

 

 

INTRODUCTION

 
You can use TigerJython on the Raspberry Pi in order to learn the Python programming language or in order to access its sound system and GPIO port. Although TigerJython starts slightly slower than the pre-installed Python with IDLE, you will have a more sophisticated graphical development environment with many library routines already integrated (turtle graphics, robotics, game development, etc.) at your selection.
 

 

 

INSTALLATION

 

The easiest way to get started is downloading the operating system installer NOOS from  http://www.raspberrypi.org/downloads copying the content to a SD card (a minimum of 8 GB), and choosing the operating system Raspbian (a Linux Debian variant) when starting up. Since the distribution already includes a JRE, you will only need to copy tigerjython2.jar into a directory, for example /home/pi/tigerjython, and give the file execution rights with the file manager (under File properties).

To start TigerJython, type the following command into a terminal (console):

java -jar /home/pi/tigerjython/tigerjython2.jar

In order to use the GPIO module, TigerJython requires administrator rights. That is why you should always start TigerJython with a preceding sudo:

sudo java -jar /home/pi/tigerjython/tigerjython2.jar

Instead of typing this command each and every time, you can specify in the file manager that files with the file type .jar are always executed using this command. You can also create a shell script. To make it even easier, you can make a desktop link proceeding as follows:

  • Right click and copy the icon IDLE and then paste it onto the desktop, creating a new link icon.

  • In order to edit the associated link script, right click on this icon and choose Leafpad. You can now adjust the entries accordingly and even specify a TigerJython logo (download). Here is an example:

After saving, you can start TigerJython by clicking on the new icon. You will need a little bit of patience until the IDE has started on the Raspberry Pi (about one minute). Fortunately, as you will notice, running Python programs is surprisingly fast. We recommend to use a fast SD card (class 10) and the Raspberry Pi 2 Model B.

 

 

DIGITAL INPUT/OUTPUT USING THE GPIO PORT

 

Raspberry Pi provides you with 17 digital input/output channels that can be tapped on a 26-pin connector (new version 40-pin also supports). Each channel can be defined as an output or an input with either an internal pull-up resistor, a pull-down resistor, or without a resistor. There are also 5V, 3.3V, and ground pins available on the connectors. The input voltage must never exceed 3.3V, so you should not connect external 5V-logic outputs directly to the inputs.

In TigerJython, you will find the class GPIO in the module RPi_GPIO, using which you can easily address the IO ports. The module uses the library Pi4J by Robert Savage, but corresponds to the module RPi.GPIO. All the necessary files can be downloaded from here and copied into the subdirectory Lib of the directory where tigerjython2.jar is located.

By default, the pins of the GPIO ports are used with the pin numbers 1..26. Each pin can be defined with GPIO.setup() as input or output channel. With GPIO.output(channel, state) can you set an output value. With GPIO.input(channel) can you read und return the current input value. You find the detailed documentation under the menu option Help > APLU Documentation.

In order to test it, you simply connect an LED to a series resistance between pin 6 (ground) and pin 12, whereby you have to try out the polarity of the LED (it is not destroyed due to an incorrect polarity). If you have a key switch available, you can connect it between pin 6 (ground) and pin 26.

In your program you first define channel 12 as an output and make the LED blink 10 times per second:

from RPi_GPIO import GPIO 

GPIO.setup(12, GPIO.OUT)
  
while True:
    GPIO.output(12, 1)
    GPIO.delay(100)
    GPIO.output(12, 0)
    GPIO.delay(100)
 

For the demonstration of an input port you connect a key switch at pin 26. Typically you will need a few flags, so you can turn on the blinking by pressing the button and turn it off by pressing it again.

Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

In the loop you first get the current state of the key switch (you "poll" it). When pressed, it connects GND to the input on pin 26, therefore input(26) returns GPIO LOW or 0. When you release the button, it causes the internal pull-up resistor to set the input to logic HIGH (3.3V), without needing to create the voltage from the outside.

The use of the key switch as an on/off switch is a little tricky, because despite the constant polling you have to convert the turning on/off into an event that only occurs when the switch goes down. For this you use a flag buttonPressed, which you set to True upon the first key pressing. After that you do not walk through the respective part of the code again, until you have released the switch and pressed it again.

Since the loop is also responsible for the flashing, you use a flag blinking, that represents the on/off status. Finally, you have to remember with the flag ledOn, whether in a specific iteration of the loop the LED should be turned on or off.

Because you are not sure if the LED is lit up at the moment of switching off, you switch it off in the last else. It is a small imperfection that after that, the program will still always run through that code.

An electronic engineer knows that when a key is pressed it does not usually make immediate contact (it "bounces"). However, since the loop is only repeated after 100 ms due to the delay(100), we can assume that the "bouncing" will have ended.

 

 

USING EVENTS

 

It is much easier to use the event model. The pressing and releasing the button is here regarded as an event that calls automatically a function onButtonPressed(). There, you only need the flag blinking reverse.

Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

To use events, you must specify with the method add_event_detect(), whether you want to respond to the transition from low to high or high to low or both. After that you register with add_event_callback() a function to be called when the event occurs.