A photoresistor on Arduino Uno
Steps done: 0 of 5
Guess the answer and press Run
light, lux300A0856time, s0.0
Lesson steps
What will happen to the number under your hand?
The lamp is on, and the monitor prints about 860. Pick an answer before you run it.
Sketch
// Photoresistor from 5V to A0, 10 kΩ resistor from A0 to GND
// Night light: LED on D9 through 220 Ω
const int sensor = A0;
const int led = 9;
int darkAt = 500; // the night light is on below this level, 0-1023
int bright = 255; // night light brightness, 0-255
int waitMs = 200; // pause between readings, ms
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(sensor);
Serial.println(light);
if (light < darkAt) {
analogWrite(led, bright);
} else {
analogWrite(led, 0);
}
delay(waitMs);
}
Tap a line or an orange number
Serial monitor
Serial, 9600 baudboard time 0.0 s
About the lesson
A GL5528 photoresistor and a 10 kΩ resistor divide 5 V, the middle of the divider goes to A0. A night light LED on D9 turns on when the reading is below a threshold. You move the lamp on the scene with a slider, a tap on the photoresistor covers it with a palm. About 12 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.
Build it for real
Parts
- Arduino Uno R3
- Mini breadboard
- GL5528 photoresistor (5 mm)
- 10 kΩ resistor
- 5 mm LED
- 220 Ω resistor
- 5 male-to-male jumper wires
Wires
| From | To |
|---|---|
| Photoresistor pin | 5V |
| Second photoresistor pin and the 10 kΩ resistor | A0 |
| Second pin of the 10 kΩ resistor | GND |
| D9 | 220 Ω resistor, then the LED anode |
| LED cathode | GND |
The threshold depends on the photoresistor and the room: look at the numbers in the serial monitor in the light and at dusk, and set darkAt between them.
Open the schematic in the builderPinout of the lesson boardPhotoresistor pinoutLED pinout
The sketch below goes to the board with the default numbers: darkAt 500, bright 255, waitMs 200. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.
The whole sketch
// Photoresistor from 5V to A0, 10 kΩ resistor from A0 to GND
// Night light: LED on D9 through 220 Ω
const int sensor = A0;
const int led = 9;
int darkAt = 500; // the night light is on below this level, 0-1023
int bright = 255; // night light brightness, 0-255
int waitMs = 200; // pause between readings, ms
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(sensor);
Serial.println(light);
if (light < darkAt) {
analogWrite(led, bright);
} else {
analogWrite(led, 0);
}
delay(waitMs);
}
What to keep in mind on a real board
Photoresistors of the same make vary by 10-20 kΩ at 10 lux. Pick the threshold for yours by looking at the serial monitor.
The night light must not shine on its own photoresistor. Otherwise it turns on, lights the sensor, and goes off, over and over. Put them apart or shield the LED from the sensor side.
If the numbers grow in the dark, the photoresistor and the 10 kΩ resistor are swapped: the photoresistor must go to 5V.
The sketch takes 2466 bytes of flash and 194 bytes of RAM.
Licenses of the lesson code
The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.
The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.
MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.