Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/BoeBot/What is an LED"

From ROBOTC API Guide
Jump to: navigation, search
(Understanding the circuit)
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{tl|1|}}
+
{{DISPLAYTITLE:What is an LED?}}
 +
<yambe:breadcrumb self="What is an LED?">Tutorials/Arduino_Projects/Mobile_Robotics/BoeBot|Parallax BoeBot + Arduino Shield, Mobile Robotics Platform</yambe:breadcrumb>{{tl|1|}}
 
== What is an LED? ==
 
== What is an LED? ==
An LED is a “Light-Emitting Diode”, which is an electronics component that emits light when it is powered. Since it is a diode, and diodes only let the current flow in one direction, for an LED to work it must be wired correctly. When connecting an LED it is important to be able to distinguish which lead is the anode (positive) and which is the cathode (negative). To make it easy to identify the leads, all LEDs are manufactured with two physical properties. The first is that LEDs have one lead that is longer that the other. This longer lead is the anode, and the shorter one is the cathode. The second feature is a small flat notch on the side of the led. The lead that is closer to the notch is always the cathode. This is important to remember since the leads may have been clipped.
+
An LED (Light Emitting Diode) is an electronics component that emits light when it is powered. Since it is a diode, an LED must be wired correctly for it to work (diodes only let current flow in one direction). When connecting an LED it is important to distinguish which lead is the anode (positive) and which is the cathode (negative).  
  
''Insert image of LED and how it relates to the schematic symbol.''
+
To make it easier to identify the two leads, all LEDs are manufactured with two physical properties; the first is that LEDs are manufactured with one lead that is longer that the other. The longer lead is the anode(+) and the shorter lead is the cathode(-). The second feature is a small flat notch on the side of the LED. The lead that is closer to the notch is always the cathode. This is important to remember since the leads may have been clipped.
  
When referencing a schematic (drawing of the electrical pathways and components using symbols), the symbol for the LED shows which way the current flows and allows you to connect the LED the correct way. The cathode on the symbol is the side with the line across the point of the triangle and the anode is the other side.
+
[[image:LED_With_Schematic_Symbol_and_Labels.jpg|thumb|c|center|400px|Close up of an LED and one of the possible schematic symbols for an LED.]]
 +
 
 +
When referencing a schematic (drawing of the electrical pathways and components using symbols), the symbol for the LED shows which way the current flows and allows you to connect the LED the correct way. On the symbol for an LED, the cathode is on the side with the line touching the point of the triangle. An easy way to remember this is that the triangle 'points' the way the current may flow. It is important to note that there are many variations on the schematic symbol; however, they all have a triangle with a line across the point and one or two arrows pointing out.
  
 
== How to Wire up an LED ==
 
== How to Wire up an LED ==
  
To wire up an LED you need an LED, a breadboard, some wires, and a 470Ω resistor (color code: yellow-violet-brown)
+
To wire up an LED you will need an LED, a breadboard, some wires, and a 470Ω resistor (color code: yellow-violet-brown). A [[Tutorials/Arduino_Projects/Additional_Info/What_Is_a_Resistor|resistor]] is an electronics component that limits the flow of electricity. This is important because most components, (including LEDs) have a limit on how much current can flow through them before they are damaged. Resistors are used to limit the current flow and help prevent this damage.
 +
 
 +
[[image:Parts_For_Wiring_1_LED.jpg|thumb|c|center|300px|LED parts]]
 +
 
 
Wiring up an LED is very simple. For this circuit, all you need to do is connect one lead of the resistor to digital pin 5 on the Arduino. Connect the other lead of the resistor to the anode lead of one of the LEDs, and then connect the cathode to ground.  
 
Wiring up an LED is very simple. For this circuit, all you need to do is connect one lead of the resistor to digital pin 5 on the Arduino. Connect the other lead of the resistor to the anode lead of one of the LEDs, and then connect the cathode to ground.  
  
 
[[image:1_LED_Schematic_Arduino.png|thumb|c|center|300px|LED Circuit Schematic]]
 
[[image:1_LED_Schematic_Arduino.png|thumb|c|center|300px|LED Circuit Schematic]]
  
To make this easier, we will use the breadboard.
+
To make this easier, we will use the breadboard. Make sure that you have the anode and cathode of the LED on separate buses (to prevent a short) and that one of the resistor leads is in the same bus as the LED's anode.  
  
[[image:Example.png|thumb|c|center|300px|Breadboard setup for one LED]]
+
{| align="center"
 
+
=== Understanding the circuit ===
+
The Arduino digital pin in this circuit will be configured as an output, which for this setup can be thought of as connecting the circuit to the power when set to high, or disconnecting it when set to low. When it is connected the current will flow from the pin, through the resistor and through the LED, then into the ground connection.
+
For electronics, the ground connection refers to the voltage supply connection that has 0 volts. For the Arduino circuits, the voltage supply has 5 volts. However the LED requires about 2 volts across the leads not the 5 volts output from the pin. This is where the resistor comes in to play.
+
For a series circuit (the components are connected one after the other), the sum of the voltage drop across each component must be equal to the source voltage, which in this case is 5 volts. Since the LED needs 2 volts the resistor must have a voltage drop of 3 volts. Additionally, for series circuits, the current (amperes) going through each component is the same. Based on these properties it is possible to calculate the required resistance of the resistor using (Vs-V¬LED)/I¬LED=R, where Vs is the supply voltage, VLED is the voltage across the LED, ILED is the desired current flowing through the LED, and R is the resistance of the resistor. We already selected the 470Ω resistor because it provides a nice safety range for variances of the components.
+
 
+
== Programing One LED ==
+
To demonstrate how you can use an LED with the Arduino, we are going to have the Arduino make the LED flash.
+
=== Configuring ROBOTC ===
+
First thing you need to do is make a new source file in ROBOTC. Once you have done that you need to tell ROBOTC what type of Arduino you are using. Since you are using the Parallax Boe Bot Shield, you will be using the Arduino UNO. To specify the board go to '''Robot''' > '''Platform Type''' > '''Arduino Types''' > '''Standard Arduino''' then click on '''Arduino UNO'''.
+
 
+
[[image:Example.png|thumb|c|center|300px|Arduino board type menu]]
+
 
+
Now that ROBOTC knows what board you are using, it is time to configure the pins on the Arduino. To do this we open the '''Motors and Sensors Setup''' window by opening the '''Robot''' drop-down menu and clicking on '''Motors and Sensors Setup'''.
+
 
+
[[image:Example.png|thumb|c|center|300px|Opening the Motors and Sensors Setup window]]
+
 
+
Now we want to let ROBOTC know that we have the Parallax Boe Bot Shield attached, so we navigate to the '''Controller Board''' tab and under '''Optional Plugin Shields''', we want to select the '''parallax Boe Bot Shield''' from the selection box next to the 0.
+
 
+
[[image:Example.png|thumb|c|center|300px|Parallax Boe Bot Sheild configured]]
+
 
+
Now we want to tell ROBOTC to configure digital pin 5 as a Digital Output and name it '''led1'''. To do this we navigate to the '''Digital 0-13''' tab. in the text field next to "dgtl5" type in led1, then next the text field select Digital Out from the drop-down list.
+
 
+
[[image:Example.png|thumb|c|center|300px|Digital pins configured]]
+
 
+
Now that everything is configured we click '''OK'''. The Motors and Sensors Setup window will close and your source code file should contain the following code.
+
{|
+
 
|-
 
|-
|<syntaxhighlight lang="ROBOTC">
+
| [[image:BoeBot_Breadboard_1_LED_virtual.jpg|thumb|c|center|300px|Virtual breadboard setup for one LED]] || [[image:BoeBot_Breadboard_1_LED.jpg|thumb|c|center|300px|Breadboard setup for one LED]]
#pragma config(CircuitBoardType, typeCktBoardUNO)
+
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
+
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
+
#pragma config(Sensor, dgtl5,  led1,          sensorDigitalOut)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//</syntaxhighlight>
+
 
|-
 
|-
 
|}
 
|}
<br />
 
=== Programming task main() ===
 
Now that the configuration is done, it is time to add the code to make the LED flash. To do this we want the Arduino to set the output to high, pause for a moment, set the output to low, pause again then repeat. The first step to doing this is to crate a task main() to run everything.
 
{|
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
#pragma config(CircuitBoardType, typeCktBoardUNO)
 
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
 
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
 
#pragma config(Sensor, dgtl5,  led1,          sensorDigitalOut)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
  
task main()
+
=== Understanding the circuit ===
{
+
The Arduino digital pin in this circuit will be configured as an output, which means the pin will act as a 5 volt source when high and a ground when low.In electronics the ground connection refers to the voltage supply connection that has 0 volts.  
 
+
}</syntaxhighlight>
+
|-
+
|}
+
Now we want the pin to be set High and pause for a second. To do this we use the following code.
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
//turn the LED on
+
SensorValue[led1] = true;
+
  
//wait 1 second
+
The Arduino supplies 5 volts for its digital pins. However, the LED requires about 2 volts across the leads, not the 5 volt output from the pin. This is where the resistor comes in to play.
wait1Msec(1000);</syntaxhighlight>
+
|-
+
|}
+
We now need to turn the LED off then pause again. By setting the value to ''false'' instead of ''true'' in the above code, the LED will turn off. Using that information, the following code will turn the LED on for one second, then turn it off and wait another second before continuing.
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
//turn the LED on
+
SensorValue[led1] = true;
+
  
//wait 1 second
+
For a series circuit (the components are connected one after the other), the sum of the voltage drop (voltage drop - the amount of voltage used by a component) across each component must be equal to the source voltage, which in this case is 5 volts. Since the LED needs 2 volts the resistor must have a voltage drop of 3 volts. Additionally, for series circuits, the current (amperes or amps abbreviated as A) flowing through each component in the circuit is the same. Based on these properties it is possible to calculate the required resistance of the resistor using (V<sub>s</sub>·V<sub>LED</sub>)/I<sub>LED</sub>=R, where V<sub>s</sub> is the supply voltage, V<sub>LED</sub> is the voltage across the LED, I<sub>LED</sub> is the desired current flowing through the LED, and R is the resistance of the resistor. We selected the 470Ω resistor because it provides a nice safety range for variances of the components.
wait1Msec(1000);
+
 
+
//turn the LED off
+
SensorValue[led1] = false;
+
 
+
//wait 1 second
+
wait1Msec(1000);
+
</syntaxhighlight>
+
|-
+
|}
+
We want this to happen indefinitely so we surround the previous code with a ''while(true)'' loop, and insert the code into the ''task main()'' and end up with the program
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
#pragma config(CircuitBoardType, typeCktBoardUNO)
+
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
+
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
+
#pragma config(Sensor, dgtl5,  led1,          sensorDigitalOut)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
 
+
task main()
+
{
+
  while(true) //repeat indefinitely
+
  {
+
    //turn the LED on
+
    SensorValue[led1] = true;
+
 
+
    //wait 1 second
+
    wait1Msec(1000);
+
 
+
    //turn the LED off
+
    SensorValue[led1] = false;
+
 
+
    //wait 1 second
+
    wait1Msec(1000);
+
  }
+
}
+
</syntaxhighlight>
+
|-
+
|}
+
<br />
+
 
+
== Adding Another LED ==
+
=== Wiring the Second LED ===
+
The wiring for the second LED is almost identical to the first. The only difference is that instead of connecting to digital pin 5 line the first LED, the second will be connected to digital pin 6. The circuit for the two LEDs will be constructed as depicted below.
+
 
+
[[image:2_LEDs_Schematic_Arduino.png|thumb|c|center|300px|Schematic for two LEDs]]
+
 
+
[[image:Example.png|thumb|c|center|300px|Breadboard setup for two LEDs]]
+
 
+
=== Programing Two Flashing LEDs ===
+
First create a new source code file. Once you have done that you need to tell ROBOTC what type of Arduino you are using. Since you are using the Parallax Boe Bot Shield, you will be using the Arduino UNO. To specify the board go to '''Robot''' > '''Platform Type''' > '''Arduino Types''' > '''Standard Arduino''' then click on '''Arduino UNO'''.
+
 
+
[[image:Example.png|thumb|c|center|300px|Arduino board type menu]]
+
 
+
Now that ROBOTC knows what board you are using, it is time to configure the pins on the Arduino. To do this we open the '''Motors and Sensors Setup''' window by opening the '''Robot''' drop-down menu and clicking on '''Motors and Sensors Setup'''.
+
 
+
[[image:Example.png|thumb|c|center|300px|Opening the Motors and Sensors Setup window]]
+
 
+
Now we want to let ROBOTC know that we have the Parallax Boe Bot Shield attached, so we navigate to the '''Controller Board''' tab and under '''Optional Plugin Shields''', we want to select the '''parallax Boe Bot Shield''' from the selection box next to the 0.
+
 
+
[[image:Example.png|thumb|c|center|300px|Parallax Boe Bot Sheild configured]]
+
 
+
Now we want to tell ROBOTC to configure digital pin 5 as a Digital Output and name it '''led1'''. To do this we navigate to the '''Digital 0-13''' tab. in the text field next to "dgtl5" type in led1, then next the text field select Digital Out from the drop-down list.
+
 
+
[[image:Example.png|thumb|c|center|300px|Digital pins configured]]
+
 
+
Now that everything is configured we click '''OK'''. The Motors and Sensors Setup window will close and your source code file should contain the following code.
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
#pragma config(CircuitBoardType, typeCktBoardUNO)
+
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
+
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
+
#pragma config(Sensor, dgtl5,  led1,          sensorDigitalOut)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//</syntaxhighlight>
+
|-
+
|}
+
<br />
+
=== Programming task main() ===
+
Now that the configuration is done, it is time to add the code to make the LED flash. To do this we want the Arduino to set the output to high, pause for a moment, set the output to low, pause again then repeat. The first step to doing this is to crate a task main() to run everything.
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
#pragma config(CircuitBoardType, typeCktBoardUNO)
+
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
+
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
+
#pragma config(Sensor, dgtl5,  led1,          sensorDigitalOut)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
 
