User Tools

Site Tools


haas:fall2023:common:projects:led1

PROJECT

PROJECT: Light Emitting Diodes (led1)

Objective

To expand upon our previous efforts, through wiring up additional LEDs and discovering what pattern they activate in, given the provided program.

We also further utilize time constraints, tying project credit to your starting on the project before the deadline (so be sure to plan accordingly!)

Abstraction

As an exercise in toning your abstraction abilities (literally seeing one thing, but understanding and using it as something else), there will be a number of 'generic' terms used throughout this (and other) documents throughout the course, requiring you to substitute in the pertinent information (else face confusion or error).

Some examples:

  • yourusername - where you see this (likely in a config file or command-line argument), you are NOT to literally type in y-o-u-r-u-s-e-r-n-a-m-e, but instead, substitute in YOUR lab46 username.
  • desig - different classes have different class designations. To make one universal document pertinent to ALL classes, specific examples will instead use 'desig', where you must substitute in the class desig of the class YOU are taking and acting upon in that moment. Example class desigs:
    • c4eng - ENGR1050 “C for Engineers”
    • cprog - CSCS1320 “C/C++ Programming”
    • unix - CSCS1730 “UNIX/Linux Fundamentals”
    • data - CSCS2320 “Data Structures”
    • discrete - CSCS2330 “Discrete Structures”
    • comporg - CSCS2650 “Computer Organization”
    • sysprog - CSCS2730 “Systems Programming”

This sort of abstraction is very similar to that we will find in our utilization of variables in programming, where we can have a “name”, but the data associated with it can change based on various conditions.

Do not be a literalist computer! Start to exercise your abstraction abilities.

2021/08/01 10:37

Locational Awareness

This document is written with TWO locations in mind:

  • lab46 (the system you may retrieve resources and SUBMIT projects)
    • identified in examples with the use of the lab46:~$ prompt
  • your pi (the system you will transfer resources to/from, and WORK ON/COMPLETE projects)
    • identified in examples with the use of the yourpi:~$ prompt

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:

  • YOU cannot install software on lab46. You don't have access.
  • projects CANNOT be SUBMITTED on your pi.

Please pay attention to your prompt, so you can perform the needed activity on the correct system.

2021/08/01 10:37

Reading

Please be sure to familiarize yourself with the following content in “the C book”:

Background

For this project, you will be working with a C program using the wiringPi library on the Raspberry Pi, wiring up a set of four red LEDs to your breadboard and witnessing your ability to control them via software.

Input and Output via the GPIO pins on the pi

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/SEMESTER/DESIG/led1$ 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/SEMESTER/DESIG/led1$ 

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.

Wiring up our circuit

This circuit is basically an expansion upon the one you made last week. So the parts needed remains the same, only expanding in the quantity of certain items as we expand the number of LEDs.

In this project, we will be hooking up a peripheral to some of our Raspberry Pi pins: light emitting diodes (LED) and supporting hardware, and then proceed to interface with them 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:

(1) breadboard

In your electronics kit, what will serve as the base, or mounting point for our electronic circuits, will be the solderless breadboard, which resembles the following:

We will be inserting the metal legs of various components in various holes on the breadboard, to connect our circuit together.

On the breadboard, there are 2 different pathways of connectivity. If you look along the top and bottom of the breadboard, do you see the red and blue strip running horizontal across the board? That means that ALL pins in that row are hooked together (all the pins along the blue line are hooked together with each other, all the pins along the red lines are hooked together with each other). We will be using that to to provide things like a common voltage (+) or a common ground (-) to components that need such things.

In the middle of the breadboard, separated by the valley in the plastic molding, are columns of connected pins (unique columns, also separate from the other side of the valley). Each group of those 5 pins is hooked together.

You'll notice occasional “by 5” counting off silkscreened on the breadboard, which can be used to try to keep things organized.

Here's a logical diagram of the breadboard layout:

Here's a good video overview of the functionality of a breadboard:

(1) T-cobbler and data cable

In your kit should be a T-shaped device (known as the “T-cobbler” with some aspect of pin names silkscreened on it, and a 40-pin ribbon cable). Colors may vary, but essentially it looks like this:

Note that the T-cobbler, on top has a set of pins, enclosed in a plastic rectangle, shaped to fit the ribbon cable.

The plastic rectangle should have a notch cut out from it, and notice that the ribbon cable connector has a notch included in it. That is a key to indicate how you need to connect the ribbon cable into the T-cobbler

(4) red LEDs

You have a few LEDs included in your kit, some of notably different colours. Go for four of the distinctly red-looking ones, resembling the following:

