Crosswalk Project

ECE301 - Intro. to Circuits, Summer 2019, EiL Written by Ben Sergent V
ECE301 Crosswalk | Stage 1
Back

Stage 1 - LEDs

LED Voltages

LEDs (Light Emitting Diodes) are circuit components that only allow current to flow in one direction through them (as with all diodes) and emit light proportional to the current flowing through. Note that this is not directly proportional as LEDs do not follow Ohm's Law exactly (and thus known as a non-linear device). Each color of LED has a different forward voltage (basically the part of the voltage across the LED that must be reached before it starts glowing at all), and so each color must have a different voltage applied to achieve the same brightness. Red LEDs generally require a lower voltage while blue LEDs require a higher voltage. Most LEDs are rated for a current of 20mA. Give them more than that, and you risk burning out the LED and making it useless thenceforth.

What are the recommended voltages for red and pure green LEDs?

Vred = V
Vgreen = V
Check

Resistance Values

5V = (20mA * R) + V_led

These voltages vary for each LED (as unfortunately no two components are identical due to error), so we'll use the values 1.8V for the red LED and 3.3V for the green LED. Now that we have the recommended voltages for each of the LEDs, we can look at calculating what resistor value is needed in series with each LED to reach the recommended current of 20mA. Arudino output pins operate at 5V (5V for digital high, 0V for digital low). You can think of the ouput pins as voltage sources with switches in series with them. In reality, there are transistors because the switches are controlled by the microcontroller, but we'll ignore that.

With the voltage of the Arudino output pin and the recommended voltage and current draw for the LEDs, we can use Kirchhoff's voltage law (KVL) to calculate the voltage across the resistor in series with the LED, and then use Ohm's law to calculate the resistance for the resistor. This is done twice, once for each LED.

What are the resistor values for each color of LED using the values labeled as correct in the previous question?

Rred = Ω
Rgreen = Ω
Check

Matching Brightness

