LEDs interfacing with Arduino

Description

LEDs are connected to PORTD (pin0 to pin7) of Arduino. Write a code to toggle the LEDs continuously one by one with a delay of 250ms (1/4 second).

I assume that you have gone through my previous blog posts LED Interfacing and how to manipulate a single bit of Arduino.

Now in this blog, I will be manipulating 8 pins of Arduino. Manipulating a bit means controlling a bit. making it HIGH(1) and LOW(0). To see the effect I will be connecting LEDs to each one of the 8 pins to Arduino. I will connect the cathode to the ground. so by applying HIGH logic, LED will be turned ON and by applying the LOW logic LED will be turned OFF.

As I stated in my previous post that we need to connect a resistor in between LED and Arduino Port Pin. I am using a red LED, I will be connecting a resistor value of 1K ohm. A resistor value of 220ohm will be good if you want to glow LED with a little bit greater intensity.

Required components

  • Arduino UNO
  • Breadboard
  • Jumper wires
  • 8 – Red LED(LED Module with 8 LEDs)
  • 8 – 1k Resistor
  • Power supply or DC Adapter

Where to place a resistor (at the cathode or at the anode side )?

In our circuit, it does not matter either way you can connect. The reason is we are connecting a resistor in series with the LED, and in series connection same current will flow through a resistor as well as LED.  But it should be present between MCU Pin and LED terminal if you don`t want to damage your LED. I have connected resistors to the Anode of LEDs.

Connection diagram

leds8_atportdone_by_one_toggeling

Led module

ledmodule

In this module which I have made cathodes of all LEDs are shorted and connected to one common lead which is at the bottom left. Anode of all the LEDs is given to separate pinout so just by applying HIGH Logic to these pins and a common terminal connected to the ground, LED will be turned ON and by applying LOW Logic LED will be turned OFF.
LED is an OUTPUT device. so first we need to declare 8 pins (as we want to connect 8 LEDs to Arduino pins) as an OUTPUT.
Now I will be doing setting the bit and resetting the individual bit after some interval of time. which means I need to have a little bit of delay so that I can see the LED glowing one by one.

Complementing logic(high and low) I will be doing inside loop function, so continuously it will turn on and turn off led after the delay which I have provided in code. First I will toggle(turning on and off) the first LED, then second, then third and after toggling the 8th LED again first LED will toggle. This operation will be repeated the Arduino is powered ON.

Code

Code description

Line 01: unsigned int i; Declared one integer to use in the iteration.

Line 05 to 08: pinMode(i,OUTPUT);  Declared pin 0 to 7 as output to connect LEDs.
Line 15: PORTD = (1 << i); Giving value directly to PORTD (pins 0 to 7 of arduino) by doing left shift logic.

Line 16: delay(250); Gave a delay of 1/4 of a second(250ms).

Iteration Execution

First Iteration:
i = 0;
PORTD = 1 << 0; PORTD = 00000001;
LED connected at Pin 0 of Arduino will be turned ON.

Second Iteration:
i = 0;
PORTD = 1 << 1; PORTD = 00000010;

LED connected at Pin 1 of Arduino will be turned ON.

Third Iteration:
i = 0;
PORTD = 1 << 2; PORTD = 00000100;

LED connected at Pin 2 of Arduino will be turned ON.

Fourth Iteration:
i = 0;
PORTD = 1 << 3; PORTD = 00001000;

LED connected at Pin 3 of Arduino will be turned ON.

Fifth Iteration:
i = 0;
PORTD = 1 << 4; PORTD = 00010000;

LED connected at Pin 4 of Arduino will be turned ON.

Sixth Iteration:
i = 0;
PORTD = 1 << 5; PORTD = 00100000;

LED connected at Pin 5 of Arduino will be turned ON.

Seventh Iteration:
i = 0;
PORTD = 1 << 6; PORTD = 01000000;

LED connected at Pin 6 of Arduino will be turned ON.

Eighth Iteration:
i = 0;
PORTD = 1 << 7; PORTD = 10000000;

LED connected at Pin 7 of Arduino will be turned ON.

Video

 

Leave a Comment

Your email address will not be published. Required fields are marked *