Night light on an Arduino Uno
A lamp fades up when the sensor pin goes dark, with a pushbutton standing in for the light sensor. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- Reading an input with an internal pull-up resistor
- Fading an LED with
analogWriteon a PWM pin - Moving toward a target a little at a time instead of jumping
- Why a button can stand in for a sensor while you test
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
A pushbutton on pin 2 stands in for a light sensor. It uses the internal pull-up, so the pin reads HIGH when nothing is pressed (light) and LOW when you hold the button (dark).
When it is dark, the sketch wants the lamp at full brightness, 255. When it is light, it wants 0. It does not jump. Every 20 ms it moves the lamp level toward that target by 8, so the fade takes about six tenths of a second.
The lamp is the yellow LED on pin 9, which can do PWM. The white LED on pin 8 is an indicator that shows what the sensor reads right now, even while the lamp is still fading.
Parts list
7 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, whiteIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| LED, yellow pin C | Arduino Uno R3 pin GND |
| LED, white pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 8 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| LED, yellow pin A | Resistor, 220 Ω (1) pin 2 |
| LED, white pin A | Resistor, 220 Ω (2) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Night light: the lamp fades up when it goes dark and down when it is light.
//
// pin 2 the light sensor, to GND, internal pull-up
// pin 9 the lamp, through 220 ohm to GND
// pin 8 the indicator, through 220 ohm to GND
//
// The sensor here is a pushbutton, standing in for the light-dependent
// resistor a real night light has. Mokxi has an LDR part, but a button is
// easier to hold, and the two behave the same way as far as the sketch is
// concerned: the pin is high while there is light and low while there is not.
// Hold the button and the room is dark.
//
// The lamp is on pin 9, which is one of the ATmega328P's PWM outputs, so
// analogWrite can give it any brightness from 0 to 255. It walks there rather
// than jumping: a night light that snaps on is a torch, and the ramp is what
// makes it a night light. Eight counts every twenty milliseconds is about six
// tenths of a second end to end.
//
// The indicator on pin 8 is a plain digital output and says what the sensor is
// reading right now, so you can see the input and the output disagree while
// the lamp is still on its way.
const int SENSOR = 2;
const int LAMP = 9;
const int INDICATOR = 8;
const int STEP = 8;
const int FULL = 255;
int level = 0;
static bool dark() { return digitalRead(SENSOR) == LOW; }
void setup() {
pinMode(SENSOR, INPUT_PULLUP);
pinMode(LAMP, OUTPUT);
pinMode(INDICATOR, OUTPUT);
analogWrite(LAMP, 0);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: night light");
Serial.println("the button stands in for a light sensor: hold it for dark");
}
void loop() {
bool night = dark();
digitalWrite(INDICATOR, night ? HIGH : LOW);
int want = night ? FULL : 0;
if (level < want) {
level += STEP;
if (level > want) {
level = want;
}
} else if (level > want) {
level -= STEP;
if (level < want) {
level = want;
}
}
analogWrite(LAMP, level);
static int said = -1;
if (level == want && level != said) {
said = level;
Serial.print(night ? "dark: lamp at " : "light: lamp at ");
Serial.println(level);
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
dark sketch.ino
Returns true when pin 2 reads LOW. Because of the pull-up, LOW means the button is held, which stands for a dark room. Wrapping the test in a function keeps the rest of the code easy to read.
static bool dark() { return digitalRead(SENSOR) == LOW; }setup sketch.ino
Sets the sensor pin as INPUT_PULLUP and the lamp and indicator pins as outputs. It starts the lamp at 0 with analogWrite and prints two lines to the serial monitor.
void setup() {
pinMode(SENSOR, INPUT_PULLUP);
pinMode(LAMP, OUTPUT);
pinMode(INDICATOR, OUTPUT);
analogWrite(LAMP, 0);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: night light");
Serial.println("the button stands in for a light sensor: hold it for dark");
}loop sketch.ino
Sets the indicator to match dark, then picks a target of FULL or 0. It moves level toward the target by STEP, without overshooting, and writes it to the lamp. Once the lamp arrives, it prints the level once. Then it waits 20 ms.
void loop() {
bool night = dark();
digitalWrite(INDICATOR, night ? HIGH : LOW);
int want = night ? FULL : 0;
if (level < want) {
level += STEP;
if (level > want) {
level = want;
}
} else if (level > want) {
level -= STEP;
if (level < want) {
level = want;
}
}
analogWrite(LAMP, level);
static int said = -1;
if (level == want && level != said) {
said = level;
Serial.print(night ? "dark: lamp at " : "light: lamp at ");
Serial.println(level);
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Holding the button does nothing. This project
Wire pin 2 to one side of the button and the other side to GND. The pull-up is inside the Uno. - The yellow lamp does not fade. This project
Check that its resistor is on pin 9, which is a PWM pin. A pin without PWM would only jump on or off. - An LED stays dark. This project
Check its direction: the anode goes toward the resistor, and the cathode goes to GND. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.