   ## Ultrasonic Sensor

An ultrasonic sensor is a distance sensor that emits ultrasonic sound waves and then receives the sound wave back, which is reflected by any obstacle present in front of the sensor. Based on the time of transmitting and receiving the wave, the distance of the object from the sensor is calculated.

Components Required :

• Arduino Uno Board
• Ultrasonic Distance Sensor HC-SR04
• Jumper Wires
• LCD Display

## Working of the Temperature Sensor Steps for Calculating the distance :

• The PIN trig of the Sensor will be set to HIGH so that, ultrasonic waves get transmitted.
• Then a delay is induced.
• Then the trig PIN is set to LOW.
• The echo PIN is then read when the reflected wave is received back.

Calculations :

The function `pulseIn()` starts an internal timer to check when the dedicated pin changes its state.(from HIGH to LOW or LOW to HIGH). And then it stops the timer, and return the time in microseconds.

Therefore, now as the speed of sound is 330 m/sec, which is almost 29 microsecond/cm. Therefore, we have to divide the distance by 29 in order to get the receiving and transmitting distance of the wave. Now, this distance should be divided by 2, to get the actual distance of the object from the sensor.

Image: Temperature Sensor- HC-SR04 Image : 16 * 2 LCD ## PIN Connections

##### Connecting the LCD Pins
• Connect the Anode Pin of the LCD into the GND of the Arduino.
• Connect the Cathode Pin of the LCD into the 5V Pin of the Arduino.
• Connect the PIN D7 of the LCD into the PIN 13 of the Arduino.
• Connect the PIN D6 of the LCD into the PIN 12 of the Arduino.
• Connect the PIN D5 of the LCD into the PIN 11 of the Arduino.
• Connect the PIN D4 of the LCD into the PIN 10 of the Arduino.
• Connect Vss PIN of the LCD into the GND of the Arduino.
• Connect the VDD pin of the LCD into the 5v pin of the Arduino.
• Connect the Contrast pin of the LCD into the GND pin of the Arduino via a 10K ohm Resistor or a 10K potentiometer.
• Connect the RS pin of the LCD to the PIN 4 of the Arduino.
• Connect the RW pin of the LCD to the GND of the Arduino.
• Connect the Enable pin of the Arduino to the PIN 6 of the Arduino.
##### Connecting the HC-SR04 Temperature Sensor
• Connect the Vss of the Sensor to the 5v pin of the Arduino Board.
• Connect the trig of the Sensor to the PIN 7 of the Arduino Board.
• Connect the echo of the sensor to the PIN 5 of the Arduino Board.
• Connect the GND of the Sensor to the PIN GND of the Arduino Board.

Edit the following code into the Arduino Editor :

