GAMERIFTRobotics & Coding Shop kits
HomeKitsFor schoolsAboutGAMERIFT Store ↗
Beginner Kit · Lesson 06 of 12

Night Light

Build a lamp that feels the dark arrive and switches itself on. Nobody touches anything.

Unlock your lessons

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.

LICENSED TO , PERSONAL USE · © GAMERIFT

BRIEF The machine decides alone

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).

KIT What you need

Concept · A resistor that feels light

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.

Concept · Thresholds and calibration

"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.

BUILD Wire it

Unplug the USB. The photoresistor has no direction, either leg either way.

FromToWhy
R3 5VPhotoresistor leg 1Top of the divider chain
Photoresistor leg 2Breadboard row (the midpoint)Where the two resistors meet
Same midpoint row10kΩ resistor → R3 GNDBottom of the chain
Same midpoint rowR3 pin A0The board eavesdrops on the midpoint
R3 pin 9 (~)220Ω resistor → LED long legThe lamp (PWM pin, for the remix)
LED short legR3 GNDLoop complete

CODE Program it, in two stages

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);
}
  1. Upload, then cup your hand over the sensor. Lamp on. Lift your hand. Lamp off.
  2. Now switch the room light off tonight and watch your creation greet the dark by itself. That feeling? That is why people become engineers.

BREAK IT Now break it on purpose

  1. Shine your phone torch at the sensor from across the room. How far away can it still push the lamp off? You are measuring your sensor's real-world range.
  2. Set the threshold to 1000. The lamp is now on almost always, a badly calibrated sensor is worse than no sensor. Set it to 50 and it may never trigger. Feel how much the number matters.
  3. Swap the photoresistor and the 10k resistor in the chain. Everything works, backwards, the reading now goes UP in darkness. Fix it in code instead of rewiring: change < to >. Sometimes software repairs hardware.

REMIX Make it yours

LOG IT Show and tell

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.

Next · Lesson 07: Count On Me

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.

All lessons