Chapter 2: Digital Output - LEDs

 

 

 

Figure 1: LED

 

Chapter 2: Digital Output - LEDs

 

 

 

Figure 1: LED

LEDs are everywhere

LEDs (Light Emitting Diodes) have been used as indicator lights for decades in things like alarm clocks and entertainment systems, but recently they have started taking over many general lighting tasks since they are durable and very energy efficient. In this chapter you will learn how to design circuits using LEDs and how to use LEDs with Arduino software to indicate events to people using your system.

 

 

Figure 2: Arduino Uno R3 LEDs

 

Figure 2 shows the four LEDs on the Arduino. The Power LED indicates that the board has power (this LED is the first and most fundamental debugging indication - if something seems amiss, first make sure the power LED is on). The TX and RX LEDs indicate serial communications traffic with the TX and RX LEDs blinking when transmitting and receiving respectively. This provides another very useful diagnostic tool helping you see if the board is communicating and which direction the communication is going. The Pin 13 LED is connected to the Arduino Digital I/O pin 13 and can be used for a variety of software tests without having to add any external hardware to see the software in action - our first program in Chapter 1 blinked this LED to indicate that the program was working. The LEDs on the Arduino are SMD (Surface Mount Devices) like the one shown in Figure 3 and while they do not look like the LED in Figure 1, the difference is in the size of the packaging - the active circuit, the silicon die, is about the same size in both packages. The limits to how small the LED is made is not the size of the semiconductor die, but the surrounding casing and connections which must be large enough to be handled by people and machinery.

 

 

Figure 3: SMD LED

 

 

 

Figure 4: LED Cross Section

 

How an LED Works:

The name says it all: Light Emitting Diode. A diode is an electronic device that only allows current to flow in one direction. It acts as a sort of valve that prevents the current from backing up or flowing in the wrong direction. [We will learn more about diodes in a later chapter.] A diode can be made from semiconductors, the same silicon based material that computer chips are made from. Some of these materials configured as diodes emit light when a current passes through them. The color of the light depends on the materials used. The brightness of an LED is proportional to the electricity flowing through it, however if you provide too much electricity the LED heats up and burns out.

 

Since too much electrical current will cause an LED to burn out we need a way to control the flow of electricity to provide the maximum light without risking destroying the LED. Electrical flow is known as current. We use a component called a resistor that resists current flow to an acceptable level. Resistors have a given resistance value measured in units called ohms. We may also see expressed with the Greek character omega: Ω. To control the current through our LED in this lab we will use a resistor with a value of 1000 Ω.

What is a Resistor?

Materials with low resistance are known as conductors, while material with high resistance are known as insulators. Electrons move easily in some things such as copper wire but are stopped cold by some things such as glass. Copper has very low resistance - the jumper wires in the projects kit have near 0 ohms resistance (about 1 ohm per 62 feet), while glass can have millions of ohms of resistance.

 

Electronic components often have symbols associated with them that are used in schematics, which are drawings of an electronic circuit. Figure 5 shows a schematic symbol for resistors next to a Fritzing drawing of a resistor showing the color stripes. [Fritzing is an open source electronic design program for drawing breadboards, schematics, and printed circuit boards]

 

 

Figure 5: Resistor Schematic Symbol and Fritzing Illustration

 

The Ohm (Ω) value of resistors is shown by the color of the stripes around the resistor as shown in Figure 6. Notice that the stripes are bunched toward one end - the end you start counting from.

 

Figure 6: Resistor Value Stripes

 

 

Figure 7: Resistor Values Card from Projects Kit

 

By referring to the Resistor Values Card from the projects kit we see that the first band is brown and represents a 1, the second band is black for Ω, the third band red for 2 zeros. So we read this as 1000 Ω resistor. The fourth band is gold for +- 5% tolerance so we know that the actual resistance is 1000 + or - %5 of 1000 which is 50 Ω so we have a tolerance of from 950 Ω to 1050 Ω and that is plenty good enough for our purposes.

Often you will see resistor values shortened with either k (kilo) for 1000, for instance 1 k Ω. You might also see 4K7 Ω for 4.7 k Ω or 4700 Ω. And you may  see M (Mega) or in usage like 1M Ω for 1,000,000 Ω.

