How to Use Photoresistor with Arduino: Simple Guide
To use a
photoresistor with Arduino, connect it in a voltage divider circuit with a fixed resistor and read the analog value using analogRead(). This value changes with light intensity and can be used to control outputs or monitor light levels.Syntax
Use the analogRead(pin) function to read the voltage from the photoresistor circuit connected to an analog pin. The photoresistor is wired with a fixed resistor to form a voltage divider, which converts light changes into voltage changes.
pin: The analog input pin number where the photoresistor circuit is connected.analogRead(): Reads the voltage level and returns a value from 0 (0V) to 1023 (5V).
arduino
int sensorValue = analogRead(A0);Example
This example reads the photoresistor value from analog pin A0 and prints the light level to the Serial Monitor every second.
arduino
const int photoPin = A0; void setup() { Serial.begin(9600); // Start serial communication } void loop() { int lightLevel = analogRead(photoPin); // Read light level Serial.print("Light Level: "); Serial.println(lightLevel); // Print value delay(1000); // Wait 1 second }
Output
Light Level: 523
Light Level: 520
Light Level: 518
... (values vary with light)
Common Pitfalls
- Incorrect wiring: The photoresistor must be connected in series with a fixed resistor to form a voltage divider; connecting it directly to power or ground will not work.
- Wrong analog pin: Use an analog input pin (A0, A1, etc.), not a digital pin.
- No Serial Monitor: Forgetting to open the Serial Monitor at 9600 baud will show no output.
- Ignoring ambient light: Light levels vary widely; calibrate your code to your environment.
arduino
/* Wrong wiring example (photoresistor directly to 5V and A0 without resistor) */ // This will not give correct readings /* Correct wiring example */ // Connect photoresistor between 5V and A0 // Connect 10k resistor between A0 and GND // Then use analogRead(A0) to get values
Quick Reference
- Connect photoresistor and 10k resistor as voltage divider: 5V - photoresistor - A0 - resistor - GND
- Use
analogRead(A0)to get light level (0-1023) - Print or use values to control LEDs or other devices
- Open Serial Monitor at 9600 baud to see readings
Key Takeaways
Connect the photoresistor in a voltage divider with a fixed resistor to an analog pin.
Use analogRead() to get a value that changes with light intensity.
Always use an analog input pin, not a digital pin, for reading the sensor.
Open the Serial Monitor at 9600 baud to view sensor readings.
Calibrate your code to your environment's light levels for best results.