Build it step by step
0 of 7 doneFit the LED to the servo horn
With tape and a loop of slack in thin wires.
Build the light divider
LDR from 5V to A0 with the 10 kilohm resistor from A0 to GND.
Wire the pot
Outer legs to 5V and GND, middle to A1.
Upload the sketch
In a bright room the lamp stays off.
Cover the LDR
The lamp comes on and the beam sweeps.
Turn the pot
The sweep gets faster or slower.
Uncover the LDR
The lamp switches off.
How do you build a model lighthouse?
A bright LED on a servo sweeps a beam round a model lighthouse, and a light sensor switches the lamp on at dusk and off at dawn. A knob sets the speed of the sweep. It is a charming night light for a shelf. Do not look straight into a bright LED. You need Arduino Nano, SG90 Micro Servo, LDR Photoresistor, Resistor 10 kΩ, LED 5 mm red, Resistor 220 Ω, Potentiometer 10 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 70 minutes.
- Level
- Beginner
- Time
- About 70 min
- Parts
- 9
Parts list
- Development BoardsBuyArduino Nano × 1
- Motors & ActuatorsBuySG90 Micro Servo × 1
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PassivesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Servo signal (orange) | Nano D9 | Swings the lamp |
| Servo red and brown | Nano 5V and GND | Servo power, a very light load only |
| LED long leg | Nano D6 through a 220 ohm resistor, short leg to GND | The lamp, fixed to the servo horn |
| LDR leg 1 | Nano 5V | Top of the light sensor divider |
| LDR leg 2 | Nano A0 and one leg of the 10 kilohm resistor | Reads the light level |
| 10 kilohm resistor other leg | Nano GND | Bottom of the divider |
| Pot outer legs | Nano 5V and GND | Speed knob supply |
| Pot middle leg | Nano A1 | Sets the sweep speed |
Code
The light sensor and the 10 kilohm resistor make a voltage divider: in daylight the reading is high, and at night it is low. The sketch uses two thresholds, 300 to switch on and 450 to switch off, so a reading hovering around one value at dusk does not make the lamp flicker. At night the LED comes on and the servo steps from 20 to 160 degrees and back, one degree at a time, with a pause of one and a half seconds, with the lamp off, at each end of the sweep. The pot sets how long each step takes, so the beam is faster or slower. Fix the LED to the servo horn with tape, using thin flexible wires and a loop of slack so nothing is pulled, and keep the LDR out of the beam so the lamp does not turn itself off; mounting it on the back of the base works well. Use a card cone around the LED to make a beam, and do not look straight into the LED. Adjust the two thresholds for your room by watching the readings on the serial monitor.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Servo.h>
const uint8_t SERVO_PIN = 9;
const uint8_t LED_PIN = 6;
const uint8_t LDR_PIN = A0;
const uint8_t POT_PIN = A1;
const int DARK_BELOW = 300; // lamp on when the reading falls below this
const int LIGHT_ABOVE = 450; // lamp off when it rises above this (stops flicker at dusk)
const int MIN_ANGLE = 20;
const int MAX_ANGLE = 160;
const unsigned long GAP_MS = 1500;
Servo lamp;
int angle = MIN_ANGLE;
int dir = 1;
bool night = false;
unsigned long lastStep = 0;
unsigned long gapUntil = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
lamp.attach(SERVO_PIN);
lamp.write(angle);
Serial.begin(9600);
}
void loop() {
int light = analogRead(LDR_PIN);
if (!night && light < DARK_BELOW) { night = true; Serial.println("Dusk: lamp on"); }
if (night && light > LIGHT_ABOVE) { night = false; Serial.println("Dawn: lamp off"); }
unsigned long now = millis();
if (!night) {
digitalWrite(LED_PIN, LOW);
return;
}
if (now < gapUntil) { // pause between sweeps with the lamp dark
digitalWrite(LED_PIN, LOW);
return;
}
digitalWrite(LED_PIN, HIGH);
unsigned long stepEvery = map(analogRead(POT_PIN), 0, 1023, 8, 60); // knob sets speed
if (now - lastStep >= stepEvery) {
lastStep = now;
angle += dir;
lamp.write(angle);
if (angle >= MAX_ANGLE || angle <= MIN_ANGLE) {
dir = -dir;
gapUntil = now + GAP_MS;
}
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Bright room | Lamp off, servo still |
| Dark room | Lamp on, beam sweeping |
| Pot turned | Sweep speed changes |
| End of each sweep | A short dark pause |
| Serial monitor | Prints dusk and dawn |
If it does not work
Lamp never turns on
Check the LDR divider and try raising DARK_BELOW.
Lamp flickers at dusk
Widen the gap between the two thresholds.
Servo jitters or the Nano resets
The load is too heavy, use a lighter servo load or a separate 5V supply.
Lamp turns off by itself
Its own light is reaching the LDR, shield the sensor.
Sweep never changes speed
Check the pot middle leg goes to A1.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the model lighthouse?
Arduino Nano, SG90 Micro Servo, LDR Photoresistor, Resistor 10 kΩ, LED 5 mm red, Resistor 220 Ω, Potentiometer 10 kΩ, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the model lighthouse take to build?
About 70 minutes for a beginner maker, in 7 steps.
How do I know the model lighthouse works?
Bright room: Lamp off, servo still. Dark room: Lamp on, beam sweeping. Pot turned: Sweep speed changes.
Lamp never turns on: what should I check?
Check the LDR divider and try raising DARK_BELOW.
Lamp flickers at dusk: what should I check?
Widen the gap between the two thresholds.
Servo jitters or the Nano resets: what should I check?
The load is too heavy, use a lighter servo load or a separate 5V supply.
Lamp turns off by itself: what should I check?
Its own light is reaching the LDR, shield the sensor.
