Sunday, October 17, 2021

Arduino: Lesson 8 - Using LDR Sensor and reading values on Serial Monitor

 

Fig. 1 - Using LDR Sensor and reading values on Serial Monitor

Welcome to Lesson 8 - Basic Arduino Course

In today's lesson, we will learn how to use an LDR sensor and read values on a Serial Monitor with Arduino. 

To measure light intensity, we will use the famous and widely used low-cost LDR (Light Dependent Resistor) sensor, to detect the intensity of light or darkness easily and cheaply.

LDR (Light Dependent Resistor) Sensor

LDR is a special type of resistor that passes higher voltage (low resistance) when light intensity is high and low voltage (high resistance) when darkness is low. 

With this method, we can use it for example in projects such as:

  • Lack of Light Alarm - Triggers an alarm when power is cut.
  • Brightness Control - Used to control the brightness of a recording or filming environment for example.
  • Contrast Control Screen - Often used on mobile phones to automatically lower or increase the brightness of the Display.
  • Emergency Light - Automatically turns on when Light is cut.
  • Ticket Counter - Also used to count people at the entrance of an establishment.
  • Invasion Alarm - Used to trigger an alarm when there is an invasion of the monitored establishment. 

In today's example, we will use this sensor to measure numerical resistance and read these values from the serial monitor.

To do this, we will use the Arduino analog Port A0. We can use any analog Port, in case of Arduino Uno there are 6 ports; A0 to A5.

Hardware Required

  • Arduino Board
  • LDR - Sensor Dependent Light
  • 100K ohms resistor - (Brown, black, yellow, gold)
  • Jumper Wires
  • Protoboard (optional)

The Circuit Connections

The circuit is very simple, we connect one leg of the LDR to positive 5V, and another leg to the 100K resistor in series with negative GND, and the same leg that takes the resistor and LDR, we connect to port A0, as shown in Figure 2 below.

Fig. 2 - Using LDR Sensor and reading values on Serial Monitor - tinkercad.com

We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino.

The Code

The analogReads() function, reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023

On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit

After building the circuit, connect your Arduino board to your computer, launch Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line. 

  • In Line 3, we declared sensorPin to select the analog input Pin A0 to connect the LDR.
  • In Line 4, we declared sensorValue as a variable to store the value coming from LDR sensor.

  • In Line 6we enter the void setup() function. This function is read only once when the Arduino is started.
  • In Line 7, we sets the serial port for communication, we will read the numeric value from LDR sensor. 
1
2
3
4
5
6
7
8
9
// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino

int sensorPin = A0;        // Select the Analog input pin for LDR
int sensorValue = 0;       // Variable to store the value coming from the sensor

void setup() {                // This function is called once when the program starts
 Serial.begin(9600);       // Sets Serial Port for communication with bounce rate in 9600
}
//------------------------------------- www.elcircuits.com --------------------------------------------
  • In Line 10, we enter in the loop() function does precisely what its name suggests, and loops consecutively.

  • In Line 11, the sensorValue variable, receives the value read from sensorPin, which is the analog input Pin, and stores the read values.

  • In Line 12, the Serial.println() function, prints the values from the variable sensorValue on the serial monitor screen.
1
...
10
11
12
13
14
// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino

void loop() { // The loop function runs over and over again as long as the Arduino has power.
sensorValue = analogRead(sensorPin);    // Read the value from the sensor
Serial.println(sensorValue);                      // Prints the values coming from the sensor on the Serial Monitor
}
//------------------------------------- www.elcircuits.com --------------------------------------------
Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to Arduino.

The complete code is showed in the sketch below!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino

int sensorPin = A0;        // Select the Analog input pin for LDR
int sensorValue = 0;       // Variable to store the value coming from the sensor

void setup() {               // This function is called once when the program starts  
 Serial.begin(9600);      // Sets Serial Port for communication with bounce rate in 9600
}

void loop() { // The loop function runs over and over again as long as the Arduino has power.
sensorValue = analogRead(sensorPin);     // Read the value from the sensor
Serial.println(sensorValue);                       // Prints the values coming from the sensor on the Serial Monitor
}
//------------------------------------- www.elcircuits.com --------------------------------------------

All ready! After you have assembled the entire circuit, and uploaded this code, open the Serial Monitor and what you will see is the numerical value of the LDR.

When you approach the hands of the LDR inhibiting the Light, the number will drop to the minimum possible, it will depend on the LDR, and when you shine a light on the sensor, the number will go to the maximum of the sensor.
With that, the possibilities are immense, to work with this sensor.

Next Lesson

Previous Lesson

If you have any questions, suggestions or corrections, please leave them in the comments and we will answer them soon.

Subscribe to our blog!!! Click here - elcircuits.com!!!

My Best Regards!!!

No comments:

Post a Comment