0

Wondering if it's the sensor or the code? can anyone help?

I've used the code here

/*
HC-SR04 Ultrasonic Sensor with LCD dispaly

HC-SR04 Ultrasonic Sensor
  VCC to Arduino 5V
  GND to Arduino GND
  Echo to Arduino pin 12
  Trig to Arduino pin 13

LCD Display (I used JHD162A) 
  VSS to Arduino GND
  VCC to Arduino 5V
  VEE to Arduino GND
  RS to Arduino pin 11
  R/W to Arduino pin 10
  E to Arduino pin 9
  DB4 to Arduino pin 2
  DB5 to Arduino pin 3
  DB6 to Arduino pin 4
  DB7 to Arduino pin 5
  LED+ to Arduino 5V
  LED- to Arduino GND

Modified by Ahmed Djebali (June 1, 2015).
*/
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(11,10,9,2,3,4,5);  //Create Liquid Crystal Object called LCD

#define trigPin 13 //Sensor Echo pin connected to Arduino pin 13
#define echoPin 12 //Sensor Trip pin connected to Arduino pin 12

//Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly 
//URL:

void setup() 
{  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
  LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
  LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(distance); //Print measured distance
  LCD.print(" cm");  //Print your units.
  delay(250); //pause to let things settle
}
  • Is the LCD working correctly? Have you tried displaying the values over Serial? Also you can test with the examples, that comes with the NewPing library. If with the example it still not works, there is something wrong with the wiring or the sensor. Then you should show us the wiring. And you could test, if the sensor outputs anything. – chrisl Sep 30 at 17:47
  • I bet it's the sensor or the way you wired it. Add a Serial.println(duration); to eliminate any other overseen errors. Does that "0" appear every 0.25 sec or every 1.25 sec only? ( Which indicates a pulseIn timeout ) – DataFiddler Oct 1 at 11:34
  • This is my first project and I am new to the arduino and programming stuff, and yes the LCD is working perfectly.When I try to display values on the Serial monitor nothing shows up but the LCD it displays- Target distance=0cm and when I remove the hcsr04 it displays values like 180,179,178 without the sensor.I've used the schematics from this site instructables.com/id/Arduino-LCD-Project-for-Measuring-Distance/… is there a problem with my code? – Tanish Oct 1 at 12:36
  • Where should I add Serial.println(duration); before the LCD.print(distance); or after the LCD.print(" cm"); ? – Tanish Oct 1 at 12:39
  • The position is not important, just after pulseIn. I don't see an obvious error with your code. The advises, that we can currently give you, are meant for debugging - finding the error. – chrisl Oct 1 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.