The LED Component

In Figure 8 we see the schematic symbol and Fritzing illustration for an LED.

 

 

Figure 8: LED Schematic Symbol and Fritzing Illustration

 

The schematic symbol has a triangle and a bar that shows the direction that the diode allows current to flow, the two arrows indicate that this particular diode emits light. Because this component is directional with respect to current there is a positive side, the side in which the current enters and a negative side, the side in which the current exits. The positive side is called the anode and is sometimes represented with a plus + (plus sign) and the negative side is called a cathode and represented by a - (negative sign).

Building the Circuit

What is a Circuit?

 

 

Figure 9: Electric Current from Battery through Resistor and LED

 

We get electricity to do useful work by channeling it from devices that produce electric force (like generators and batteries) through devices that do electric work (like lights and motors) and back to the device that created the force. That last part is critical. Circuit is just a fancy way of saying 'circle': electricity must run around a circle to do useful work.

 

Figure 9, shows arrows marking the direction of conventional current from the higher voltage side of a 9-volt battery (the positive terminal) through a resistor and an LED back around to the lower voltage terminal of the battery.

 

You have probably seen really complex circuits on printed circuit board or as schematics, but no matter how complex it looks, it can be simplified to one part producing the force as a current, one part using that force to do work, and the electrical connection between them.

 

In case you wonder why we have to add 'conventional' to current? Well that is because the guy whose face graces the US 100 dollar bill, yes Benjamin Franklin himself, guessed wrong when he said that charge is carried by positive particles that flow from the + side of a circuit to the - side. It was much later that folks came to realize that the actual charge carrier is an electron, which has a negative charge, and of course, by then the text books all had current going from + to -, so we are still stuck with this backwards concept.

Schematic Symbols for 5V and GND

We see that the current flows from the higher voltage [anode (+)] to the lower voltage, in Figure 10 we see the schematic symbols for the current source and destination. The, source shown [anode (+)] is 5 volts and the destination is 0 volts [cathode (-)] also known as ground (GND).

 

 

Figure 10: Schematic Symbols for 5 Volts and Ground

 

We will build our circuits on the Arduino Proto Shield that is included in the Arduino 101 projects kit. But first let's see how a breadboard works.

How a breadboard works

 

In the good old days, electronics experimenters would build prototypes by nailing components to an actual wooden breadboard and then soldering wire between connection points. Today's solderless breadboards are made of plastic blocks with holes on 0.1-inch centers that allow you to insert jumper wires into hidden clips below the holes.

 

Integrated circuits (IC) are made of tiny blocks of silicon that are much too small and delicate to be used by hand. Early on, they were encapsulated in a special package like the one shown in Figure 11. This DIP (Dual Inline Package) packaging has the IC silicon encapsulated in a block of epoxy with the pins directed downward. These pins are usually lined up 0.1 inch apart on the side of the package with the two lines often 0.3 inch apart. Breadboards were designed facilitate prototyping with these packages so that these DIP ICs could be plugged the breadboard with the pins lined up on either side of the trough in the middle of the breadboard as shown in Figure 12. The vertical rows of sockets shown above and below the IC are electrically connected and allow components to be added to the prototype to make temporary circuit contacts.

 

Figure 11: ATmega328 DIP Package

 

 

Figure 12: ATmega328 on a breadboard

 

Figure 13 shows the top and bottom of a solderless breadboard (the bottom has the foam tape stripped off to show the connections. Figure 14 shows the clips pulled out. Figure 15 shows how a clip grabs a wire and Figure 16 shows a cutaway drawing with an LED, 1K O resistor, and a jumper wire all connected such that if you have +5 volts in the upper + channel and GND in the lower - channel, the LED should light up.

 

 

Figure 13: Top and bottom of breadboard

 

 

Figure 14: Breadboard bottom with clips pulled out

 

 

Figure 15: Illustration of how clip connects to wire

 

 

Figure 16: Cross section with components

 

Our Mini Breadboard

The Arduino 101 Projects Kit has a miniature breadboard shown in Figure 17. Figure 18 shows that this board has 17 columns of sockets with two sets of 5 connected sockets, one each above and below the central trough. This provides 170 tie-points for connecting circuits together.

 

 

Figure 17: Mini Breadboard

 

Figure 18: Mini Breadboard Internal Connections

 

The Mini Breadboard Shield Mounted on an Arduino

Our electronics and computing learning platform throughout this book has a Arduino Proto Shield mounted on an Arduino as shown in Figure 19. This platform provides many useful and convenient features that you will learn about in the coming Lab exercises.

 

 

 

 

Figure 19: Prototyping Area

 

Lab 1: Assemble your Arduino Mini Breadboard Shield

If you have the Arduino Proto Shield already assembled you may proceed with this lab. If your Arduino Proto Shield is a kit of un-assembled parts then you will first go to: http://blog.smileymicros.com/arduino-proto-shield-information-and-assembly/ and assemble your Arduino Proto Shield before beginning this exercise.

 

Required Tools:

1 Arduin

1 Proto Shield

Estimated time for this lab: 5 minutes

BE VERY CAREFUL NOT TO BEND THE PROTO SHIELD LEGS WHEN YOU DO THIS LAB!

Check off when complete:

Place the shield with the long legs above the mating Arduino header sockets, as shown in Figure 20

 

Figure 20: Plug Shield into Arduino step 1

 

Place the legs carefully into the sockets making sure that they align properly as shown in Figure 21. You may have to use your fingernails to carefully align the pins while gently applying pressure.

 

 

 

Figure 21: Plug Shield into Arduino step 2

 

Finally Figure 22 shows the legs pushed in all the way. Note that there is about 1/16 space between the bottom of the Shield and the top of the Arduino sockets.

 

Figure 22: Plug Shield into Arduino step 3

 

Lab 2: How to use an LED - analog

 

Required Tools:

1 Arduino

1 Proto Shield

 

Estimated time for this lab: 20 minutes

 

Parts Required:

2 Red LEDs

2 1000 Ω Resistors

2 jumper wires

 

Check off when complete:

CAUTION: MAKE SURE POWER IS OFF BEFORE YOU BUILD A CIRCUIT. Check that the Arduino USB is unplugged and that you do not have a battery plugged into the power connector.

Plug your Arduino Proto Shield into your Arduino as shown in Lab 1.

 

Figure 23: LED in Mini Breadboard

 

Plug an LED into the breadboard as shown in Figure 23. Make sure that the long leg is toward the bottom of the board as shown. Notice that the illustration shows the short leg connected to column of 5 sockets above the LED and that the long leg connects to column of 5 sockets below the LED.

 

Figure 24: LED + Resistor Circuit

 

Plug a 1000 Ω resistor (brown, black, red bands) as shown in Figure 24 and note the connection to the 5 sockets in two columns, one unconnected and the other connected to the LED long leg.

 

Figure 25: LED + Resistor + Power + Ground Circuit

 

Attach a wire between the column of 5 sockets on the LED short leg to GND (ground) and then connect the column of 5 sockets on the resistor to 5V(5 volts) as shown in Figure 25.

Provide power your system by either plugging the Arduino USB into a PC as shown in Figure 26 or plugging a 9-volt battery into the power jack as shown in Figure 27. When you provide power, the LED should light up.

 

Figure 26: Power from the PC

 

Figure 27: Power from an external 9-V battery

 

Figure 28: LED + Resistor Schematic

 

In Figure 28 you see a schematic illustration on the left of the LED circuit on the right. At the top you see a symbol with 5V above it. That symbol is for the input of 5 volts into the circuit. At the bottom you see three progressively smaller bars that are the schematic symbol for ground which is 0 volts. Since current flows from a higher voltage to a lower voltage, here it will run from 5V to 0V (ground) and the LED will light up. Notice that the current in the schematic image flows from the top to the bottom, but in the Arduino Proto Shield illustration the current flows from the bottom to the top. These two images are 'upside down' relative to each other. This is just an artifact due to the traditional way schematics are drawn, usually with the higher voltage toward the top and the lower voltage or ground toward the bottom. The 'flipped' illustration is due to the 5V being at the image 'bottom' on the Arduino Proto Shield. This may be momentarily confusing, but it is good to keep in mind that current flows from high to low voltage and has nothing to do with the physical orientation of the device.

 

What Happens if We Reverse the Current?

In Figure 28 we have the 5 volts connected such that the current runs from the 5 volts through the resistor and the LED to ground. But what happens if we reverse the connection and place the 5 volts at the bottom and the ground at the top of the schematic as shown in Figure 29?

 

Reverse the 5V and GND wires as shown in Figure 29 - What happens?

 

 

Figure 29: Reversing 5V and GND

 

Why didn't the LED light up? It is because an LED is a diode - as discussed earlier - symbol. Notice that we had to bend the lines in the schematic to put the GND at the top and the 5V at the bottom. This is because, as mentioned above, it is conventional to put the higher voltage symbol above the ground symbol.

 

Lab 3: How to use an LED - digital

 

Now that we know how to light up an LED with current supplied by our 5 volts source, let's apply that to using an Arduino digital output pin to supply 5 volts or 0 volts to turn the LED on and off.

We saw in Chapter 1 Lab 4 how to write and run a simple program that blinks the pin 13 LED. In this lab we will put an LED on the breadboard and make it blink using pin 12.

 

Required Tools:

1 Arduino

1 Proto Shield

Estimated time for this lab: 30 minutes

Parts Required:

2 Red LEDs

2 1000 Ω Resistors

2 jumper wires

 

Check off when complete:

Make sure the power is off before building the circuit.

Hookup the wire to pin 12 and ground as shown in Figure 30.

 

Figure 30: LED on Pin 12 Circuit

Plug the USB cable into the Arduino.

Open the Arduino IDE and File/Examples/Basic/Blink like you did for Lab 2.

Change the code to add the pin 12 controlled LED as follows:

void setup() {
     // initialize the digital pin as an output.
     // Pin 13 has an LED connected on most Arduino boards:
     pinMode(13, OUTPUT);
     pinMode(12, OUTPUT); // Use pin 12 to blink LED on breadboard
}

void loop() {
     digitalWrite(13, HIGH); // set the LED on
     digitalWrite(12, HIGH); // set the LED on
     delay(1000); // wait for a second
     digitalWrite(13, LOW); // set the LED off
     digitalWrite(12, LOW); // set the LED off
     delay(1000); // wait for a second
}

 

 

 

Figure 31: Modified Blink Sketch

Note that the delay(1000) causes a 1000 millisecond (1 second) delay.

Following the methods you learned in Chapter 1, verify and upload the program to your Arduino.

Does the pin 13 LED on the breadboard blink?

  • Yes - Great, you are ready to move on.
  • No - Looks like you'll need to do some debugging as follows:
    • Does the Pin 13 LED on Arduino blink?
      • Yes - You've probably wired the circuit wrong on the breadboard. Carefully check your wiring.
      • No - You've probably entered something wrong in the code or not uploaded it properly. Carefully check that the code is exactly as shown and try verifying and uploading it again.

 

Summary

In this chapter you were introduced to the following concepts:

  • How to use an Arduino to turn things on and off.
  • How to use an LED to produce light.
  • That diodes conduct electrical current in only one direction.
  • That an LED's long leg is connected to the anode (+) and the short leg is connected to the cathode (-).
  • That an LED must have a resistor in the circuit to limit the current.
  • The purpose and use of the Arduino Proto Shield.
  • How to use a breadboard.
  • How to turn and LED on and off with electricity.
  • How to turn and LED on and off with a microcontroller.

 

Questions

1. What does LED stand for?

2. If you are having a problem with your Arduino, what is the first thing to check?

3. How can you tell that your Arduino is receiving information from you PC?

4. What units are used to describe resistance?

5. To make it light up, the short leg of an LED is connected to ground -T or F?

6. What do you use a breadboard for?

7. What happens if you forget to put the resistor in the LED circuit?

8. The cathode should be connected to the higher voltage - T of F?

9. Where does the Arduino get its electrical power?

10. What happens if you reverse the electrical current to an LED?

Exercises

1. Draw the schematic for an analog circuit that would turn on two LEDs.

 

2. Modify the blink program so that the LED is on for 1 second and off for 1/2 second.

Project

1. Add an LED to the circuit attached to pin 11. Modify the blink software so that it blinks the breadboard LEDs on pins 11 and 12 in addition to the Arduino built in LED connected to pin 13.

Solutions

Q1: Light Emitting Diode.

Q2: See if the power LED is lit.

Q3: The RX LED will be blinking.

Q4: Ohms.

Q5: T

Q6: Prototyping circuits.

Q7: The LED burns out.

Q8: F

Q9: Either from the USB connection or a battery plugged into the power jack.

Q10: The current will not flow through it.

 

E1:

 

Figure 32: Circuits to turn on two LEDs

 

 

E2: Change the second delay(1000) to delay(500).

 

P1: The Circuit to build:

 

Figure 33: Use software to blink two LEDs

 

The Software:

 

void setup() {
     // initialize the digital pin as an output.
     // Pin 13 has an LED connected on most Arduino boards:
     pinMode(13, OUTPUT);
     pinMode(12, OUTPUT); // Use pin 12 to blink LED on breadboard
     pinMode(11, OUTPUT); // Use pin 11 to blink LED on breadboard
}

void loop() {
     digitalWrite(13, HIGH); // set the LED on
     digitalWrite(12, HIGH); // set the LED on
     digitalWrite(11, HIGH); // set the LED on
     delay(1000); // wait for a second
     digitalWrite(13, LOW); // set the LED off
     digitalWrite(12, LOW); // set the LED off
     digitalWrite(11, LOW); // set the LED off
     delay(1000); // wait for a second
}

 


 

Supplementary Questions and Answers:

 

NOTE: these are here temporarily until I get a quiz module installed where I put these sorts of things. In the meantime if you think of some good quiz questions please post them on the forum.

A diode lets current pass in two directions - T or F?

F

 

How can you tell that your Arduino is sending information to your PC?

The TX LED will be blinking.

 

What happens if you send current to an LED without using a resistor?

It may burn out.

 

What do you use to prevent an LED from burning out from receiving too much current?

A resistor.

 

How can you tell the resistance of a resistor?

You use the color bands.

 

What is a drawing of an electronic design called?

A schematic.

 

What does the symbol: Ω stand for?

Ohms.

 

A conductor has very high resistance - T or F?

F

 

An insulator has very high resistance - T or F?

T

 

What is this called?

A resistor

 

What is this called?

An LED

 

The LED long leg goes to ground - T or F?

F

 

Draw the schematic symbol for a resistor.

 

Figure 34: Resistor schematic symbol

 

Draw the schematic symbol for an LED.

 

Figure 35: LED schematic symbol

 

Draw the schematic symbol for a 5-volt source.

 

Figure 36: 5-volt source schematic symbol

 

Draw the schematic symbol for ground.

 

Figure 37: ground schematic symbol

 

What is a circuit?

It is a path for electrical current to flow from a more positive to a more negative voltage value.

 

How do you get electricity to do useful work?

You pass it through a circuit.

 

Draw a circuit for lighting an LED

 

Figure 38: Circuit to light an LED

 

Who decided which direction electric current flows?

Benjamin Franklin

 

The anode is connected to the higher voltage to make the LED light - T or F?

T

 

What does DIP stand for?

Dual Inline Package

 

What is this package called:

 

Figure 39: DIP or IC package

DIP or IC

 

How does a breadboard connect one wire to another?

It has metal clips under the holes.

 

The center part of a breadboard has connections by rows - T or F?

F

 

The center part of a breadboard has connections by columns - T or F?

T

 

What does the 1000 Ω resistor do in the LED circuit?

It limits the current.

 

What does 'digital output' do?

It puts either high or low voltage on a pin.

 

What is digital output used for?

To turn things on or off

 

Remember that all the components used in the Arduino 101 series are available from the Nuts&Volts magazine web site. And if you have any questions about this series, please don’t hesitate to visit the forum on www.arduinoclassroom.com and ask.