How to interface Ultrasonic sensor with Arduino UNO (Arduino Series - Part 05)
Display the results from the HC-SR04 Ultrasonic Sensor on an LCD Display
Hardware Required:
- Arduino Board
- Ultrasonic Sensor HC-SR04
- LCD Display
- 1k ohm resistor
- Breadboard and wires
Ultrasonic Sensor HC-SR04 Connections:
- The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.
- The HC-SR04 sensor attach to the Breadboard
- The Sensor VCC connect to the Arduino Board +5V
- The Sensor GND connect to the Arduino Board GND
- The Sensor Trig connect to the Arduino Board Digital I/O 10
- The Sensor Echo connect to the Arduino Board Digital I/O 11
Look the basic tutorial about the HC-SR04 https://youtu.be/OFlt5vgCzZs
LCD Display Connection:
Before wiring the LCD screen to your Arduino or Genuino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen.
To wire your LCD screen to your board, connect the following pins:
LCD VSS pin to Arduino GND
LCD VDD pin to Arduino 5V
LCD VO pin to 1k ohm resistor
LCD RS pin to digital pin 2
LCD RW pin to Arduino GND
LCD Enable pin to digital pin 3
LCD D4 pin to digital pin 4
LCD D5 pin to digital pin 5
LCD D6 pin to digital pin 6
LCD D7 pin to digital pin 7
The 1k ohm resistor's other legs connect to GND
For the backlight of the display, pin 15 (A+) and 16 (K-) of the LCD connect to +5V and GND
Look the basic tutorial about the LCD Display https://www.youtube.com/watch?v=Sv3ptHWxpA4&t=11s
Code:
- The LiquidCrystal library allows you to control LCD displays that are compatible.
- First you have to define the Trig and Echo pins. In this case they are the pins number 9 and 10 on the Arduino Board and they are named trigPin and echoPin. Then you need a Long variable, named “duration” for the travel time that you will get from the sensor and an integer variable for the distance.
- In the setup you have to define the trigPin as an output and the echoPin as an Input and also start the serial communication for showing the results on the serial monitor.
- If the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.
#include 'LiquidCrystal.h' //Please replace the single quote characters ('') with the parenthesis character (<>)
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print("inch");
delay(10);
bhai gsm aur gps connection with arduino
ReplyDeleteka code dete to mja aa jata...
It's not working LCD is showing white cubes
ReplyDeleteAdjust the brightness of lcd using pot
DeleteHow to get library funcition of liquid crystal.
ReplyDelete