0

When I had the code run just the LCD it worked perfectly fine, but when I added an HC-SR04 without changing the text it showed weird characters. All I had the code do with the HC-SR04 was print it in the serial monitor.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 5, e = 4, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
const int trigPin = A0;
const int echoPin = A1;
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);

void setup() {
  // set up the LCD's number of columns and rows:
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("zach is cool");
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  long duration, inches, cm;
  lcd.setCursor(0, 1);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance traveled.
  return microseconds / 29 / 2;
}
0

2 Answers 2

3

It looks like you have the LCD connected to pins 0 and 1. Those are the Serial Rx and Tx pins. When you also use Serial in your sketch it creates a conflict. Move those two lines from the LCD to different pins and update the sketch.

2
  • it was the Tx and Rx pins Thank You! Mar 3 at 5:00
  • Thanks. Please mark as solution if it fixed the problem.
    – Delta_G
    Mar 3 at 8:36
0

With the following code, I ran an HCSR04 and a 16x2 LCD with Arduino UNO. It ran successfully.

#define trigPin 8
#define echoPin 9
#define pinIR 7
// Define variables:
float duration=0.00;
float distance=0.00;
int visitor,sum,threshold,temp;
 
// declaring variables to hold the new and old IR states
boolean oldIRState = LOW;
boolean newIRState = LOW;
 
 
#include <LiquidCrystal.h>
 
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
  // Define inputs and outputs:
  pinMode(pinIR, INPUT);
  pinMode(trigPin, OUTPUT);
 
  pinMode(echoPin, INPUT);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Visitor=");
  lcd.print(visitor);
 
  //Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}
 
void loop() {
  // Clear the trigPin by setting it LOW:
  newIRState = digitalRead(pinIR);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
 
  // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance = duration /58.82 ;
 
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");
 
  delay(1000);
  if(distance<=10 && abs(temp)>=70)
  {
    visitor++;
    lcd.clear();
    lcd.print("Visitor=");
    lcd.print(visitor);
 
 
  for(int i=0;i<500;i++)
 {
 
 sum +=distance;
 delay(5);
 
 }
 threshold = sum/500;
  }
 
temp=threshold-distance;
 
 if ( newIRState != oldIRState )
  {
 
    if ( newIRState == LOW && visitor>0 )
    {
 
        visitor--;
         lcd.clear();
    lcd.print("Visitor=");
    lcd.print(visitor);
 
      }
 
    }
    oldIRState = newIRState;
 
 
}

You can follow this code and circuit arrangement. I also suggest you check the HCSR04 and LCD separately with The Arduino. You can also check this Arduino UNO pinout configuration guideline This will be helpful for any Arduino UNO project you work on.

3
  • Thank you, I will try this. I tried simplifing the code to just the LCD and it didnt work either so that is probably the issue. Feb 29 at 2:03
  • can you explain to me the IRstate and Is and extra stuff you added? I'm new to this and I don't understand the new code. Feb 29 at 7:15
  • Actually, there was two sensors in my setup. One was the HCSR04, another was the IR obstacle sensor. I was making a visitor counter. The HCSR04 was counting people entering the room. IR sensor was counting people exiting the room. When you are working with the HCSR04, you do not need the variables associated with the IR sensor. You can get a simplified example here: theengineeringprojects.com/2017/08/… This one only features an Arduino UNO and an HCSR04.
    – liaifat85
    Mar 4 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.