# Light intensity indicator with 7-segment LED display

Contents

This is similar to the other light intensity indicator tutorial. In this tutorial, the 7-segment LED display will be used to display the intensity of the light received by the photo resistor, mapping the light intensity values from 1 to 9.

The simpler version of the light intensity indicator tutorial: LED Light intensity indicator

## Overview

Well, the resistance of the photoresistor will change according to the amount of light it receives. To determine the change, the photoresistor will be connected to arduino’s analog pin to measure the value of the photoresistor, which I would be using pin A0 for this tutorial. Therefore, when the photoresistor receives more light, the value received by the arduino will be higher (since more light decreases the resistance in the photoresistor), and vice versa.

How do I get the reading from the photoresistor? Use the analogRead() function! Firstly, we would declare an integer variable, lightLevel, to store the reading from the photoresistor. Next, I will map the the photoresitor value from its highest & lowest value to 0 – 9, since I would only want to display the intensity with that range (0 to 9). I will also print the mapped value in the serial monitor for debugging purposes. Finally, the 7 segment display will display the respective number according to the lightLevel value.

For the 7 segment part, I hard-code the ouput of the 7 segment, hence the code appears to be very long.

## Parts

• Arduino Uno x 1
• Jumper Wires
• 7-Segment LED Display x 1
• 330Ω resistors x 7
• 10KΩ resistors x 1
• LDR x 1

## Code

int lightPin = 0; //Photoresistor Pin

void setup() { //set outputs and inputs
pinMode(12, OUTPUT); //a
pinMode(11, OUTPUT); //b
pinMode(10, OUTPUT); //c
pinMode(9, OUTPUT); //p
pinMode(8, OUTPUT); //f
pinMode(7, OUTPUT); //g
pinMode(6, OUTPUT); //e
pinMode(5, OUTPUT); //d
Serial.begin(9600); // Monitor
}

void loop() {

lightLevel = map(lightLevel, 410, 640, 0, 9);
Serial.println(lightLevel);
int l = lightLevel;
if(l==0){zero();}
if(l==1){one();}
if(l==2){two();}
if(l==3){three();}
if(l==4){four();}
if(l==5){five();}
if(l==6){six();}
if(l==7){seven();}
if(l==8){eight();}
if(l==9){nine();}
}
void one(){
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
delay(250);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}

void two(){
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void three(){
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(7, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(7, LOW);
digitalWrite(5, LOW);
}
void four(){
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(250);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
}
void five(){
digitalWrite(12, HIGH);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(5, LOW);
}
void six(){
digitalWrite(12, HIGH);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void seven(){
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);

}
void eight(){
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void nine(){
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(5, LOW);
}
void zero(){
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(8, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}