Introduction
In this article I will explain about how to measure distance from any object. It can be easily display in the LCD Screen. We can use in the front door to view the distance of people.
Parts of List
- Arduino uno
- Ultrasonic Sensor (HC-SR04)
- BreadBoard
- LCD Screen
- HookUp Wires.
Ultrasonic Sensor (HC-SR04)
Figure 1: Ultrasonic Sensor(HC-SR04)
- It is used to produce the high frequency sound effect.
- It can measure the signal from the sender to the receiver.
- The echo to be determined from the object.
Where ever we can fix the Ultrasonic Sensor(HC-SR04)
- Hospital for medicine
- Industry
- Home etc.
LCD Screen
Figure 2: LCD Screen
- LCD is Liquid Crystal Display (LCD).
- It can have the flat-panel or the electronic display
- It is used in hospital, showrooms, buses, railway station, airport, etc.
Connections
Connection from the Ultrasonic Sensor (HC-SR04) to the Arduino board
In the sensor it has the following four pins; they can work under the process of the receiving and the sending of the signal.
- Vcc in the Ultrasonic Sensor can be connected to the arduino board of the 5V of Vcc.
- Triger in the Ultrasonic Sensor can be connected to the arduino board of the digital pin 12.
- Echo pin the Ultrasonic Sensor can be connected to the arduino board of the digital pin 08.
- Gnd in the Ultrasonic Sensor can be connected to the arduino board of the Gnd.
Connection from LCD Screen to the Arduino board
The LCD screen can have the following four pins.
- The Vcc can be connected to the vcc of the arduino board.
- The Gnd can be connected to the Gnd of the arduino board.
- The SDA can be connected to the arduinoboard of the analog pin A4 .
- TheSCL can be connected to the arduino board of the analog pin A5.
Before uploading the program add the library:
- Programming
- #define trigPin 12
- # define echoPin 8
-
- # define LEDPin 13
-
- # include < Wire.h >
- #include < LCD.h >
- #include < LiquidCrystal_I2C.h >
- #define I2C_ADDR 0x3F
-
- # define BACKLIGHT_PIN 3
- # define En_pin 2
- # define Rw_pin 1
- # define Rs_pin 0
- # define D4_pin 4
- # define D5_pin 5
- # define D6_pin 6
- # define D7_pin 7
-
- int n = 1;
- LiquidCrystal_I2Clcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
- void setup()
- {
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(LEDPin, OUTPUT);
- lcd.begin(20, 4);
-
-
- lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
- lcd.setBacklight(HIGH);
- lcd.home();
- }
- void loop()
- {
- int duration, distance;
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(100);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration / 2) / 29.1;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Distance from object");
- lcd.setCursor(0, 1);
- lcd.print(distance);
- lcd.print("cm");
- if (distance >= 15)
- {
- lcd.setCursor(0, 4);
- lcd.print("Safe Zone :)");
- digitalWrite(LEDPin, HIGH);
- delay(500);
- digitalWrite(LEDPin, LOW);
- delay(500);
- }
- else
- {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("STEP AWAY!!!");
- lcd.setCursor(0, 1);
- lcd.print("STEP AWAY!!!");
- lcd.setCursor(0, 2);
- lcd.print("STEP AWAY!!!");
- lcd.setCursor(0, 3);
- lcd.print("STEP AWAY!!!");
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
- delay(50);
- digitalWrite(LEDPin, HIGH);
- delay(50);
- digitalWrite(LEDPin, LOW);
-
- }
- }
Explanation
In this case it can send and receive signal from the ultrasonic sensor, when the object is near it can be displayed in the LCD screen.
When the object is near it can detect the SAFE ZONE message in the LCD screen.
When the object is away it can detect the STEP AWAY message in the LCD screen.
Output
Figure 3:Output
Read more articles on Arduino: