Build a lamp that feels the dark arrive and switches itself on. Nobody touches anything.
Enter the access code from the card inside your Beginner Kit (or from your GAMERIFT order email). Your name appears on every page and printout as the licensee.
Don't have a kit yet? Get the Beginner Kit, the code ships inside. Lost your code? Contact us with your order number.
Streetlights don't have a person flicking a switch at sunset. They feel the light fade and decide for themselves. Today you build exactly that: a light sensor, a threshold, and a lamp that comes on when your room gets dark. This is your first fully automatic machine, the first one that works while you are not even there.
Time: 45–60 minutes · Difficulty: 3/5 · You need: Lesson 04 done (analog readings and the Serial Monitor).
The photoresistor is a resistor whose resistance changes with light: bright room, low resistance; dark room, high resistance. But your board cannot read resistance directly, it reads voltage. So we team the photoresistor up with a normal 10k resistor in a chain from 5V to GND, and measure the voltage at the midpoint. As light changes, the midpoint voltage slides up and down. Engineers call this famous two-resistor trick a voltage divider, and it is how thousands of real sensors work.
"Dark" is not a number until you make it one. First you spy on the sensor with the Serial Monitor: what does it read in daylight? Under your hand? Then you pick a number in between, the threshold, and write: below this, lamp on. Choosing that number from real measurements is called calibration, and it is a ritual every engineer performs on every sensor they ever meet.
Unplug the USB. The photoresistor has no direction, either leg either way.
| From | To | Why |
|---|---|---|
| R3 5V | Photoresistor leg 1 | Top of the divider chain |
| Photoresistor leg 2 | Breadboard row (the midpoint) | Where the two resistors meet |
| Same midpoint row | 10kΩ resistor → R3 GND | Bottom of the chain |
| Same midpoint row | R3 pin A0 | The board eavesdrops on the midpoint |
| R3 pin 9 (~) | 220Ω resistor → LED long leg | The lamp (PWM pin, for the remix) |
| LED short leg | R3 GND | Loop complete |
Stage 1: spy first. Never guess what a sensor sees, look.
// Stage 1: what does my room look like, in numbers? void setup() { Serial.begin(9600); } void loop() { Serial.println(analogRead(A0)); delay(200); }
Open the Serial Monitor. Note the number in normal light. Now cup your hand over the sensor, note that number too. Write both down, really, on paper. Pick a threshold roughly in the middle.
Stage 2: the night light. Use YOUR numbers, not ours.
// GAMERIFT Beginner Kit, Lesson 06: Night Light // Below the threshold, the lamp wakes up. const int sensorPin = A0; const int lampPin = 9; const int threshold = 400; // <-- YOUR number from Stage 1 goes here void setup() { pinMode(lampPin, OUTPUT); Serial.begin(9600); } void loop() { int light = analogRead(sensorPin); Serial.println(light); if (light < threshold) { digitalWrite(lampPin, HIGH); // dark: lamp on } else { digitalWrite(lampPin, LOW); // bright: lamp off } delay(100); }
Film the moment the room light goes off and your lamp wakes up, that clip belongs in your lesson log. Stuck? Send a photo of your wiring via contact us.
Lights that react are great, but machines also need to show you things. Next lesson you wire the seven-segment display, the classic digital number, and build a press counter.