This is an old revision of the document!
PROJECT: Light Emitting Diodes (led0)
To create a program that generates a unique pattern of lit LEDs on your pi based on constraints
This document is written with TWO locations in mind:
There are commands you can ONLY run on one system or the other. Pay attention to any prompt cues in the given examples (or section headings, context of language leading up to any examples).
For example:
Please pay attention to your prompt, so you can perform the needed activity on the correct system.
Please be sure to familiarize yourself with the following content in “the C book”:
For this project, you will be writing a C program using the wiringPi library on the Raspberry Pi, wiring up a red LED to your breadboard and witnessing your ability to control it via software.
One of the intended uses of the Raspberry Pi and other small, single board computers is as an interface tool to peripherals in projects, personal and industrial.
The pi has a set of General Purpose Input Output (GPIO) pins intended for precisely this purpose:
Please note the orientation of the pi (ethernet/USB at bottom) to calibrate yourself to the location of pin 1 and all subsequent pins, along with their identified function (ie top left pin, pin 1, provides a constant 3.3v DC power)
We can also get a live update on the state of each pin on our pi itself, using the 'gpio readall' command at our pi prompt:
yourpi:~/src/c4eng/led0$ gpio readall +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5v | | | | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 0 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+ yourpi:~/src/c4eng/led0$
Yes, it may be packed with information, but it is an informative, technical reference.
Why is this so layered and directed, you may ask? Well, it is a matter of competing standards (approaches to identification of each pin, based on particular reference points).
For example, the “Physical” columns (dead center) in the center conform to the image diagram I posted above of the 40 pins (where pin 1 is the top-left).
The “BCM” columns (far left and far right) refer to the hardware identifications for each pin by the manufacturer. You might notice that BCM pin 17 (physical pin 11) corresponds to wPi pin 0.
And the “wPi” columns (second from left, and second from right), correspond to the pin identifications as use by the wiringPi library, which we are using here.
Additionally, we see “Mode” and “V” columns; mode informs us of the operating mode of that pin at present configuration (IN means it is configured for INPUT, OUT means it is configured for OUTPUT). V implies voltage on the pin (0 means no voltage, 1 means there is voltage present at time of checking). For this project, we will be configuring a specific pin to OUTPUT mode, and modulating it between a state of ON (1) and OFF (0).
So, in exploring the use of the table: if we wanted to hook a component up to wiringPi pin #0, that corresponds to manufacturer (BCM) pin 17, which is physical pin 11.
You will want to verify placement before supplying power, that is why we are taking things slow, and providing you opportunities to confirm (by posting pictures in the discord channel) before proceeding.
It may seem a bit bewildering or overwhelming at first, but like anything, time and exposure will ensure it becomes increasingly second nature.
Here we are, about to embark on our first foray into mixing with hardware and software.
In this project, we will be hooking up a peripheral to a couple of our Raspberry Pi pins: a light emitting diode (LED) and supporting hardware, and then proceed to interface with it to control its state (of being ON or OFF).
You have likely encountered LEDs endlessly in your everyday life: commonly used as a “power light”, which when lit, indicates the device is powered on, among many, MANY, MANY other applications.
For this endeavour, you will need to obtain the following items out of the electronics kit you obtained for the class:
It is your task to write a program to interface with an LED (light emitting diode), a nice software-hardware connection, on your raspberry pi.
Develop, test, and run this program on your pi. When done, submit it on lab46.
To utilize the needed functionality for this project, you will need to ensure you have the following packages installed:
An exception to the usual package installation process, especially for those with a Raspberry Pi model 4B: when you install wiringpi the usual way, we may end up with errors when proceeding further, such as the following:
yourpi:~/src/c4eng/led0$ gpio readall Oops - unable to determine board type... model: 17
What you will need to do is manually install an updated (and out of database) version of the wiringpi package, as follows (looking out for errors along the way):
yourpi:~/src/c4eng/led0$ wget https://project-downloads.drogon.net/wiringpi-latest.deb ... yourpi:~/src/c4eng/led0$ sudo dpkg -i wiringpi-latest.deb ... yourpi:~/src/c4eng/led0$ rm -f wiringpi-latest.deb
The program you will need is once again provided via a grabit on lab46, source code also included here for study:
#include <stdio.h> // include support for C standard library input/output functionality #include <stdlib.h> // include support for general C standard library functionality #include <wiringPi.h> // include support for wiringPi library functionality int main (void) // every program has a starting point, for us in C, it is main() { ////////////////////////////////////////////////////////////////////////// // // Declare variables // int gpio_pin = 17; // set to GPIO pin we are hooking LED circuit into ////////////////////////////////////////////////////////////////////////// // // Initialize wiringPi subsystem and verify no errors occurred // if (wiringPiSetup() == -1) { fprintf (stderr, "[error] wiringPi initialization has failed!\n"); exit (1); } ////////////////////////////////////////////////////////////////////////// // // Display usage information to the user // fprintf (stdout, "Starting program, interrupt to terminate (CTRL-c)\n"); ////////////////////////////////////////////////////////////////////////// // // Initialize utilized GPIO pin to OUTPUT mode // pinMode (gpio_pin, OUTPUT); ////////////////////////////////////////////////////////////////////////// // // Repeat until interrupted (CTRL-c to interrupt) // while (1) { digitalWrite (gpio_pin, LOW); // set signal on gpio_pin LOW delay (1000); // delay for 1000ms digitalWrite (gpio_pin, HIGH); // set signal on gpio_pin HIGH delay (1000); // delay for 1000ms } return (0); }
The program for this project is complete. You merely have to get it on your pi, compile it, and run it, with the appropriate circuitry hooked up to the specified places. In future projects you will start implementing more logic to attain further functionality.
Your program should:
I have prepared a grabit for resources related to this project. To obtain:
lab46:~/src/desig$ grabit desig led0 make: Entering directory '/var/public/SEMESTER/desig/lob0' '/var/public/SEMESTER/desig/led0/Makefile' -> '/home/user/src/desig/led0/Makefile' '/var/public/SEMESTER/desig/led0/led0.c' -> '/home/user/src/desig/led0/led0.c' make: Leaving directory '/var/public/SEMESTER/desig/led0' lab46:~/src/desig$
At which point you can change into the newly created and populated led0 directory.
Since the grabit brought in a Makefile, you can compile your code simply by typing: make
Any compiler errors will go into a text file called errors
To do a full cleaning, run: make clean then make (or make debug)
If you'd like to see compiler messages as you compile, run: make debug
When done and ready to submit, on lab46: make submit
This is the first project to dig into our electronics kit. You have been given a few weeks to get your pi up and running.
You will need the following items from your electronics kit:
To successfully complete this project, the following criteria must be met:
To submit this program to me using the submit tool, run the following command at your lab46 prompt:
lab46:~/src/desig/led0$ submit desig led0 led0.c Submitting desig project "led0": -> led0.c(OK) SUCCESSFULLY SUBMITTED
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.
What I'll be looking for:
26:led0:final tally of results (26/26) *:led0:post picture of unpowered layout to #desig and get approval [4/4] *:led0:post picture to #desig by Sunday before deadline [2/2] *:led0:post picture of powered LED to #desig [4/4] *:led0:grabit the code on lab46 by Sunday before deadline [2/2] *:led0:code is pushed to lab46 repository [6/6] *:led0:no negative compiler messages for program [6/6]
Additionally: