Automated Light System 💡
- Princess Bamigboye
- Aug 17, 2023
- 3 min read
Updated: Nov 1, 2023
Hi everyone 😊
I know it's been a while since I posted.
Not too long, but you get 😅
I've been working on a few things.
So, to you reading,
Welcome back 🤗
This is quite possibly the most complex and advanced project I have had the opportunity of working on so far, and yes, that includes considering my project with the two ultrasonic sensors.
This is because this project is a prototype for an automated light system.
Components used:
Arduino Uno R3 board.
PIR (Passive Infrared) Sensor.
An LED (in the place of a light bulb).
One (1) 330-ohm resistor.
A one-channel relay module.
Female-male wires.
Jumper wires.
Now, I will need you to visualize...
VISUALIZATION:
Imagine, you have an automated light system in your house, meaning that as you walk into your house (or a room), the lights automatically come on.
HOW THIS WORKS :
There will be a sensor connected to the lighting system in your house(/room).
Once the sensor picks up the infrared rays coming from your body, it sends a signal to the Arduino, which sends a a command to the relay that powers (or turns) on the light system.
What made this project a bit more complex for me was the fact that I had to learn how to work with a relay module.
"WHAT IS A RELAY? ", you may ask.
Well, a relay is a programmable electrical switch, which can be controlled by an Arduino or any microcontroller. It is used to programmatically control (on/off) devices, which use high voltage and/or high current.
It is a bridge between the Arduino and high-voltage devices.
The relay has pins that will be connected to the Arduino.

Relay pin-out diagram
Relay connection in real time
WHY USE A RELAY?
We cannot connect a light bulb directly to the Arduino pins. This is because the light bulb is a device that uses a high-voltage(/high-current) power supply. This can burn the Arduino.
So, since relays are used to programmatically control devices that use high voltage and/or high current, we will need to use one as a bridge between the Arduino pins and the light bulb to protect the Arduino from the high voltage/current.
NOTE:
Since this project is a prototype (and for safety reasons) an LED was used in place of a light bulb.

Wiring diagram for light system
Apparently, the relay to be used in real-time, and the relay(s) used in Tinkercad are different. As a result, I had to learn how to use the simplest relay with the help of two YouTube videos.
The links to the video(s) will be in my references at the end of this post.
The circuit diagram on Tinkercad;

Now...
THE CODE:
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirSensor = 7; //the digital pin connected to the PIR sensor's output
int relay = 4;
int pirVal;
int dt = 1000;
int dt2 = 500;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirSensor, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(pirSensor, LOW);
//give the sensor some time to calibrate
Serial.print("Calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(dt);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(dt2);
}
////////////////////////////
//LOOP
void loop(){
pirVal = digitalRead(pirSensor);
if(pirVal == HIGH){
digitalWrite(relay, HIGH); //the relay & led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(dt2);
}
takeLowTime = true;
}
if(pirVal == LOW){
digitalWrite(relay, LOW); //the relay & led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(dt2);
}
}
}

This is what should be displayed on the Serial monitor.
Hardware circuit
The end result
💎
👑
💎
REFERENCES :
THE DIAGRAMS (Relay & Visual): https://arduinogetstarted.com/tutorials/arduino-relay
THE YOUTUBE VIDEOS: https://youtu.be/DaZxZ98A1Jo
&








Comments