The photoresistor, also known as a light-dependent resistor (LDR), is a versatile sensor that changes its resistance based on the amount of light it detects. This tutorial will guide you through integrating a photoresistor with your Arduino, enabling you to create light-reactive projects!
How It Works
- The photoresistor is connected in a voltage divider circuit with the 10 KOhm resistor.
- As light intensity changes, the resistance of the photoresistor varies.
- The analog value from the photoresistor is read using
analogRead()
. - If the value exceeds a threshold (e.g., 25), the LED turns on; otherwise, it remains off.
Components
Arduino Uno
Breadboard
Photoresistor Module
Led
Resistor 10K
Jumper wires
Connection Diagram
The Code!
This line declares an integer variable named led and initializes it with the value 13.
int led = 13;
First we define all the pins that we are going to use. In our case we have only one led defined to the pin 13
int led = 13; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); } void loop() { int value = analogRead(A0); Serial.println("Analog value : "); Serial.println(value); if (value > 50) { digitalWrite(led, LOW); } else { digitalWrite(led, HIGH); } delay(250); }