Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

DIY Arduino - Project 02: Dark Room

Today I’ll demonstrate my second DIY project. I call it Dark Room. But first, I want to talk about the idea of this project. When people come home late in the evening, they struggle to find the light switch because it’s too dark in their room. They can’t leave the light on all day either since it will be a waste of energy during the day. Therefore, they need a smaller light that automatically turns on whenever the room gets dark enough. With an Arduino UNO, a photocell, and an LED, that need is totally achievable.






The photocell reads the amount of light it receives, and the LED will turn on if the amount of light reaches the threshold. The circuit has the same configuration like the fourth project of the book, but this time we only use one non-PWM LED.

Here goes the schematic:


Here is the breadboard layout:


As always, both of them are available on my GitHub. And here is the real circuit:



There is nothing new here. I use the knowledge that I have learned from previous projects to program this circuit.

See below for my code, or view it on my GitHub.

 * Project Name: DIY Arduino - Project 02: Dark Room
 * File Name: dark_room.ino
 * Description: Automatically turns on the LED when it's dark.
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: December 13, 2015
 * Updated: June 22, 2017

// Required hardware I/O connections
const byte PHOTO_PIN = A0; // connect photocell to A0
const byte LED_PIN   = 2;  // connect LED to 2

// Global constants
const unsigned short THRESHOLD = 100;

void setup() {
    pinMode(LED_PIN, OUTPUT);

void loop() {
    int photo_val = analogRead(PHOTO_PIN);

    // toggle the light based on the threshold of light
    if (photo_val < THRESHOLD) {
        digitalWrite(LED_PIN, HIGH);
    } else {
        digitalWrite(LED_PIN, LOW);


The way I used it was pretty awkward. I assembled the circuit and complete the code the night before, but I didn’t have time to record a demo. I should have used it when the room gets dark (and I could have done it by turning on/off the desk lamp). Thanks for reading!