Ultrasonic Ranging Module HC-SR04 garbage on lcd

hi noob questions for my first project

I’ve tried to merge an serial output an a lcd in the same code taken from 2 websites concerning this project :

the lcd project did not work for me … http://fritzing.org/projects/arduino-ultrasonic-ranging-with-hc-sr04/
the serial one works ok, was able to see it in rs232 terminal.

here’s to code i’m trying …

#include “Ultrasonic.h”
#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 10, 9, 4, 5, 6, 7);
Ultrasonic ultrasonic(12,13);

void setup()
{
Serial.begin( 9600 );
lcd.begin(16, 2);
lcd.print(“testing…”);
}
void loop()
{
Serial.print( ultrasonic.Ranging(CM) );
Serial.println( “cm” );
delay(1000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(ultrasonic.Ranging(CM));
lcd.print(“cm”);
delay(100);
}

the lcd is somewhat scrolling garbage or weird characters, or blank completly and restart ???.

thanks for the help

I don’t know what Ultrasonic.h looks like. Perhaps you should try adding serial output to the original example and letting us know what the results are.

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, 10, 9, 4, 5, 6, 7); 
int pingPin = 13; 
int inPin = 12;

void setup() 
{ 
  Serial.begin( 9600 );
  lcd.begin(16, 2); 
  lcd.print("testing..."); 

  pinMode(pingPin, OUTPUT); 
  pinMode(inPin, INPUT); 
}

void loop() 
{
  long duration, inches, cm; 

  digitalWrite(pingPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(pingPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(pingPin, LOW);

  duration = pulseIn(inPin, HIGH);

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

  Serial.print(cm);
  Serial.println( "cm" );

  lcd.clear(); 
  lcd.setCursor(0, 0); 
  lcd.print(cm); 
  lcd.print("cm");

  delay(1000); 
}

long microsecondsToInches(long microseconds) 
{
  return microseconds / 74 / 2; 
}

long microsecondsToCentimeters(long microseconds) 
{ 
  return microseconds / 29 / 2; 
}

hi
thanks tried your code and doesn’t work on lcd nor the rs232 output
and the lcd is still garbled

oh i’m running it on a duemilanove forgot to mention it

the original code for rs232 output :

#include “Ultrasonic.h”
Ultrasonic ultrasonic( 12, 13 );

void setup()
{
Serial.begin( 9600 );
}

void loop()
{
Serial.print( ultrasonic.Ranging(CM) );
Serial.println( “cm” );

delay(1000);
}

ultrasonic.h

/*
Ultrasonic.h - Library for HR-SC04 Ultrasonic Ranging Module.
Created by ITead studio. Alex, Apr 20, 2010.
iteadstudio.com
*/

#ifndef Ultrasonic_h
#define Ultrasonic_h

#include “WProgram.h”

#define CM 1
#define INC 0

class Ultrasonic
{
public:
Ultrasonic(int TP, int EP);
long Timing();
long Ranging(int sys);

private:
int Trig_pin;
int Echo_pin;
long duration,distacne_cm,distance_inc;

};

#endif

ok got it working, i don’t understand the lcd pinout vs the arduino pins and the lcdlibrary.h

there’s a glitch in the 30 to 40 cm, don’t know why, was reported in other forums.

Hope it helps others.

// this is a reworked pinout and code, the original code wasn’t working ??
// with rs232 output while reading it on a lcd
// there’s is a glitch in the 30 to 40 cm distance, sometimes it display’s 4 digits ???
// was reported on a few web sites ???
#include <LiquidCrystal.h>
#include <Ultrasonic.h>

// initialize the library with the numbers of the interface pins, R/W tied to ground
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//tried HC-SR04 sensor on pins # (6,7, 7,8 8,9 9,10) 1,2 don’t work ???
Ultrasonic ultrasonic(9,10);

void setup()
{
Serial.begin( 9600 );
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“Ultrasonic Range”);
}

void loop()
{
Serial.print(ultrasonic.Ranging(CM));
Serial.println(“cm”);
//refresh rate in seconds, in a rs232 terminal windows
delay(2000);

lcd.clear();
// set the cursor to column 0, line 0
// start at top/left position
lcd.setCursor(0, 0);
lcd.print(ultrasonic.Ranging(CM));
// print the value in cm
lcd.print(“cm”);
delay(100);
}

Sometimes the glitch is due to no decoupling capacitors between 5V and ground near the sonic ranger. Stick a capacitor of 1uF the glitch may go away.

A couple posts that may be helpful:

http://liudr.wordpress.com/2011/02/12/arduino-parking-sensor/

http://liudr.wordpress.com/2011/03/02/cross-talk/

Hey i was wondering if anyone has tried to get mm resolution from this sensor, i wanted to get more accurate readings from these sensors, if there away to access the actusl delay time the sensor reads ? that way i could calculate the distance in mm. any help, examples would be great thanks

if there away to access the actusl delay time the sensor reads

If these devices are similar to the Devantech SRF04 devices, you could go poking with a scope on the PIC on the sensor. The circuits used to be available on the Web ...and it still appears to be: http://www.robot-electronics.co.uk/htm/srf04tech.htm

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(ultrasonic.Ranging(CM));
  lcd.print("cm");
  delay(100);

You do not need to set the cursor to 0,0 after clearing the LCD, this happens automatically. Also you should avoid clearing the LCD within 'loop' since that instruction takes a very long time to complete. You may have to restructure things a bit but here's the general idea.

Clear the display once, in 'setup', after displaying "testing..." for as long as you want.

Instead of clearing the display in 'loop' you should: Set the cursor to 0,0 Display enough spaces to just cover up your old data Set the cursor to 0,0 Display your new data Delay (you already have one in the part of loop that I did not display above)

Also: The 'Code' button works better for code than the 'Quote' button.

Don

pikeynoodles:
Hey i was wondering if anyone has tried to get mm resolution from this sensor, i wanted to get more accurate readings from these sensors, if there away to access the actusl delay time the sensor reads ? that way i could calculate the distance in mm.
any help, examples would be great thanks

you can do this with the Parallax Ping modules, as the device returns the pulses as durations in microseconds, correlating that to distance…so you can do the math as accurate as you need…(within reason)