Do note that the two legs of the LED are not equal in length.

The longer leg is the positive side of the LED, called the “anode,” and the shorter leg is the negative side, called the “cathode.” Within an LED, current can only flow from the anode (positive side) to the cathode (negative side) and never in the opposite direction.

(4) 220 Ohm resistors

Your kit should come with 3-4 packs of differently rated resistors (220, 1K, and 10K). You want FOUR of the lowest rated ones, the 220 ohm variety. You should see the value marked on the paper strip holding the legs of the resistors.

The color bands are important, not the body color itself. You resistor body may be beige, may be blue, but the color of the 4-5 rings is what is important. Here we have red (2), red (2), brown (x10); 22 * 10 = 220

Unlike the LED, where the direction of connection matters, it does not matter which way you plug in a resistor. Its purpose is simply to resist.

male to male jumper wire, as needed

As a final piece of equipment you will need, locate and have a few male-to-male jumper wires handy for use in our circuit:

The Circuit to Construct

With those components, you will want to place them onto your breadboard, expanding upon last week's circuit (picture of last week's circuit provided):

Take note of how everything is being plugged in, and what connection on the T-cobbler is being utilized.

Notice how the circuit runs from ground to LED to resistor to pin “GPIO17/Physical pin 11/Wiring Pi pin 0”

Please keep in mind:

  • EACH LED needs to be interfaced with a unique GPIO pin
  • EACH LED needs a corresponding resistor (no sharing!) Forgetting to use the resistor can lead to damage to the LED!
  • ground connections can be shared (we call this “common ground”)
  • take note of which GPIO pin you are using, so can update the led1.c program

Before you seek to actually test your circuit, please get verification to proceed from the class channel on discord by posting a clear picture of everything. There are MANY moving parts, and especially as we have our first exposure, it is best to insert as many quality control checks as possible to ensure the greatest chances of mistakes are mitigated.

It may be clarifying if you arranged your four LEDs in a row, from right to left, in order of your GPIO pins you've plugged into.

Testing connectivity

To verify whether everything is hooked up correctly (including the correct positioning of the LED), please try the following:

Set wiringPi pin 0 to OUTPUT mode

Remember in that big readout of all the gpio pins above? Take a look at the settings for wiringPi pin 0:

 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | ...
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 |

Note that, currently wPi pin 0 is set to INPUT (with no voltage). We need to change that to OUTPUT, which you can do by doing the following:

yourpi:~/src/SEMESTER/DESIG/led1$ gpio mode 0 OUT

At which point that particular pin will be changed from IN to OUT:

 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | ...
 |  17 |   0 | GPIO. 0 |  OUT | 0 | 11 |

Okay, output mode enabled, now to twiddle the bits off and on:

yourpi:~/src/SEMESTER/DESIG/led1$ gpio write 0 1      ## activate voltage on wiringpi pin 0 (turn on)
yourpi:~/src/SEMESTER/DESIG/led1$ gpio write 0 0      ## deactivate voltage on wiringpi pin 0 (turn off)

If your LED is hooked up correctly, the LED should light up, ideally during the ON line.

If you get no activity out of the LED, try reversing it and try again.

With an expanded set of LED circuits, be sure to test the operation of each one.

Program

It is your task to write a C program that interfaces successfully with four independently connected LED circuits, arranged in some orientation to ascertain an order or positioning, where your program will (in endless fashion, or until being manually interrupted) display a count (in binary) of values from 0 to 15 (then rollover, or reset).

If “1” means the LED in that position is ON, and “0” means the LED in that position is OFF, then you want to write a program that performs the following progression (over and over again):

0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1   <-- 15, the maximum value to display
0 0 0 0   <-- 0, we "roll over" and start again
0 0 0 1
0 0 1 0
...

The program files provided for this project are, while not complete, a good base to start from. Be sure to ASK QUESTIONS, and do so EARLY enough so that you aren't in a mad dash to make the deadline.

Bitwise logic

Since the off/on state of each LED is essentially binary in nature (or one bit of an overall sequence), why not utilize the powerful bitwise tools available to us in C?

bitwise-AND

C provides a bitwise AND operator, the & symbol, which can be used in equations to perform bitwise-AND operations:

char value  = 5;         // 00000101 in binary
value       = value & 6; //   00000101
                         // & 00000110
                         //   ========
                         //   00000100

Remember the truth table for the AND:

( AND )
A B | X
====+===
F F | F
F T | F
T F | F
T T | T

Think of how this property could be used in combination with other concepts we've learned about in C to solve the problem at hand.

bitwise-iOR

