Advertisement
Tempist

SurilliGSM Ultrasonic [HC-SR04 and 16x2 LCD] [Arduino setup]

Jul 10th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Twitter: @ItsWizardly | Instagram: @ImWizardly | Discord: wizard#7815
  2.  
  3. #include <LiquidCrystal.h> // Include the LiquidCrystal Library.
  4. LiquidCrystal lcd(6, 11, 5, 12, 2, 3); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7).
  5. const int trigPin = 9;
  6. const int echoPin = 10;
  7. long duration;
  8. int distanceCm, distanceInch;
  9. void setup()
  10. {
  11.    lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display.
  12.    pinMode(trigPin, OUTPUT);
  13.    pinMode(echoPin, INPUT);
  14. }
  15. void loop()
  16. {
  17.    digitalWrite(trigPin, LOW);
  18.    delayMicroseconds(2);
  19.    digitalWrite(trigPin, HIGH);
  20.    delayMicroseconds(10);
  21.    digitalWrite(trigPin, LOW);
  22.    duration = pulseIn(echoPin, HIGH);
  23.    distanceCm= duration*0.034/2;
  24.    distanceInch = duration*0.0133/2;
  25.    lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed.
  26.    lcd.print("Distance: "); // Prints string "Distance" on the LCD.
  27.    lcd.print(distanceCm); // Prints the distance value from the Ultrasonic Sensor (HC-SR04).
  28.    lcd.print(" cm");
  29.    delay(10);
  30.    lcd.setCursor(0,1);
  31.    lcd.print("Distance: ");
  32.    lcd.print(distanceInch);
  33.    lcd.print(" inch");
  34.    delay(10);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement