Arduino Light-Triggered LED: a Beginner Circuit Design With Hysteresis
by smartrobotlabs in Circuits > Arduino
41 Views, 0 Favorites, 0 Comments
Arduino Light-Triggered LED: a Beginner Circuit Design With Hysteresis
Can a small light sensor decide when an LED should turn on? This Arduino light-triggered LED activity helps young makers explore input, decisions, and output, with a useful extra idea: two switching thresholds instead of one.
This is a circuit-design walkthrough for an UNO R3, with an original schematic, a complete sketch, calibration instructions, and a prediction activity for ages 10–12 with an adult. The logic has been checked with scripted input values. We have not built this circuit on physical hardware or run this sketch in Tinkercad; the pictures are design illustrations, not photographs of a finished build.
You can recreate the design in a circuit simulator before trying hardware. No book purchase is needed to follow the activity.
Supplies
For a virtual version: a computer and an Arduino-compatible circuit simulator with an UNO R3, photoresistor, resistors, and LED. Tinkercad Circuits is one option.
For an optional physical build: 1 Arduino UNO R3; USB data cable; 1 photoresistor (LDR); 1 fixed 10k-ohm resistor; 1 standard indicator LED; 1 330-ohm resistor; solderless breadboard; jumper wires; Arduino IDE.
Adult supervision is recommended. Disconnect USB power before changing wiring. This is a low-voltage learning circuit, not a controller for mains lamps or a safety device. Hardware is not supplied.
Predict What the Light Sensor Will Read
A photoresistor changes resistance with light. It needs a second resistor to turn that change into a voltage for A0.
Our orientation is 5V → LDR → A0 junction → 10k-ohm resistor → GND. More light lowers the LDR resistance and raises the A0 voltage. Covering it raises the resistance and lowers the voltage.
On an UNO R3 with its default 5V reference, analogRead returns an integer from 0 to 1023. This is a relative reading, not a calibrated lux measurement.
Before wiring, ask the learner: will the number increase or decrease when the sensor is covered? Write the prediction down.
Wire the Two Circuit Branches
Disconnect power first. Recreate the schematic in your simulator or on a breadboard.
INPUT: connect one LDR leg to 5V. Join the other leg to A0 and one end of the 10k-ohm resistor. Connect the remaining resistor end to GND. The A0 connection is the junction, not a separate resistor in series with the input pin.
OUTPUT: connect D9 to the 330-ohm resistor, then to the LED anode (+). Connect the LED cathode (−) to GND. For an unused standard LED, the longer leg is normally the anode; confirm the part markings if its legs have been cut.
Use the UNO GND for both branches. Breadboard rows and split power rails vary: check which holes are actually connected. Ask an adult to check for 5V-to-GND shorts before connecting USB.
Add the Complete Arduino Sketch
Create a new sketch for an Arduino UNO R3. Copy the code below, or open the attached NightLight_Hysteresis.ino file. In a simulator, use its text-code mode. On hardware, choose the correct board and port in Arduino IDE before uploading. Open Serial Monitor at 9600 baud.
The example thresholds 350 and 550 are starting points, not universal light levels.
const int SENSOR_PIN = A0;
const int LED_PIN = 9;
const int DARK_ON = 350;
const int BRIGHT_OFF = 550;
bool ledOn = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
}
void loop() {
int reading = analogRead(SENSOR_PIN);
if (reading < DARK_ON) ledOn = true;
else if (reading > BRIGHT_OFF) ledOn = false;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
Serial.print(reading);
Serial.print(" LED=");
Serial.println(ledOn ? "ON" : "OFF");
delay(100);
}
Downloads
Calibrate Instead of Guessing
Run your recreated circuit and record ten readings in bright conditions and ten while covering the LDR. In a simulator, change its light control instead.
For this wiring, dark readings should be lower. If the readings do not change, check the A0 junction, shared ground, breadboard rows, and simulator light setting.
Choose DARK_ON above your dark reading range, then choose BRIGHT_OFF higher than DARK_ON but below the bright reading range. Example only: if dark readings stay near 150–220 and bright readings near 700–800, 350 and 550 are reasonable starting thresholds. These are illustrative values, not measurements from a build.
If the dark and bright ranges overlap, improve the lighting contrast or sensor placement before choosing thresholds. Never treat the ADC number as lux.
Explore the Memory Between Two Thresholds
Below DARK_ON, the LED turns on. Above BRIGHT_OFF, it turns off. Between the thresholds, it keeps its previous state. This is called hysteresis. It reduces rapid switching near one boundary; it does not eliminate every source of flicker or sensor noise.
Prediction exercise with thresholds 350 and 550: start OFF and imagine the readings 700, 350, 349, 450, 550, 551, 450, 100, 900. The expected states are OFF, OFF, ON, ON, ON, OFF, OFF, ON, OFF.
At exactly 350 or 550, this sketch retains the previous state because it uses strict < and > comparisons. Ask the learner why the same reading of 450 can sometimes mean ON and sometimes OFF.
Those nine software state transitions were checked with a small scripted model. They do not replace compiling the sketch, running a circuit simulation, or checking physical hardware.
Troubleshoot and Extend the Lesson
LED never lights: check its polarity, D9 wiring, current-limiting resistor, and whether the sensor reading ever drops below DARK_ON.
LED stays on: check whether the reading ever rises above BRIGHT_OFF. Recalibrate rather than assuming the sensor is faulty.
Behavior is reversed: verify that the LDR is on the 5V side and the fixed resistor is on the GND side. Swapping them reverses the voltage trend.
Fast switching: widen the gap between thresholds, keep the LED from shining directly onto the LDR, and check loose connections.
For a parent or teacher: ask the learner to draw input → decision → output and explain why the program needs to remember its state. Change one threshold at a time and record predictions and observations.
Further reading: Arduino Documentation, Analog Input: https://docs.arduino.cc/built-in-examples/analog/AnalogInput/
About the author: Smart Robot Labs creates the Arduino Robotics for Kids book series for ages 10–12. This standalone activity is free to follow and is not a copied chapter. Text and schematic were prepared with AI assistance and editorial review. No physical build or Tinkercad run is claimed.
Optional resources from Smart Robot Labs: free Arduino guide and optional paid English-language books.