Built-in project · Arduino Uno R3
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.
BeginnerRuns in your browser. Free, and no account needed.
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
sketch.ino · Arduino Uno R3
// 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 no LDR part, 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);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Pushbutton
- 2 × Resistor, 220 Ω
- 1 × LED, yellow
- 1 × LED, white
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Pushbutton pin 2a; LED, yellow pin C; LED, white pin C
- Arduino Uno R3 pin 9; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 8; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 1a
- Resistor, 220 Ω (1) pin 2; LED, yellow pin A
- Resistor, 220 Ω (2) pin 2; LED, white pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.