100% found this document useful (1 vote)
466 views

Lab06 - Smart Home Automation Using Sensors

The document provides code for an Arduino-based smart home automation system using various sensors. The system uses an ultrasonic sensor to control a servo motor-operated door, a PIR motion sensor and light dependent resistor to control lights, and a gas sensor to detect gas levels and trigger an alarm. An LCD display is used to output real-time sensor readings and system status.

Uploaded by

Châu Trần
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
466 views

Lab06 - Smart Home Automation Using Sensors

The document provides code for an Arduino-based smart home automation system using various sensors. The system uses an ultrasonic sensor to control a servo motor-operated door, a PIR motion sensor and light dependent resistor to control lights, and a gas sensor to detect gas levels and trigger an alarm. An LCD display is used to output real-time sensor readings and system status.

Uploaded by

Châu Trần
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Smart Home Automation Using Sensors V1:

- Login Tinkercad: https://www.tinkercad.com/


- Tutorial video: https://www.youtube.com/watch?v=iHOWohC6Ecs

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;

long readUltrasonicDistance(int triggerPin, int echoPin)


{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

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

pinMode(8,OUTPUT); //signal to piezo buzzer


pinMode(9, INPUT); //signal to PIR
pinMode(10, OUTPUT); //signal to npn as switch
pinMode(4, OUTPUT); //Red LED
pinMode(3, OUTPUT); //Green LED

void loop()
{

//------light intensity control------//


//--------------------------------------------------------------
int val1 = analogRead(LDR);
if (val1 > 500)
{
digitalWrite(13, LOW);
Serial.print("Bulb ON = ");
Serial.print(val1);
}
else
{
digitalWrite(13, HIGH);
Serial.print("Bulb OFF = ");
Serial.print(val1);
}

//--------------------------------------------------------------
//------ 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);

if (sen1Value < 100)


{
servo_7.write(90);
Serial.print(" || Door Open! ; Distance = ");
Serial.print(sen1Value);
Serial.print("\n");

}
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);

//For ultrasound sensor


int distanceThreshold = 0;
int cm = 0;
int inches = 0;

//for Relay Control


int releNO = 13;
int inputPir = 8;
int val = 0;
int resuldoSensorLDR;
int sensorLDR = A0;

//For Gas sensor


int const PINO_SGAS = A1;

long readUltrasonicDistance(int triggerPin, int echoPin)


{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

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);
}

int color = analogRead(PINO_SGAS);

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);
}

You might also like