Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04
Software & Tools
Arduino IDE
Project description
Code
Arduino Ultrasonic Code with 16x2 LCD
c
This code reads values from US sensor and displays
1#include <Wire.h> 2#include <LiquidCrystal_I2C.h> 3LiquidCrystal_I2C lcd(0x27,16,2); 4const int trig=4; 5const int echo=5; 6void setup() { 7 // put your setup code here, to run once: 8 lcd.begin(); 9 lcd.backlight(); 10 lcd.print("Distance : "); 11 pinMode(trig,OUTPUT); 12 pinMode(echo,INPUT); 13 Serial.begin(9600); 14} 15void loop() { 16 // put your main code here, to run repeatedly: 17 digitalWrite(trig,LOW); 18 delay(2); 19 digitalWrite(trig,HIGH); 20 delay(10); 21 digitalWrite(trig,LOW); 22 long duration = pulseIn(echo,HIGH); 23 long distance=duration*0.034/2; //speed of sound=340 m/s =0.034 cm/microsecond. 24 lcd.setCursor(10,0); 25 lcd.print(" "); 26 lcd.setCursor(10,0); 27 lcd.print(distance); 28 Serial.print("Distance : "); 29 Serial.println(distance); 30 Serial.print(" cm") 31 delay(10); 32}
Comments
Only logged in users can leave comments