Arduino with relay
This week I decided to apply the assignment to a part of my other final project which was a backup for my clothes folder project.
So the idea and inspiration behind it was that I some times I go to sleep and get under the covers and forget to turn off the lights and it's hard to just get out of bed and close them XD.
So i wanted to make something that can help me turn off the lights while I was in bed, and I found this idea:
https://howtomechatronics.com/tutorials/arduino/control-high-voltage-devices-arduino-relay-tutorial/
https://www.youtube.com/watch?v=HKy4w9YLJqo
I replaced the ir reciever and remote control with the ir module for now and added the microwave sensor for motion detection in the room.
Circuito.io
Arduino IDE
Bread Board
Jumpers
Adaptor 9V
Relay Module
Servo motor *2
Arduino UNO
Crocodile wires
IR sensor
ON/OFF switch
Microwave sensor
Initially i wanted to make a simulation or wiring diagram for the circuit on a software but I encountered some dificulties because not all components are available on the softwares that I know of, Michael recomended circuit.io on slack and it was nice but unfortunately I also didn't find everything I need and not the easiest to use but it was helpful and nice to know.
I just put the components for visualization.
The circuit (the lamp and microwave sensor are missing)
The problem with circuito.io is that l can't edit the places for the sensors pins on arduino and it's not easy to use and edit like tinkercad.
but the wires are color coded to show where each component is on the arduino.
I then connected my components to the arduino and bread board and started to write down the code logic and the code.
(microwave sensor is missing because I put it later on to solve a problem I encountered)
So, my inputs were supposed to be 2, the switch and the ir sensor, I later discovered that they have conflicting conditions that will miss with my code so I decided to put the switch series with the lamp and my other input will be the microwave sensor, it will satisfy the multiple input requirement and also i forget to close the lights when i go out of the room so that was a great bonus XD
The logic for the code
What I originally wanted to do (but failed so many times to reach it) is to control the light via the ON/OFF switch and the IR sensor.
when I press the ON/OFF switch the lamp turns ON/OFF and the LCD displays that the lamp is ON/OFF.
And when the IR recieves a signal the lamp turns off.
and after 5 times the actuator turns off the lamp by first opening the door by one servo then the actuator servo moves to turn off the switch (like the usless box)
But unfortunately I didn't have enough time for adding the motors part because I got caught up in modifying the code to do the original function first.
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servo_door;
Servo servo_act;
#define relay 7
#define sw 4
#define ir 2
bool lamp = false;
void setup(){
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.noBacklight();
pinMode(sw, INPUT_PULLUP); //sw
pinMode(relay, OUTPUT); //relay
pinMode(ir, INPUT); //ir
servo_door.attach(6);
servo_act.attach(3);
}
void loop(){
if (digitalRead(ir) == HIGH) {
if (digitalRead(sw) == LOW ) {
digitalWrite(relay, HIGH);
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is on");
Serial.println("Lamp is on");
lamp = true;
delay(100);
}
else if (digitalRead(sw) == HIGH){
digitalWrite(relay, LOW );
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is off");
Serial.println("Lamp is off");
lamp = false;
delay(100);
}
}
else if (digitalRead(ir) == LOW ){
digitalWrite(relay, LOW );
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is off");
Serial.println("Lamp is off");
lamp = false;
delay(100);
}
}
Fitting the components inside the enclosure
Turning the lamp on using the switch
Turning the lamp off using the switch
using switch and IR sensor
Video with lamp on the relay
This week I had dificulties in implementing the code I wanted and My amazing instructor "Ahmed Abdelnasser" helped me alot and gave me clues and tried with me untill we reached an acceptable result.
One of the solutions was with using boolean and it was maybe the first time l actually use it in a code even though I knew about it before.
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servo_door;
Servo servo_act;
#define relay 7
#define sw 4
#define ir 2
int state_lamp=0;
void setup()
{
//pinMode(3, INPUT); //slider
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.noBacklight();
pinMode(sw, INPUT_PULLUP); //sw
pinMode(relay, OUTPUT);//relay
pinMode(ir, INPUT);//ir
servo_door.attach(6);
servo_act.attach(3);
}
void loop()
{
if (digitalRead(sw) == HIGH )
{
digitalWrite(relay, HIGH);
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is on");
state_lamp = 1;
}
else if(digitalRead(sw) == LOW )
{
digitalWrite(relay, LOW);
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is off");
state_lamp = 0;
}
if (digitalRead(ir) == LOW)
{
digitalWrite(relay, HIGH);
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is on");
state_lamp = 1;
}
else if(digitalRead(ir) == LOW && state_lamp == 1)
{
digitalWrite(relay, LOW);
lcd.clear();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Lamp is off");
state_lamp = 0;
}
delay(10); // Delay a little bit to improve simulation performance
}
So, the problemsl faced in my code were:
l wanted to operate the lamp with 2 diffrent inputs, and that often results in conflcting conditions and doesn;t do the intended purpose.
l tried different variations of conditions and tried to use booleans to establish when the lamp is on or off and work from there, but for some reason that also didn't work.
eventualy l reached some solution where l put only two conditions to close the light, when the switch is off and when the ir reads low, it did the function but just not how l wanted it exactly.
Title of Media