How To Ultrasonics Sensor With LCD

Required Hardware

For users who do not have Hackerspace Karachi Arduino Kit v1.0

  • Arduino Board
  • Breadboard
  • Ultrasonic Sensor (HC-SR04)
  • L293D driver IC
  • Hook-up Wires

Circuit

For users who do not have Hackerspace Karachi Arduino Kit v1.0

Schematic

Code

                    

1. /*
2. Ultrasonic Sensor with LCD
3. This example shows how to use Ultrasonic Sensor with LCD
4. The circuit:
5. -Ultrasonic sensor
6. echo pin connected to digital pin 13 of Arduino
7. trig pin connected to digital pin 12 of Arduino
8. Vcc pin connected to Vcc pin of Arduino
9. Gnd pin connected to Gnd pin of Arduino
10. -LCD
11. SDA connected to analog pin A4.
12. SCL connected to analog pin A5.
13.
14. http://hackerspacekarachi.org/
15. */
16. //Library version:1.1
17. #include <Wire.h>
18. #include <LiquidCrystal_I2C.h>
19.
20. #define echoPin 13 // define output pin
21. #define trigPin 12 //define input pin
22.
23. long duration; //store time taken for wave to trvel
24. int distance; // store distance travel by wave
25. LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2
line display
26.
27. void setup() {
28.
29. pinMode(trigPin, OUTPUT); //read and store output
30. pinMode(echoPin, INPUT); //read and store input
31. lcd.init(); // initialize the lcd
32. lcd.init();
33. lcd.backlight();
34. }
35.
36. void loop() {
37.
38. digitalWrite(trigPin, LOW); //trigger object with delay of 2micsec
39. delayMicroseconds(2);
40. digitalWrite(trigPin, HIGH);
41. delayMicroseconds(10);
42. digitalWrite(trigPin, LOW);
43.
44. duration = pulseIn(echoPin, HIGH); //time taken for the wave to travel
45. distance = (duration * 0.034 / 2); //distance divided by 2 for incoming
46. // outgoing waves
47. lcd.setCursor(0, 1);
48. lcd.print(“Distance : “); //Display results
49. lcd.setCursor(11, 1);
50. lcd.print(distance);
51. lcd.setCursor(13, 1);
52. lcd.print(” cm “);
53. delay(1000);
54. lcd.clear();
55. }

1. /*