ECE 110/Equipment/LED

From PrattWiki
Jump to navigation Jump to search

Introduction

"Display" may be a strong term here - this page looks at how to control a single LED. Typically this would mean connecting a series combination of an LED and a resistor to a digital output pin. You could also connect one to an analog output pin if you want a PWM signal to change the brightness.

The Arduino itself has a built-in LED but that is not replicated on the CX-Bot; the CX-Bot has a built in RGB LED you can use, though its operation is a little more complicated than a simple LED.


Leads

An LED will turn on when the voltage across it is sufficient to cause current to flow through the semiconductor out of which it is made. The greater the current, the brighter the light. Typical LEDs need between 1.5 and 2.5 V to turn on. Given that the CX-Bot outputs are at 5 V when high, you will need a resistor in series to protect the LED from cooking itself.

  • Longer lead / anode - connected to V+
  • Shorter lead / cathode - connected to V-

Operation

Pick a digital IO pin and connect it to a series combination of a resistor (220-1000 $$\Omega$$ generally works) and an LED. Make sure the long lead / anode is connected to the higher voltage side. You can have two elements in whatever order you want in the series combination.

Sample Code

Simple

The Blink example works well here. Connect a resistor between pin 13 and a row of the breadboard, connect the long lead to that row of the breadboard and some other row, then connect that second row to ground. Run the Blink example - note that the LED will strobe initially because the built-in LED (which is connected to 13) lights up to indicate when information is being sent to the Arduino.

2-Bit Counter (Brute Force)

Here is code that uses two lights; a green light connected to pin 5 will be the MSB or 2s digit and a red light connected to pin 4 will be the LSB or 1s digit. Connect a circuit with a resistor and LED in series to the relevant pin, making sure the other end is connected to ground.

#define RED 4
#define GRN 5

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GRN, OUTPUT);
}

void loop() {
  digitalWrite(GRN, LOW);
  digitalWrite(RED, LOW);
  delay(1000);
  digitalWrite(GRN, LOW);
  digitalWrite(RED, HIGH);
  delay(1000);      
  digitalWrite(GRN, HIGH);
  digitalWrite(RED, LOW);
  delay(1000);      
  digitalWrite(GRN, HIGH);
  digitalWrite(RED, HIGH);
  delay(1000);
}

Notes

  • Brightness does not change linearly with the channel value!

References