Exercises 5: First Exercise on Hardware: LEDs
Goal:
Now that we know how to program the Raspberry Pi we will start to setup our first electronic circuits, albeit extremely simple, and control them through the Raspberry Pi.
All circuits will be built on a bread board which is connected to the Rpi through a “cobbler” which supplies it with power and which gives access to the Rpis I/O pins. The significance of the pins is printed on the cobbler.
A word of warning: The Rpi can provide a maximum of 60 mA of current. Drawing more than that may damage the processor chip and thus kill our poor little Raspberry.
Therefore only wire up your circuit with the Rpi switched off! and triple control your wiring, before switching it on. When unsure, please ask a supervisor! If the Raspberry Pi was on, then shut it down orderly before switching off.
Exercise 1: Testing the LED
As said above, we have to limit the current flowing through the LED, which is done with a 330
Ω resistor as shown in the circuit diagram:
Under the assumption that the resistance of the LED can be neglected compared to the 330 Ω current limiting resistor and Vcc = 3.3V, what is the current flowing through the LED?
Exercise 2: Connect the LED to the GPIO pin
Now that we know the LED works fine, we disconnect (with Raspberry Pi switched off!) the LED from Vcc and connect it to GPIO pin 0 instead. Please check the pin correspondence table between GPIO numbering and physical numbering on the CPU chip.
We have installed the wiringpi package for you, which contains libraries for access to the GPIO pins but also to the SPI and
I2C serial interfaces. This package contains in addition to the library and include files a new command
gpio allowing us to talk to a gpio pin. We will have to define this pin to be output and the write 0 or 1 to it .
Have to look at the gpio man page to figure out how to do this.
Exercise 3: Shell script to make the LED blink
Once you are able to switch a LED on and off, write a shell script to make it permanently blink.
Exercise 4: Write a C program to make the LED blink
In order to translate your blinking program into C you will have to include <wiringPi.h>. Then you can call
wiringPiSetup () to initialize the library. Finally use
pinMode and digitalWrite to tel the library you want to use the pin as output pin and write to it. Check the wiringpi API documentation for the parameters of these calls. When building the program you have to link the wiringPi library to your program (-lwiringPi). The wiringpi include files and the library are stored in /usr/local/include and /usr/local/lib respectively.
Write a Makefile to build your project. Test it to make sure it works as expected.
Exercise 5: Implementing your own gpio command
Knowing how to switch on and off the LED and knowing that you can read and write a gpio pin, can you write your own
mygpio command? This command will take 2 or 3 command line parameters:
- the strings "read" of "write"
- the gpio pin number (0..7)
- the logic level to be written (0 or 1) only in case of writing
Implement this command and make sure it is water tight (the user can type any rubbish and the program will react correspondingly but does not crash). Check if you can write a LED. Put the logic level at the LED onto a second GPIO pin and make sure you can read it back.
Exercise 6: Make your LED blink SOS
Write a C program that makes the LED blink an SOS. Write a subroutine pulse (int pulseLength) that switches the LED on for pulseLength ms and then off again for pulseLength ms. Use this routine to implement the SOS. Wait for 1 s between each SOS sequence.
--
Uli Raich - 2017-01-03
Comments