LED Interfacing with Arduino

ARDUINO Tutorial Series

LED interfacing using an ARDUINO UNO

Arduino Talks With LED

Contributed By:- Vedant Patil

-----------------------------------

LED basics

An LED is a two terminal device as shown in figure

     

 

Functioning of LED

  • LED only works when connected in forward bias mode. Thus LED needs LOGIC 1 on anode and LOGIC 0 on cathode to TURN “ON”.
  • An LED can withstand only 20mA of maximum current. Normally it starts functioning at a current of 2.5mA to 3mA.
  • Normally we use a 1kΩ resistor along with an LED to reduce the current.

 

LED interfacing in Arduino

          We can connect LEDs in two ways with an Arduino

  • Common cathode mode

In this mode the anode terminal of the LED is connected to the analog/digital pin of the Arduino and the cathode terminal is supposed to be grounded. As we discussed earlier that the LED only TURN ON when a logic 1 on the anode and 0 on the cathode .

In this case, 0 on the cathode is already done so when we provide  logic 1 to the anode LED  will turn ON and for 0 it turnedOFF.

 

  • Common anode mode

In this mode the cathode terminal of the LED is connected to the analog/digital pin of the Arduino anode terminal is supposed to be connected to source voltage. As we discussed earlier that the LED only TURN ON when a logic 1 on the anode and 0 on the cathode.

In this case, 1 on the anode is already done so when we provide logic 1 to the cathode, LED  will turn OFF and for 0  it turnedON.

Note:- We generally prefer common cathode mode.

 

TASK1

Write a program to blink an LED in infinite loop connected with P1of ARDUINO UNO.

Compiler:- Aruduino IDE1.8.3

Source code

void setup()

{

pinMode(1,1); //TO DECLARE PIN1 AS OUTPUT

}

void loop()

{

digitalWrite(1,1);//LED ON STATE

delay(500);          //DELAY OF 0.5s

digitalWrite(1,0);//LED OFF STATE

delay(500);

}

 

TASK2

To blink 8 LEDs in Even Odd Alternate pattern

Source code:

//PROGRAM TO BLINK ALTERNATE LEDs

//EVEN-ODD LED BLINKING IN ARDUINO UNO

 

void setup()

{

int i;                //DECLARE VARIABLE I HERE

for(i=0;i<8;i++)      // FOR LOOP FOR PINMODE

{

pinMode(i,1);         // DECLARE PIN0-7 AS OUTPUT

}                     //FOR LOOP TERMINATED

}

void loop()

{

 

for(int i=0;i<8;i=i+2) //TO BLINK EVEN LEDs

{

digitalWrite(i,1);     //ON STATE

delay(500);            //0.5s DELAY     

digitalWrite(i,0);     //OFF STATE  

delay(500);

}

for(int i=1;i<8;i=i+2) //TO BLINK ODD LEDs

{

digitalWrite(i,1);

delay(500);

digitalWrite(i,0);

delay(500);

}

}

 

CLICK HERE TO VIEW / DOWNLOAD ARDUINO TRAINING SYLLABUS

 

CLICK HERE TO REGISTER NOW FOR ARDUINO TRAINING

 

 

 

 

Top