``````#include <LiquidCrystal.h>
const int echopin = 5;
const int trigpin = 7;
LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
void setup() {
pinMode(echopin, INPUT);
pinMode(trigpin, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
delay(1000);
}
void loop() {
int distance = ping(echopin);
lcd.setCursor(0, 0);
lcd.print("Object Distance is:");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print("cm");
delay(500);
}
int ping(int echopin) {
long duration, distance;
pinMode(trigpin, OUTPUT);
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(5);
digitalWrite(trigpin, LOW);
pinMode(echopin, INPUT);
duration = pulseIn(echopin, HIGH);
distance = convert(duration);
return distance;
}
long convert(long microseconds) {
return microseconds / 29 / 2;
}``````

Program Analysis :

• `#include <LiquidCrystal.h>` is to include the LCD library.
• `const int ehcopin=5;` is to declare an integer variable `echopin` and is assigned to PIN 5.
• `const int trigpin=5;` is to declare an integer variable `trigpin` and is assigned to a PIN 7.
• `LiquidCrystal lcd()` is the function where we declared that the PIN 4, 6, 10, 11, 12 and 13 will be used.
• Inside the `void setup()` function, we have set that, the `echopin` and `trigpin` will be input and output respectively.
• And we induced a delay of 1000 milliseconds.
• Inside the function` void loop()`, we have `int distance= ping(echopin);` , here `ping()` is the function which returns the input which is generated in the Sensor pin `trig` after converting the time into centimeter, using the function `convert()`.
• Now, the variable `distance` inside the function `void loop()`, will contain the actual distance of the object.
• The function `convert()`, divided the time by 29 and then by 2 to convert into the distance.

## Project-Distance based temperature Sensing

Aim of the project: The aim of the project is to create a module which can sense distance and can activate the temperature sensor when a minimum distance is reached.

Applications of this module :

• The module can be applied to design a device which can detect the temperature of an area where humans cannot reach.
• The module can be used to sense, whether the object in front of the device is a fire or not.

Components Required :

• Arduino Uno Board
• Ultrasonic Distance Sensor HC-SR04
• Jumper Wires
• LCD Display
• DHT11 Temperature Sensor
• Active Buzzer Alarm

#### Step 1: Connecting the LCD Module into the Arduino • Connect the Anode Pin of the LCD into the GND of the Arduino.
• Connect the Cathode Pin of the LCD into the 5V Pin of the Arduino.
• Connect the PIN D7 of the LCD into the PIN 13 of the Arduino.
• Connect the PIN D6 of the LCD into the PIN 12 of the Arduino.
• Connect the PIN D5 of the LCD into the PIN 11 of the Arduino.
• Connect the PIN D4 of the LCD into the PIN 10 of the Arduino.
• Connect Vss PIN of the LCD into the GND of the Arduino.
• Connect the VDD pin of the LCD into the 5v pin of the Arduino.
• Connect the Contrast pin of the LCD into the GND pin of the Arduino via a 10K ohm Resistor or a 10K potentiometer.
• Connect the RS pin of the LCD to the PIN 4 of the Arduino.
• Connect the RW pin of the LCD to the GND of the Arduino.
• Connect the Enable pin of the Arduino to the PIN 6 of the Arduino.

#### Step 2: Connecting the Ultrasonic Sensor(HC-SR04) • Connect the Vss of the Sensor to the 5v pin of the Arduino Board.
• Connect the trig of the Sensor to the PIN 7 of the Arduino Board.
• Connect the echo of the sensor to the PIN 5 of the Arduino Board.
• Connect the GND of the Sensor to the PIN GND of the Arduino Board.

#### Step 3: Connecting the DHT11 • Connect the plus pin(+) of the sensor to the 5v pin of the Arduino.
• Connect the minus pin (-) of the sensor to the GND pin of the Arduino
• Connect the out pin(middle pin) of the sensor to the pin 2 of the Arduino.

Edit the following code into the Arduino Editor :

``````#include <LiquidCrystal.h>
#include <dht11.h>
#define DHT11PIN 2
const int echopin = 5;
const int trigpin = 7;
const int buzzerpin = 8;
dht11 data;
LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
void setup() {
pinMode(buzzerpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(trigpin, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
delay(1000);
}
void loop() {
int distance = ping(echopin);
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Object Distance is:");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print("cm");
if (distance < 50) {
pinMode(buzzerpin, OUTPUT);
digitalWrite(ledpin, HIGH);
pinMode(DHT11PIN, INPUT);
lcd.setCursor(0, 0);
lcd.print("Calculating Temperature:");
lcd.setCursor(0, 1);
lcd.print((float) data.temperature);
delay(50);
}
if (distance > 50)
digitalWrite(buzzerpin, LOW);
delay(500);
}
int ping(int echopin) {
long duration, distance;
pinMode(trigpin, OUTPUT);
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(5);
digitalWrite(trigpin, LOW);
pinMode(echopin, INPUT);
duration = pulseIn(echopin, HIGH);
distance = convert(duration);
return distance;
}
long convert(long microseconds) {
return microseconds / 29 / 2;
}``````

Program Analysis :

• `#include <LiquidCrystal.h>` is to include the LCD library.
• `const int ehcopin=5;` is to declare an integer variable `echopin` and is assigned to PIN 5.
• `const int trigpin=5;` is to declare an integer variable `trigpin` and is assigned to a PIN 7.
• `const int buzzerpin=8;` is to declare an integer variable `buzzerpin` and is assigned to a PIN 8.
• `LiquidCrystal lcd()` is the function where we declared that the PIN 4, 6, 10, 11, 12 and 13 will be used.
• Inside the `void setup()` function, we have set that, the `echopin` and `trigpin` will be input and output respectively.
• And we induced a delay of 1000 milliseconds.
• Inside the function` void loop()`, we have `int distance= ping(echopin);` , here `ping()` is the function which returns the input which is generated in the Sensor pin `trig` after converting the time into centimeter, using the function `convert()`.
• Now, the variable `distance` inside the function `void loop()`, will contain the actual distance of the object.
• The function `convert()`, divided the time by 29 and then by 2 to convert into the distance.

Setting the Conditions for the Alarm and activating the DHT11 Temperature Sensor :

`if (distance < 50)` , this line checks whether the distance is less than 50 cm, if so then the following task is done:

• ` pinMode(buzzerpin, OUTPUT);` , set the `buzzerpin` as output.
• `digitalWrite(buzzerpin, HIGH);` , set the `buzzerpin` as HIGH.
• `pinMode(DHT11PIN, INPUT);` , set the DHT11PIN as Input.
• `lcd.setCursor(0,0);` , sets the cursor of the display in the first column and first rows.
• `lcd.print("Calculating Temperature");` , this line will inform the that the temperature is being calculated.
• `lcd.print((float)data.temperature);`, prints the temperature.
• `delay(50)`, delay the iteration by 50 milli-seconds.

if(distance>50), then do the following task :

• `digitalWrite(buzzerpin, LOW);` , set the `buzzerpin` to LOW, if the distance of the object is higher than 50 cm.

## Run the Project:

Simply verify the code, and then upload into the Arduino Board.