Lab06 - Smart Home Automation Using Sensors
Lab06 - Smart Home Automation Using Sensors
Code:
float x,y,z,temp;
void setup()
{
pinMode(8, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(A5, INPUT);
pinMode(A4, INPUT);
Serial.begin(9600);
}
void loop()
{
x= digitalRead(8);
y= analogRead(A5);
z= analogRead(A4);
Serial.println(x);
Serial.println(y);
Serial.println(z);
temp = (double)z / 1024;
temp = temp * 5;
temp = temp - 0.5;
temp = temp * 100;
if ( (x>0) )
{
if ((y<550)&&(temp>30))
{
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}
else if((y<550)&&(temp<30))
{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
else if((y>550)&&(temp>30))
{
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
}
else if((y>550)&&(temp<30))
{
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
}
else
{
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
}
Smart Home Automation Using Sensors V2:
- Login Tinkercad: https://www.tinkercad.com/
- Tutorial video: https://www.tinkercad.com/things/eprJv0vDKHU
Code:
#include <Servo.h>
int output1Value = 0;
int sen1Value = 0;
int sen2Value = 0;
int const gas_sensor = A1;
int const LDR = A0;
int limit = 400;
Servo servo_7;
void setup()
{
Serial.begin(9600); //initialize serial communication
pinMode(A0, INPUT); //LDR
pinMode(A1,INPUT); //gas sensor
pinMode(13, OUTPUT); //connected to relay
servo_7.attach(7, 500, 2500); //servo motor
void loop()
{
//--------------------------------------------------------------
//------ light & fan control --------//
//--------------------------------------------------------------
sen2Value = digitalRead(9);
if (sen2Value == 0)
{
digitalWrite(10, LOW); //npn as switch OFF
digitalWrite(4, HIGH); // Red LED ON,indicating no motion
digitalWrite(3, LOW); //Green LED OFF, since no Motion detected
Serial.print(" || NO Motion Detected " );
}
if (sen2Value == 1)
{
digitalWrite(10, HIGH);//npn as switch ON
delay(5000);
digitalWrite(4, LOW); // RED LED OFF
digitalWrite(3, HIGH);//GREEN LED ON , indicating motion detected
Serial.print(" || Motion Detected! " );
}
//---------------------------------------------------------------
// ------- Gas Sensor --------//
//---------------------------------------------------------------
int val = analogRead(gas_sensor); //read sensor value
Serial.print("|| Gas Sensor Value = ");
Serial.print(val); //Printing in serial monitor
//val = map(val, 300, 750, 0, 100);
if (val > limit)
{
tone(8, 650);
}
delay(300);
noTone(8);
//--------------------------------------------------------------
//------- servo motor ---------//
//-------------------------------------------------------------
sen1Value = 0.01723 * readUltrasonicDistance(6, 6);
}
else
{
servo_7.write(0);
Serial.print(" || Door Closed! ; Distance = ");
Serial.print(sen1Value);
Serial.print("\n");
}
delay(10); // Delay a little bit to improve simulation performance
}
Smart Home Automation Using Sensors V3:
- Login Tinkercad: https://www.tinkercad.com/
- Tutorial video: https://www.youtube.com/watch?v=6Xi6qZHl7ws
Code:
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(releNO, OUTPUT);
pinMode(inputPir, INPUT);
pinMode(sensorLDR, INPUT);
Serial.begin(9600);
}
void loop() {
// set threshold distance to activate LEDs
distanceThreshold = 350;
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 6);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("D:"); // Prints string "Distance" on the LCD
lcd.print(cm); // Prints the distance value from the sensor
lcd.print("cm");
delay(10);
val = digitalRead(inputPir);
resuldoSensorLDR = analogRead(sensorLDR);
if(resuldoSensorLDR<600)
{
if(val == HIGH)
{
digitalWrite(releNO, HIGH);
lcd.setCursor(0,1);
lcd.print("L: On ");
delay(5000);
}
else{
digitalWrite(releNO, LOW);lcd.setCursor(0,1);
lcd.print("L: Off");
delay(300);
}
}
else{ digitalWrite (releNO, LOW);
Serial.println(resuldoSensorLDR);
delay(500);
}
lcd.setCursor(8,0);
//lcd.print("");
if(color <= 85){
lcd.print("G:Low ");
} else if(color <= 120){
lcd.print("G:Med ");
} else if(color <= 200){
lcd.print("G:High");
} else if(color <= 300){
lcd.print("G:Ext ");
}
delay(250);
}