Advertisement
DjKammo

detect

Oct 20th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. /*
  2. HC-SR04 Ultrasonic Sensor with LCD dispaly
  3.  
  4. HC-SR04 Ultrasonic Sensor
  5.   VCC to Arduino 5V
  6.   GND to Arduino GND
  7.   Echo to Arduino pin 12
  8.   Trig to Arduino pin 13
  9.  
  10. LCD Display (I used JHD162A)
  11.   VSS to Arduino GND
  12.   VCC to Arduino 5V
  13.   VEE to Arduino GND
  14.   RS to Arduino pin 11
  15.   R/W to Arduino pin 10
  16.   E to Arduino pin 9
  17.   DB4 to Arduino pin 2
  18.   DB5 to Arduino pin 3
  19.   DB6 to Arduino pin 4
  20.   DB7 to Arduino pin 5
  21.   LED+ to Arduino 5V
  22.   LED- to Arduino GND
  23.  
  24. Modified by Ahmed Djebali (June 1, 2015).
  25. */
  26. #include <LiquidCrystal.h> //Load Liquid Crystal Library
  27. LiquidCrystal LCD(11,12,9,2,3,4,5);  //Create Liquid Crystal Object called LCD
  28.  
  29. #define trigPin 6 //Sensor Echo pin connected to Arduino pin 13
  30. #define echoPin 7 //Sensor Trip pin connected to Arduino pin 12
  31.  
  32. //Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly
  33. //URL:
  34.  
  35. void setup()
  36. {  
  37.   pinMode(trigPin, OUTPUT);
  38.   pinMode(echoPin, INPUT);
  39.  
  40.   LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
  41.   LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
  42.   LCD.print("Target Distance:");  //Print Message on First Row
  43. }
  44.  
  45. void loop() {
  46.   long duration, distance;
  47.   digitalWrite(trigPin, LOW);
  48.   delayMicroseconds(2);
  49.   digitalWrite(trigPin, HIGH);
  50.   delayMicroseconds(10);
  51.   digitalWrite(trigPin, LOW);
  52.   duration = pulseIn(echoPin, HIGH);
  53.   distance = (duration/2) / 29.1;
  54.  
  55.   LCD.setCursor(0,1);  //Set cursor to first column of second row
  56.   LCD.print("                "); //Print blanks to clear the row
  57.   LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  58.   LCD.print(distance); //Print measured distance
  59.   LCD.print(" cm");  //Print your units.
  60.   delay(250); //pause to let things settle
  61. }
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement