All About Arduino 2: Making an Automatic Night Light, Part 1
Here comes the second of our new series, All About Arduino, where we explore all the things you can do with just an arduino! If you missed it, be sure to check out our Getting Started Guide!
As a reminder, for this series, we’ll only be using things you can find in the Elegoo UNO Project Super Starter Kit, and requires only a minimal knowledge of electronics. I myself only know a little bit, and what I’ve learned, I learned from experimenting, and messing around with these components and circuits! And if anything gives you real trouble, I recommend you get in touch with us (maybe on our discord server?) and we’ll try to help you out!
To kick off the projects, I tackled a little something I’ve been wanting to try out for a while, just to help me get acquainted with the equipment: making an automatic night light. You see, the Starter Kit comes with a special sensor called a photoresistor. This little device adds resistance into a circuit based on how much light is hitting it! With this, we can make all sorts of devices that change based on whether it’s in bright or dim light.
So today, I’m going to try to make a light that only turns on in the dark!
To help me out, I’m going to be using the Arduino company’s tutorials https://www.arduino.cc/en/Guide, and building off of them! If you’re looking for somewhere to get started, this is the place. Here we go!
Step 1: Measuring Resistance
First off, I figure that it’ll be helpful if we can see just how much resistance our photoresistor is adding to our circuit. Looking through the “built-in examples” section of Arduino’s website, I found just the tutorial for me: https://www.arduino.cc/en/Tutorial/AnalogInOutSerial. Following it step-by-step, I’m able to add a dial that adds or reduce resistance based on how I turn it! It’s really helpful to see how the Arduino receives this information.
To make this circuit at home, you’re going to want to use the breadboard in your kit to prototype it! Take a look at the first diagram in that tutorial; in order to connect the LED, resistor, and potentiometer to the board, you’re gonna have to plug them into your breadboard. Put them in whatever row you like, just make sure you’re keeping track of what’s connected to what! Finally, you can connect these components to the Arduino using the multicolored wires in your kit!
Once all the components are connected, use the blue USB cord in your kit to plug your Arduino into a computer. Desktop, laptop, doesn’t matter, as long as you have the Arduino program installed! If you’ve followed the Getting Started guide, you should be all set to run the program: now all you need to do is copy the code block from the Analog In Out Serial tutorial, and paste it in your program! Click “upload” at the top, and you’ll see the light on your arduino blink on. Then, if everything was connected properly, your LED should light up when you twist your potentiometer! Here’s what mine looked like when it was all set up:
In addition to the LED, if you click on the “Serial Monitor” button in the upper right of your Arduino program, you can see the Arduino record data in real-time!
Step 2: Measuring Photoresistor Resistance
My next task is to swap out the potentiometer for a photoresistor! Remember, the goal is to make a circuit that activates an LED when it senses low light. The potentiometer added more or less resistance based on how the dial was positioned; the photoresistor should do the same thing, but use light instead of a dial!
To figure out exactly how we should plug this into our breadboard, we need to take a look at how this circuit works:
If you’ve never looked at a circuit diagram before, this is probably pretty confusing. Worry not! I’ll try to explain what’s important. You’ll probably recognize the big rectangle in the center — that’s a simplified version of your Arduino. The lines on either side represent the wires in your circuit!
On the right side, pin 9 is connected to the zig-zag, which is the symbol for a resistor! This is connected to your LED, which is then connected to GND on your Arduino! On your hardware, it’s a pin like everything else; but because all “grounds” on your Arduino are effectively connected, the diagram just represents them with “GND” on the bottom.
On the left side, that resistor with a line through it is a potentiometer! It adds a bunch of resistance between the 5V pin and GND, but it provides some fraction of that resistance between 5V and A0. Here, it’s important to know exactly what A0 is measuring!
Your electric circuit has a certain amount of Voltage at every part of the circuit. Voltage is a bit tricky to wrap your head around, but for this project, we can think about it just like water pressure. At the start of the circuit, the 5V pin is putting out five volts of pressure! If it was simply connected to ground, that would be like opening a huge pipe that lets all the pressure out, and electricity would freely flow from 5V to GND.
However, when we add some resistance to the circuit with a potentiometer, we use a smaller pipe; even though there’s five volts of electricity trying to get out of 5V, it’s being blocked, and having a harder time getting through to GND! When we channel some of this electricity away to the A0 pin, it’s like adding a new pipe to the system; some water will flow through the 2nd pipe, and some will go through the first. However, the A0 pipe doesn’t go anywhere; sitting at the end is a biiiig sensor. As water flows through the first pipe to GND, there’s still a bunch of pressure, because the pipe is so small.
Essentially, A0 is measuring how much pressure is hitting it! This means that if we turn the dial of the potentiometer, we make the pipe bigger or smaller, and can measure how the pressure gets bigger or smaller in turn. To get rid of the metaphor, A0 measures the voltage that is hitting the pin! The code that we’re using converts this voltage to a number, and that’s how we change the brightness of the LED!
Alright. Phew. That’s a lot of text, I know. Sorry about that! If things are still unclear, please feel free to ping me on the IRL Discord server, and I’ll explain whatever you need! Anyway: the important thing to know for our circuit is that we need all three connected: 5V, A0, and GND. We need the changing resistance of the photoresistor to be recorded by A0, so let’s connect those two using the two legs of the photoresistor. However, we still need the excess to flow into GND! To do this, I found it was best to use a 1k resistor connecting the row with the photoresistor and A0 to GND. Here’s a couple pictures of my setup!
With this, we’ve made a light-sensitive LED! This has been a real explanation-heavy post, but we’ve made a lot of conceptual progress! Because there was so much ground to cover, I’m gonna leave this as a “Part 1.” Hold on to this circuit, and in Part 2 I’ll go over some edits to the code, how to adjust it to the light levels in your home, and how you might add finishing touches to your night light!