Project 8.00 Light Sensor

This Arduino sketch reads the light intensity from a light sensor connected to analog pin A2 and prints the values to the serial monitor!
In this project you’ll learn what an LDR is and how to analogRead it to determine light intensity.

Project Code:

				
					///////////////////////////////////////////////////
//8.00 - Reading Light Intensity

byte lightSensorPin = A2;

void setup() {
  pinMode(lightSensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.print("The light level is at: "); 
  Serial.println(analogRead(lightSensorPin));
  delay(1000);
}

///////////////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

Reading the LDR is exactly the same as reading the potentiometer.

The pin that is connected to the LDR is an analog pin.

				
					byte lightSensorPin = A2;
				
			

The LDR is going to have an analog voltage, which is read using analogRead. It is then printed out onto the Serial monitor. Make sure and open the Serial port to see the data being printed.

				
					Serial.print("The light level is at: "); Serial.println(analogRead(lightSensorPin));
				
			

There is then a delay to keep the Serial port from being flooded with messages.

				
					delay(1000);
				
			
The LDR will read more voltage the darker it is. We can use this information to control outputs based on the light intensity.

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.