Advertisement
marlonungos

Arduino: Ultrasonic sensor HC-SR04 with LCD I2C

Mar 31st, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h> //Link of library: https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c
  4.  
  5. #define BUZZER 5 //BUZZER
  6.  
  7. int trigPin = 11;    // Trigger
  8. int echoPin = 12;    // Echo
  9. long duration, cm, inches;
  10.  
  11. //Define LCD pinout
  12. const int  en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
  13. //Define I2C Address - change if reqiuired
  14. const int i2c_addr = 0x27;
  15. // Define LCD display connections
  16. LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
  17.  
  18. void setup() {
  19.   //Serial Port begin
  20.   //Serial.begin (9600);
  21.   //Define inputs and outputs
  22.   pinMode(trigPin, OUTPUT);
  23.   pinMode(echoPin, INPUT);
  24.   noTone(BUZZER);
  25.  
  26.     // Set display type as 20 char, 4 rows
  27.   lcd.begin(20,4);
  28.   // Print a message to the LCD.
  29.   lcd.setCursor(0,0);
  30.   lcd.print("Welcome to");
  31.   lcd.setCursor(0,1);
  32.   lcd.print("Ultra Sonic Sensor");
  33.   lcd.setCursor(0,2);
  34.   lcd.print("Distance");
  35.   lcd.setCursor(0,3);
  36.   lcd.print("Mesurement");
  37.   delay(3000);
  38.   lcd.clear();
  39. }
  40.  
  41. void loop() {
  42.   // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  43.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  44.   digitalWrite(trigPin, LOW);
  45.   delayMicroseconds(5);
  46.   digitalWrite(trigPin, HIGH);
  47.   delayMicroseconds(10);
  48.   digitalWrite(trigPin, LOW);
  49.  
  50.   // Read the signal from the sensor: a HIGH pulse whose
  51.   // duration is the time (in microseconds) from the sending
  52.   // of the ping to the reception of its echo off of an object.
  53.   pinMode(echoPin, INPUT);
  54.   duration = pulseIn(echoPin, HIGH);
  55.  
  56.   // Convert the time into a distance
  57.   //char cm[],inches[];
  58.   cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  59.   inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
  60.   char bufferIn[100];
  61.   char bufferCm[100];
  62.  
  63.   Serial.print(inches);
  64.   Serial.print("in, ");
  65.   Serial.print(cm);
  66.   Serial.print("cm");
  67.   Serial.println();
  68.   delay(250);
  69.  
  70.   lcd.setCursor(0,0);
  71.   sprintf(bufferIn, "Distance: %d in.", inches);
  72.   lcd.print(bufferIn);
  73.   lcd.setCursor(0,1);
  74.   sprintf(bufferCm, "Distance: %d cm.", cm);
  75.   lcd.print(bufferCm);
  76.   //lcd.setCursor(0,1);
  77.   //lcd.print("Centimeter: %d",cm);
  78.   //lcd.setCursor(0,2);
  79.   //lcd.print("A-Maghulog ng barya");
  80.   //lcd.setCursor(0,3);
  81.   //lcd.print("B-Pumili ng Port.");
  82.   delay(100);
  83.   if (inches <= 54 && inches >= 33) {
  84.     lcd.setCursor(0,3);
  85.     sprintf(bufferIn, "Distance is LT 32in.", inches);
  86.     lcd.print(bufferIn);
  87.     delay(100);
  88.       for(int i = 0; i < 5; i++)
  89.       //for(;;)
  90.       {
  91.         //lcd.backlight();
  92.         tone(BUZZER,1000);
  93.         delay(150);
  94.         //lcd.noBacklight();
  95.         noTone(BUZZER);
  96.         delay(100);
  97.       }
  98.   }else if (inches <= 32 && inches >= 13) {
  99.     lcd.setCursor(0,3);
  100.     sprintf(bufferIn, "Distance is LT 24in.", inches);
  101.     lcd.print(bufferIn);
  102.     delay(250);
  103.       for(int i = 0; i < 5; i++)
  104.         //for(;;)
  105.         {
  106.           //lcd.backlight();
  107.           tone(BUZZER,500);
  108.           delay(100);
  109.           //lcd.noBacklight();
  110.           noTone(BUZZER);
  111.           delay(100);
  112.         }
  113.   }else if (inches <= 12) {
  114.     lcd.setCursor(0,3);
  115.     sprintf(bufferIn, "Distance is LT 24in.", inches);
  116.     lcd.print(bufferIn);
  117.     delay(100);
  118.     tone(BUZZER,2000);
  119.     delay(10);
  120.   }else{
  121.     lcd.backlight();
  122.     lcd.setCursor(0,3);
  123.     sprintf(bufferIn, "Distance is GT 21in.", inches);
  124.     lcd.print(bufferIn);
  125.     delay(100);
  126.     noTone(BUZZER);
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement