STEM

Arduino: Build a custom nightlight

In this project, I built a night light with exact parameters of my own liking using simple components. I wanted the light to come ON when I detect the ambient light to be an exact value customized for my room, stay ON exactly as long as I want, and/or blink as I wish. Additionally, I can also choose the exact color to shine, by mixing all RGB colors to create any humanly visible color.

For this I used a fixed 10KΩ Photoresistor, a 5K Ω resistor for photoresistor, a 330Ω resistor for a LED, and some power and data wires. Then I used C/C++ code to read signals from the photoresistor and write to a LED lamp.

The C/C++ code is written in Arduino IDE that also connects via USB to the UNO controller, and code is transferred to the chip on the controller via USB A-B cable. The circuit set up is straight forward and shown below. The important thing to remember is connect the right resistors to the photoresistor and to the LED (or you’ll permanently damage the components!). As usual, it’s powered only by a 5V source from the controller or through a battery or AC adapter. It looks like this on the breadboard circuit:

In this case, I’m showing a yellow LED. Whenever the light value coming from the photosensor module is below my specified value the LED turns ON, otherwise it turns OFF. This is monitored continuously (every microsecond) so whenever the light conditions change, the LED lamp will behave accordingly (ON or OFF). Now, change the yellow LED to a RGB LED with 4 prongs (IMPORTANT: the longest pin is the CATHODE for a RGB LED, which seems quite opposite of the usual single-colored LED we encounter)…and now you can actually make the light glow in ANY color. For a real example of that, see my other project: https://flyingsalmon.net/?p=2660

The other pins (R,G,B) can be controlled independently by code to create all hues possible.

At first glance, you might think: Big deal! I can buy a night light at any store for cheap! Yes, you can…without understanding any of its innerworkings or having any fun in building it yourself, but more than that, you cannot do the things we can with this from an off-the-shelf product. You can’t change its color. You can’t control how long it stays on, or whether it blinks and if so at what frequency, or its brightness, etc. etc. One of the practical uses for myself is that if I have to get up to use the restroom in the middle of the night, I want to be able to see just enough to find my ‘target’ and not be blasted with light to disturb my return to sleep, and I don’t want the light to be always on either. Yes, you can also tie this circuit to a motion detection module/logic (that project is coming in the future). And then if you change your mind and need to change its behavior, you just write new code, download it to your controller’s chip in seconds and you have a new device!

So, now you see the power in your hands! Have fun 🙂

Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!


Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top