Automatic Night Light
Part of the Inputs section of Coddy's Arduino journey. Lesson 13 of 22.
A photoresistor is a resistor whose value changes with light: the more light falls on it, the lower its resistance. This one sits on a small module with four pins: VCC and GND for power, AO (analog out) and DO (digital out).
AO gives a voltage you read with analogRead(). On this module it falls as the light rises: about 1023 in the dark, close to 0 in bright light. So a big reading means dark.
int reading = analogRead(A0);
if (reading > 600) {
// dark
}On the board, the slider on the sensor sets how much light falls on it.
Challenge
EasyThe light module's AO is on A0 and an LED on pin 13. Make a night light:
- read A0 and print the reading, then wait 100 ms
- light the LED when the reading is above 600 (dark), otherwise turn it off
Press Run, move the light slider, then Check.
Try it yourself
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the light module on A0 and print the reading
// light the LED when it is dark (reading above 600)
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Inputs
Practice on your own: Arduino playground