Ardiuno Code For Measuring Distance




Display the results from the HC-SR04 Ultrasonic Sensor on an LCD Display With  Arduino  ->

Hardware Required:

-  Arduino Board
-  Ultrasonic Sensor HC-SR04
-  LCD Display
-  1k ohm resistor
-  Wires

Here's the circuit diagram to connect ardiuno to display and ultrasonic sensor

_
_
__
_
_
__



Ultrasonic Sensor Connections with Arduino:


-  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 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  4
- The Sensor Echo connect to the Arduino Board Digital  2





LCD Display Connection :



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 7
LCD RW pin to Arduino GND
LCD Enable (E) pin to digital pin 9
LCD D4 pin to digital pin 10
LCD D5 pin to digital pin 11
LCD D6 pin to digital pin 12
LCD D7 pin to digital pin 13
The 1k ohm resistor's other legs connect to GND
For the backlight of the display, pin 15 and 16  of the LCD connect to +5V and GND








First Connect your arduino to PC
Run This Code in  arduino ide

Source-Code


#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 9, 10, 11, 12, 13);

const int trigPin = 4;
const int echoPin = 2;
long duration;
int distanceCm, distanceInch;

void setup() {
 
lcd.begin(16,2);
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("Dist:");                    // 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("Dist:");
lcd.print(distanceInch);
lcd.print("   in");
delay(10);

}




: ) if you get any type of error then Contact Us : )

Ardiuno Code For Measuring Distance Ardiuno Code For Measuring Distance Reviewed by Coding Guy on October 31, 2019 Rating: 5

No comments:

Powered by Blogger.