To program an automatic night light system using Arduino Uno or Arduino Nano, you need to have main components such as a light sensor (e.g., LDR – Light Dependent Resistor), a relay or transistor to control the light, and an LED light to simulate the night light. Below are step-by-step instructions to implement this project.
Hello engineers. Hope everyone is fine. We have made Automatic Night Light circuit on this website. Those circuits are very easy and useful using only few components and working fine.
But, in this article we will discuss automatic night light circuit using Arduino Uno and also automatic night light circuit using Arduino Nano board. We will discuss both circuits as well as practical implementation using Arduino Uno and Nano. With complete detailed circuit diagram, code and actual circuit working, video using both Arduino Uno and Nano. So let’s get started with our Arduino Night Light tutorial.
1. List of Components
- Arduino Uno or Nano: Microcontroller board.
- Light Dependent Resistor (LDR): Measures the intensity of surrounding light.
- 10kΩ Resistor: Used to pull down the LDR.
- Relay or Transistor: Used to control the light.
- LED (or actual light bulb): Used as the night light.
- 220Ω Resistor: Used for the LED.
- Connecting wires and breadboard: Used to connect the components.
Materials needed
- BreadBoard/ PCB
- Q1, 2N2222 or 2N3904
- Relay 5V/ 12V (we used 12v)
- R1 10K resistor
- D1 LED-RED
- D2-diode 1N4007
- Arduino Uno R3 or Arduino Nano
- Software Tools-Arduino.ide Download here.
- 12V 1A power supply (Or from adapter to power both board and 12V relay), 5V USB for Arduino nano
- 220R resistor
- Other components. Male
- Here is a suggestion for different Arduino starter kits.
Automatic Night light using Arduino Uno
Circuit Operation
In the above circuit we have used Arduino Uno, but this can be done with Nano as well. The pins and everything are the same, only the pin number and power specifications are different.
We used an Arduino Uno and powered it with a 12V 1A DC adapter through its DC jack. The +12V positive line also goes to the other leg of the 12V relay coil.
Analog pin A1 is set as a signal reading pin to receive the variable current input from LDR and the voltage divider network of 10Kilo ohms pull-down resistor. With the changing brightness of the light shining on LDR, its internal resistance also changes continuously.
As we know, LDR (Light-dependent Resistor) has an internal resistance that keeps changing with light and darkness. As the light shining on LDR increases, its resistance drops to just a few hundred ohms from 1M Ohm. And with the increase of darkness, its resistance keeps increasing and reaches its maximum.
The output of the LDR network and the 10k resistor is read by Analog pin A1.
Now, it is the turn of the output pins and we have used Digital pin 2 to control the LED and relay according to the input signal and logic provided by the program. Of course, you can use any pin, or if you want PWM control then you can use any PWM pin. We just want to enable the relay so we have used pin 2.
Pin 2 provides 5V output to the base of the 2N3904 Transistor to activate it. Once the transistor is turned on, it activates the relay through the collector. The 4.3V DC after the transistor is not enough to activate that relay. So we have taken the common positive +12V from the 12V 1A supply.
You can also power the Arduino with a USB cable and use an external 12V supply for the 12V relay. Just make sure you connect the emitter pin to the Arduino’s ground and the ground of the 12V supply.
Check out the videos and pictures. Make sure you subscribe. The video below shows how I used an Arduino Uno and LDR with a 12V relay for this automated light kit, where a 220V AC load is turned on when the room lights are off and turned off when the room lights are on.
Source code for Arduino automatic night light.
Below is the code, with each of its components clearly explained.
2
3
4
5
6
7
8
9
ten
11
twelfth
13
14
15
16
17
18
19
20
21
22
23
|
int IN; int Out = 2 ; void setup () { Â Â Serial.begin ( 9600 ); Â Â pinMode (Out, OUTPUT ); } void loop () { Â Â IN = analogRead (A1); Â Â Serial.println (IN); Â Â delay ( 100 ); Â Â if (IN < = 120 ) Â Â { Â Â Â Â digitalWrite (Out, HIGH ); Â Â } Â Â else if (IN > = 200 ) Â Â { Â Â Â Â digitalWrite (Out, LOW ); Â Â } } |
So the above code is very simple and easy to understand.
Looking from lines 1 and 2 of the code, “int IN;” and int OUT = 2; “int IN” is a variable declaration to receive input and will be assigned a foot later in the code.
“int OUT = 2;” declares variable “OUT” of data type int to receive input from pin assigned as 2 (digital output pin).
“void setup()”, void is the keyword used to declare functions with Arduino.ide. “setup()” is a built-in function that sits at the top of the Arduino code (in arduino.ide, the program code is called a sketch). It runs at the top of the sketch and here we use it to initialize the pin modes. Like which pin will be output or input and which pin we will use. Here we write the code that we want to run only once, as soon as the program is executed.
“Serial.begin(9600)”, serial.begin() is used to transfer serial data between Arduino board and other devices such as personal computer.
Used to allow the Arduino board to send and receive information over the serial port via a common serial interface in a set number of bits at a defined baud rate. It allows the Arduino to communicate with a serial monitor to read data and display it on your screen.
9600 is the specified baud rate, which means the speed or rate at which it transmits bits.
pinMode(out, OUTPUT);, the pinMode() function is used to specify or configure the behavior of an output pin, so it makes sense to be in the void setup() function.
Here we have used the variable ‘out’ to specify which pin provides the output, we have assigned 2 as our digital output pin.
OUTPUT is to define it as an output pin. This is the mode parameter.
pinMode(pin, mode);, pin and mode are parameters.
void loop(), is the function where we write the instructions we want to execute in a loop. This means that it will repeat itself over and over again as long as a particular piece of code continues to run. This is the main part of the code. The actual tasks.
The code starts running after the curly brace ‘{‘ from line 1 and runs each instruction until the last line until the last curly brace ‘}’ and again starts running from the first line of the loop function.
IN = analogRead(A1);, here we have used analogRead() function to read the analog input value coming from pin A1 and the result is stored in int variable IN.
Serial.print(IN); is used to print serial port data in human readable form. We will discuss that later.
delay(100); it displays values ​​with a delay of 100 microseconds.
if ((IN) <= 140) { here we start the if statement and set the condition according to the values ​​we get on the serial monitor, we get from the serial port according to the analog data read by pin A1.
These values ​​continuously change according to the strength of the light we receive on the LDR and are printed continuously with a delay of 100 microseconds onto the screen.
If the condition matches then the statements within the if braces are executed. digitalWrite(out, HIGH); to make pin 2 ‘HIGH’. HIGH is 5V dc.
else if (IN>200) { here we have the second condition if the value is greater than 200 or equal, then the output of pin 2 will be ‘LOW’. That means 0V.
There is a LEFT gap between 120 and 200. So that there is no output flicker.
If you don’t want to use that else if statement, simply use ‘else {statement;}’.
In case you don’t have an Arduino UNO or simply don’t want to use a 12V relay, you can do it with a 5V relay, check the video below for my friend’s channel.
Make sure you follow the steps correctly and correct the circuit diagram. In case you face any problem with it, leave a comment below. Scroll down, you will find the comment section.
ConcludeÂ
With the above steps, you can build an automatic night light system using Arduino Uno or Nano. This system will help the light automatically turn on when the surrounding light decreases and turn off when the light increases. This is a simple yet effective application of Arduino in controlling devices based on light sensors.