Larger resistances will result in a smaller current flow (according to Ohm's Law), so we can increase the size of the resistors used until the LEDs are too dim to be visible. This is important because we don't have any resistors with the specific values found in the previous section in our kit (the calculated values are actually fairly uncommon when buying resistors, although they are custom made for integrated circuits). Instead, we'll use the closest values we can without getting lower than the found values. In our kit, the closest is 220 Ω. Working through the above equations again, these resistances will give us currents of ~14.5 mA for the red LED and ~7.7 mA for the green LED. These values are fine, but our LEDs are going to be glowing at different brightnesses. Let's try to fix that.

To get the green LED a bit brighter, up to the ~14.5 mA of the red LED, we need to decrease the resistance value in series with it. As you've learned, placing resistors in parallel results in an equivalent resistance lower than that of either original. If we place two 220 Ω resistors in parallel, the resulting current through the green LED is ~15.5 mA, much closer to that of the red LED.

What is the equivalent value of two 220 Ω resistors in parallel?

Req = Ω
Check

Reading Resistors

See paper in the Arduino kit

Resistors have color coded bands along their length that denote their value. The two or three rightmost bands (two for four total bands, three for five total bands) are read as a straight nominal value (called the mantisa in floating point representation). The next band is the multiplier for the nominal value (just called the exponent in FPR). The last band is the tolerance of the resistance (as you can't produce a resistor with an exact value for a number of reasons.

As an example, a 10 kΩ resistor is represented as brown-black-black-red-brown. The last band, the tolerance, varies based on the quality of resistor. The brown-black-black bands are read as 1-0-0 or 100, and the red band is read as 102, resulting in a value of 100x102±1% or 10k±1%. The same resistor could be represented as brown-black-orange-tolerance (10x103) on a four band resistor.

There is a slew of videos online if you need more explanation or examples.

What are the color bands for a five band 220 Ω resistor, ignoring the error tolerance band?

Please ignore the nominal label on the resistor packet labeled 220 Ω...
Check

Building the Circuit

Google for "breadboard connection"

The quickest and easiest way to prototype circuits is with a breadboard. Breadboards (like the two in the starter packet) are plastic bricks perforated with holes that will conduct electricity. Circuit components and wires can be pushed in these holes to easily create a connection. This prevents the need of soldering (melting conductive solder onto the wires and component leads to connect them) or using wire nuts to twist them together.

On the large breadboard that we will be using to build the crosswalk emulator, there are two sets of two vertical strips. All of the holes within a vertical strip along the sides are connected (they're a single node) and usually called rails. Most often, these are used for Vcc (the voltage source commonly 5V or 3.3V in digital circuitry) and ground (0V). You can use these vertical strips for anything you want though.

In the middle, there are two vertical sections separated by a gap. In each of the sections, all the holes on each horizontal strip are connected (there are nodes of five holes in width all up and down each section). These nodes are generally used to build the actual circuitry.

Full circuitry diagram

Now that you know how to use the breadboard, pick out the larger one, the Arduino, resistors, and LEDs to build the full circuit to light up both LEDs. For the voltage sources, the positive terminal will be the digital I/O pins (labeled as 2 through 13), and the negative terminal will be any gound pin (labeled GND). Note that the LEDs may share a common ground pin (thus is the nature of ground). In addition, the LEDs are polarized by nature (because they're diodes) and will only allow current to flow forward. The longer lead/wire is positive while the shorter lead is negative. If the LED doesn't light up in later on, it may be because you have the polarity flipped.

Arduino IDE

To actually program the Arduino to have the pin voltages change, you'll need to either install the full IDE or the browser plugin. If you download the plugin, go to their online editor to start coding. I've always used the full IDE, so I don't know a lot about the online editor.

In either program, you'll want to create a new sketch and copy-paste the skeleton code from the bottom of this page inside. With that program, you can verify/compile by clicking the "Verify" check button and actually upload with the "Upload" arrow button. Uploading the sketch will send it to the Arduino via the USB cable and will take a moment. If you get an error about not being able to open the device, make sure the Arduino is connected to your computer via the USB cable and that the correct board type is selected in the IDE. You can check the type from the menu bar: Tools > Board > Arduino/Genuino Uno. The board type is necessary because the machine code that your code will compile to upon upload will vary depending on what microcontroller you're using. Once the Arduino stops spazzing out, it'll start running the routine you programmed in the sketch.

If you have any problems (such as your resistors getting too hot), you can stop the code from executing by unplugging the USB/power cable. You can also press the "reset" button to start your code from the beginning again.

As for the programming side of the IDE, the language is C++ with special libraries for interfacing with the Arduino hardware. Every sketch consists of two functions by default: setup() and loop(). The setup() function is called once when the Arduino first begins executing the sketch and is used to initialize state and pin modes. The loop() function is where the main logic of the program is generally located and is executed as quickly as possible, over and over, forever. You can think of this as a while(true) loop.

For more details on the Arduino language, see their language reference.

Powering the Pins

Now that you know how to use the Arduino IDE, it's time to start writing some code (only four lines though). You'll need to set the pins you selected earlier when building the circuit to be output pins, and then turn them on. I've prepared some skeleton code for you to add your four lines to. Go ahead and replace your sketch with it, and then we'll get into what functions and constants you'll need; or you can just write it from scratch if you want.

The first function you'll need to use is pinMode(pin, mode) where pin is an integer that represents the pin number written on the board (the earlier pins of 2 through 13), and mode is one of two constants: INPUT or OUTPUT. Since we'll be outputting a voltage to the LED instead of reading a value, you'll want to set the pin to OUTPUT. Generally, the pin numbers are placed at the top of the file as to be easily changed later (by students working on later stages if/when they need to use different pins). Do this by declaring a constant integer and referencing it in your function call (const int PIN_LED1 = x; where x is the actual pin number and PIN_LED1 is appropriately named). Sometimes this is done with compiler directives instead (such as #define PIN_LED1 x) but both are optimized to the same machine code during compilation. Do this for each LED.

The second function you'll need to use is digitalWrite(pin, output) where pin is the pin number (use the constant you defined earlier) and output is either HIGH or LOW (were HIGH is 5V and LOW is 0V). Set each LED's pin to 5V.

Skeleton Code

// Crosswalk Project - Stage 1
// ECE301 - Intro. to Circuits

// TODO Replace the -1s with your pin numbers
const int PIN_LED_RED = -1;
const int PIN_LED_GREEN = -1;

void setup() {
	// TODO Set the pin modes for each LED
	// pinMode(pin constant, INPUT | OUTPUT);
	// TODO Turn each LED on
	// digitalWrite(pin constant, HIGH | LOW);
}

void loop() {
}