LED diodes – how to flash and light

Recently there was silence on Starter Kit, because before holidays I was preparing to Bootstrap 9.4, what took most of my free time. Now, taking advantage of free day I’m making up for it.

Previously, we were dealing with reading from external sensor (photoresistor). Now we will try to show this reading.

We will use 7 LED diodes arranged in a line making a kind of indicator – the more light falls on the photoresistor, the more diodes will light. Beginning from the end – how it looks in action:

Arduino’s Eye from Starter Kit on Vimeo.

Forgive poor quality, but mobile phone camera is the only tool I can use to perpetuate my experiments with Arduino.

But despite of that the effect is visible. Behind the frame I zoom in and out the photoresistor to a lamp as the diodes are lighting up and fading out.

LED diodes are mounted directly to the breadboard with cleverly bended and cut terminals so they are arranged in a line and sitting firmly in the board.

LED diode mounted on breadboard
LED diode mounted on breadboard

The photo is slightly unsharp (again because of the mobile phone) but you can see in which way the diode is placed. In one of the lower rows of holes the ground is connected and this connection is shared by all diodes. The next photo shows exactly, how the terminals are bended.

Single diode prepared to mounting
Single diode prepared to mounting
Circuit diagram
Circuit diagram

The ground from Arduino (GND) becomes connected to the lower row on breadboard. Voltage divider with photoresistor is placed between ground and supply from Arduino. As the second resistor a 5.6 kΩ is used.

Taking into account that the resistance of my photoresistor varies from hundreds Ω to a few MΩ, voltage drop on 5.6 kΩ resistor would vary from almost 5V to just over 0V, thus the readings from analog input would be in full scale from 0 to 1023.

All the diodes are connected to the ground rail. Every diode is wired to digital output through a resistor. The resistor’s role is to limit current flowing through the diode. It’s not about safety of the diode, it’s about safety of Arduino digital output.

CODE:

// pin 1 isn't used
// when the value is 0 - it shouldn't light
int leds[] = {-1,7,8,2,3,4,5,6};
int photoPin = 0;
int size = 8;
int val = 0;

void setup()
{
  //setting
  for (int i=1; i< size; i++) {
    pinMode(leds[i], OUTPUT);
  }
}

void loop()
{
  val = analogRead(photoPin);
  val = val/128;
  for (int i=1;i<size;i++) {
    if (i <= val ) {
      digitalWrite(leds[i], HIGH);
    } else {
      digitalWrite(leds[i], LOW);
    }
  }
  delay(200);
}

leds is array containing digital outputs numbers to which the diodes are connected in sequence. Because the array is numbered starting from 0, entering non-existent output value and using the table index always from 1 cause turning off all the diodes when the light intensity is lowest.

Our display has 8 states (from 1 to 7 and ma 8 zero state with all diodes out), so we divide the reading from analog input by 128, what gives result from 0 to 7, which describes number of diodes to be lighted.

Summary

It’s first of the articles about LED diodes. One of the following will be about controlling LEDs in number higher than 14. In this case controlling one diode with one digital output, what was presented above, is no more effective.