In the previous post, we looked at accessing the Raspberry Pi Compute Module via another computer through SSH. The Pi can do a gazillion things. It depends on how imaginative and creative you are. For now, I want to show you something simple; lighting up three LEDs through the Pi using the GPIO (General Purpose Input Output). Since the Raspberry Pi Compute module is a bootstrapped version of the normal Raspberry Pi, we will use the CMIO’s GPIO pins.
For further explanation on GPIO, read this article.
What’s in the Cooking Pot?
Basically, we’re sending two signals to the 3 LED to go on and off 10 times in a sequence. What do I need to achieve this?
- Three LEDs
- 3 resistors of 330 ohms
- Raspberry Pi compute module
- Compute module input output board
- USB cable
- Ethernet cable
- USB-Ethernet adapter (if your computer does not have an Ethernet port)
I first setup a circuit to test if the LED will work with the Raspbery Pi when connected to GPIOs pins for 3.3V and 0V. This is just a basic circuit and no programming involved:
The final setup should look like this:
Now that the setup is complete, we’ll need to write the code that will light up the lights.
In summary the code does the following:
- Export GPIO pin to the file system (lines 17,21,25); a new directory will appear in the file system that corresponds to the pin number
- Give the I/O direction that the GPIO will respond to when activated (lines 18,22,26). In this case its output i.e. the pin becomes an output entity
- Sets the pin value high (3.3V) or low (0V) by echoing one or zero (lines 36 – 56). This is a digital logic system hence high and low do not correspond to 1 or 0 binary values respectively; they allow for current flow in the pins
- Light up the LEDs one after the other 9 times using a while loop
Of importance, the script should be run as root for it to execute.
Brian Birir May 27, 2016
Posted In: Information Technology
Tags: raspberry pi