Go Down

Topic: Need help getting my ultra sound distance measurement and lcd working (Read 126 times) previous topic - next topic

johnboyman

Hello all. I am new to arduino but not programming. I have accomplished some projects now. I am working on with a HC-SR04 a standard lcd and an elegoo but will get an arduino soon. I am using this following tutorials.

https://howtomechatronics.com/tutorials/arduino/lcd-tutorial/

and

https://simple-circuit.com/arduino-hc-sr04-sensor-distance-meter/

The first one is how I hooked up my lcd it is working fine. The second url has added stuff like the ultra sound sensor.


The following code is what I have compiled and run on my elegoo.



Code: [Select]
[code]

#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

#define  trigger_pin  15                          // HC-SR04 trigger pin is connected to Arduino A1
#define  echo_pin     14                          // HC-SR04 echo pin is connected to Arduino A0
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 9;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {

  pinMode(trigger_pin, OUTPUT);
  pinMode(echo_pin, INPUT);
  digitalWrite(trigger_pin, LOW);
 
  lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }

  lcd.print("Distance"); // Prints "Arduino" on the LCD
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 unsigned long duration;
unsigned int  distance;
void loop(){

 
  // Send 10us pulse to HC-SR04 Trigger pin
  digitalWrite(trigger_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger_pin, LOW);
 
  // Read pulse comes from HC-SR04 Echo pin
  duration = pulseIn(echo_pin, HIGH);           // Read echo pulse width
  if(duration == 0) {                           // Previous function returns 0 if timeout
    lcd.setCursor(2, 1);
    lcd.print("  Time Out  ");
  }
  else {
    distance = duration / 58;                   // Calculate the distance in cm
    if(distance > 400) {                        // HC-SR04 module can measure up to 400cm
      lcd.setCursor(2, 1);
      lcd.print("Out of Range");
    }
    else {
      lcd.setCursor(2, 1);
      lcd.print("       cm   ");
      lcd.setCursor(5, 1);
      lcd.print(distance);
    }
  }
  delay(100);   


 
}

[/code]



The output result is a few machine letters that move around the lcd screen to many different locations I uploaded an image of what the letters say. I cannot understand them I was hoping that someone would understand what it means so maybe I can figure out what is wrong.


johnboyman


groundFungus

Code: [Select]
LiquidCrystal lcd(1, 2, 4, 5, 6, 7)
It is unwise to use pin 1 for the LCD.  It is the TX pin for hardware serial.

I wired a LCD and HCSR04 to my Uno and tried your code after changing the LCD constructor to:
Code: [Select]
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
And changing the LCD wiring to match.   The top line of the LCD reads "Distance" and on the second line the range in cm or "Out of Range".
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

johnboyman

Waw totally big help. now that i did what you said it is 100% working the way i wanted. thanks alot.

Go Up