+
task main()
+
{
+
 
+
}</syntaxhighlight>
+
|-
+
|}
+
Now we want the pin to be set High and pause for a second. To do this we use the following code.
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
//turn the LED on
+
SensorValue[led1] = true;
+
 
+
//wait 1 second
+
wait1Msec(1000);</syntaxhighlight>
+
|-
+
|}
+
We now need to turn the LED off then pause again. By setting the value to ''false'' instead of ''true'' in the above code, the LED will turn off. Using that information, the following code will turn the LED on for one second, then turn it off and wait another second before continuing.
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
//turn the LED on
+
SensorValue[led1] = true;
+
 
+
//wait 1 second
+
wait1Msec(1000);
+
 
+
//turn the LED off
+
SensorValue[led1] = false;
+
 
+
//wait 1 second
+
wait1Msec(1000);
+
</syntaxhighlight>
+
|-
+
|}
+
We want this to happen indefinitely so we surround the previous code with a ''while(true)'' loop, and insert the code into the ''task main()'' and end up with the program
+
{|
+
|-
+
|<syntaxhighlight lang="ROBOTC">
+
#pragma config(CircuitBoardType, typeCktBoardUNO)
+
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
+
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
+
#pragma config(Sensor, dgtl5,  led1,          sensorDigitalOut)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
 
+
task main()
+
{
+
  while(true) //repeat indefinitely
+
  {
+
    //turn the LED on
+
    SensorValue[led1] = true;
+
 
+
    //wait 1 second
+
    wait1Msec(1000);
+
 
+
    //turn the LED off
+
    SensorValue[led1] = false;
+
 
+
    //wait 1 second
+
    wait1Msec(1000);
+
  }
+
}
+
</syntaxhighlight>
+
|-
+
|}
+
<br />
+

Latest revision as of 19:19, 10 October 2012

What is an LED?

An LED (Light Emitting Diode) is an electronics component that emits light when it is powered. Since it is a diode, an LED must be wired correctly for it to work (diodes only let current flow in one direction). When connecting an LED it is important to distinguish which lead is the anode (positive) and which is the cathode (negative).

To make it easier to identify the two leads, all LEDs are manufactured with two physical properties; the first is that LEDs are manufactured with one lead that is longer that the other. The longer lead is the anode(+) and the shorter lead is the cathode(-). The second feature is a small flat notch on the side of the LED. The lead that is closer to the notch is always the cathode. This is important to remember since the leads may have been clipped.

Close up of an LED and one of the possible schematic symbols for an LED.

When referencing a schematic (drawing of the electrical pathways and components using symbols), the symbol for the LED shows which way the current flows and allows you to connect the LED the correct way. On the symbol for an LED, the cathode is on the side with the line touching the point of the triangle. An easy way to remember this is that the triangle 'points' the way the current may flow. It is important to note that there are many variations on the schematic symbol; however, they all have a triangle with a line across the point and one or two arrows pointing out.

How to Wire up an LED

To wire up an LED you will need an LED, a breadboard, some wires, and a 470Ω resistor (color code: yellow-violet-brown). A resistor is an electronics component that limits the flow of electricity. This is important because most components, (including LEDs) have a limit on how much current can flow through them before they are damaged. Resistors are used to limit the current flow and help prevent this damage.

LED parts

Wiring up an LED is very simple. For this circuit, all you need to do is connect one lead of the resistor to digital pin 5 on the Arduino. Connect the other lead of the resistor to the anode lead of one of the LEDs, and then connect the cathode to ground.

LED Circuit Schematic

To make this easier, we will use the breadboard. Make sure that you have the anode and cathode of the LED on separate buses (to prevent a short) and that one of the resistor leads is in the same bus as the LED's anode.

Virtual breadboard setup for one LED
Breadboard setup for one LED

Understanding the circuit

The Arduino digital pin in this circuit will be configured as an output, which means the pin will act as a 5 volt source when high and a ground when low.In electronics the ground connection refers to the voltage supply connection that has 0 volts.

The Arduino supplies 5 volts for its digital pins. However, the LED requires about 2 volts across the leads, not the 5 volt output from the pin. This is where the resistor comes in to play.

For a series circuit (the components are connected one after the other), the sum of the voltage drop (voltage drop - the amount of voltage used by a component) across each component must be equal to the source voltage, which in this case is 5 volts. Since the LED needs 2 volts the resistor must have a voltage drop of 3 volts. Additionally, for series circuits, the current (amperes or amps abbreviated as A) flowing through each component in the circuit is the same. Based on these properties it is possible to calculate the required resistance of the resistor using (Vs·VLED)/ILED=R, where Vs is the supply voltage, VLED is the voltage across the LED, ILED is the desired current flowing through the LED, and R is the resistance of the resistor. We selected the 470Ω resistor because it provides a nice safety range for variances of the components.