C provides a bitwise inclusive OR operator, the | symbol, which can be used in equations to perform bitwise-iOR operations:

char value  = 5;         // 00000101 in binary
value       = value | 6; //   00000101
                         // | 00000110
                         //   ========
                         //   00000111

Remember the truth table for the iOR:

( iOR )
A B | X
====+===
F F | F
F T | T
T F | T
T T | T

Think of how this property could be used in combination with other concepts we've learned about in C to solve the problem at hand.

bitwise-xOR

C provides a bitwise exclusive OR operator, the ^ symbol, which can be used in equations to perform bitwise-xOR operations:

char value  = 5;         // 00000101 in binary
value       = value ^ 6; //   00000101
                         // ^ 00000110
                         //   ========
                         //   00000011

Remember the truth table for the xOR:

( xOR )
A B | X
====+===
F F | F
F T | T
T F | T
T T | F

Think of how this property could be used in combination with other concepts we've learned about in C to solve the problem at hand.

Grabbing project resources (on lab46)

I have prepared a grabit for resources related to this project. To obtain:

lab46:~/src/SEMESTER/DESIG$ grabit desig led1
make: Entering directory '/var/public/SEMESTER/desig/led1'
'/var/public/SEMESTER/desig/led1/Makefile' -> '/home/user/src/SEMESTER/DESIG/led1/Makefile'
'/var/public/SEMESTER/desig/led1/led1.c' -> '/home/user/src/SEMESTER/DESIG/led1/led1.c'
make: Leaving directory '/var/public/SEMESTER/desig/led1'
lab46:~/src/SEMESTER/DESIG$ 

At which point you can change into the newly created and populated led1 directory.

Getting project resources from lab46 to your pi

Okay, you've snagged the project files on lab46. Now, how to get them to your pi?

The same way you've been juggling project files already, by using your mercurial repository!

Using the hg tool, be sure to add, commit, and push successfully on lab46.

Then, over on your pi, use hg to pull and update the new changes into place. Then you can proceed.

On your pi

Implement the needed program on your pi in conjunction with testing and verifying operation of your properly hooked-up electronics circuit. 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:

  • build-essential (hopefully you took care of this in ntr0)
  • wiringpi

Installing wiringpi

NOTE: you should have already installed wiringpi in order to do the led0 project; you do not need to do it again

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/SEMESTER/DESIG/led1$ 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/SEMESTER/DESIG/led1$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
...
yourpi:~/src/SEMESTER/DESIG/led1$ sudo dpkg -i wiringpi-latest.deb
...
yourpi:~/src/SEMESTER/DESIG/led1$ rm -f wiringpi-latest.deb

Compiling

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

Process

The general flow of the process (one way of going about it, anyway) can be described as follows:

number <- 0
loop (endlessly)
    if the number is zero
        activate/deactivate the four LEDs to represent zero

    if the number is one
        activate/deactivate the four LEDs to represent one
       
    if the number is two
        activate/deactivate the four LEDs to represent two

    ... on and on through the cases for the remaining numbers

    strategic delay so you can see the current number

    let the number increment by one, but not to exceed fifteen
done

Submission

To successfully complete this project, the following criteria must be met:

  • Code must compile cleanly (no notes, warnings, nor errors)
  • Output must be correct, and match the form given in the sample output above.
  • Code must be nicely and consistently indented
  • Code must be well commented
  • Do NOT double space your code. Group like statements together.
  • Output Formatting (including spacing) of program must conform to the provided output (see above).
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.
  • Post required images and obtain needed confirmation to proceed from me on class channel on discord.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

lab46:~/src/SEMESTER/DESIG/led1$ make submit

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:

39:led1:final tally of results (39/39)
*:led1:post picture of unpowered layout to #desig and get approval [6/6]
*:led1:post picture to #desig by Sunday before deadline [6/6]
*:led1:post picture of powered LEDs in layout to #desig [6/6] 
*:led1:grabit on the code on lab46 by Sunday before deadline [3/3]
*:led1:led1.c code adequately expanded per project requirements [6/6]
*:led1:led1.c comments describing what is happening [6/6]
*:led1:updated code is pushed to lab46 repository [6/6]

Additionally:

  • Solutions not abiding by spirit of project will be subject to a 25% overall deduction
  • Solutions not utilizing descriptive why and how comments will be subject to a 25% overall deduction
  • Solutions not utilizing indentation to promote scope and clarity will be subject to a 25% overall deduction
  • Solutions not organized and easy to read are subject to a 25% overall deduction
haas/fall2023/common/projects/led1.txt · Last modified: 2021/09/12 22:54 by 127.0.0.1