HC-SR04 is not working in arduino but in raspberrypiB+ is running fine

Hi folks,

I am little bit confused. I tried HC-SR04 in two arduino uno boards with basic instructions how to connect that for echo and trigger, vcc and ground. But result is still same distance is returned 3555 cm. I know that HC-SR04 worked fine with raspberrypib+, I was able to program that sensor and measured wall 4 meters away.
It is not giving sense, that with raspPi is fine and with arduino is not working.
NewPing library with example was tested too, but same result.

const int echoPin = 12; // Echo Pin
const int trigPin = 11; // Trigger Pin

long duration, distance; // Duration used to calculate distance


void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 
 //digitalWrite(trigPin, LOW);
}

void loop() {
 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(15); 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW);
 
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 Serial.println("duration:");
 Serial.println(duration);
 //Calculate the distance (in cm) based on the speed of sound.
 //distance = (duration*0.000001)*332;
 distance = (duration/2)/58;
 Serial.println("distance:");
 Serial.println(distance);
 
 
 //Delay 250ms before next reading.
 delay(250);
}

Looks like is sensor wrong. I have tested again with raspberrypi and the same result 35 meters... So wrong cheap sensor. I can go sleep right now better..

Hi Luke,

I bought 2 of the HC-SRO4 Ultrasonics sensors and they are great.

The NewPing Library & demos are good but a bit confusing, so I am using the original Ultrasonic library and code examples, which is available from:

Ultrasonic_Ranging_Sensor_Module

Here is an example of the default Sketch which I have tested & modified repeatedly on my Arduino Duemilanove and two Uno's.

Default sketch:

#include <Ultrasonic.h>
Ultrasonic ultrasonic(12,13);//Init an Ultrasonic object
int Distance;
void setup() {
Serial.begin(9600);
}

void loop()
{
 Distance=ultrasonic.Ranging(CM);//get the current result;
 delay(100);
 Serial.print("the distance is ");
 Serial.println(Distance);
 delay(1000);
}

Modified Sketch, which also features an LED on Pin 13, to show the thing works:

// *** INCLUDES ***

#include <Ultrasonic.h>

// *** INITIALISE OBJECTS ***

int pin = 13;
Ultrasonic ultrasonic(2,3); // Use Pins 2,3 due to SPI needs if Ethernet Shield connected;
int Distance;

// *** SETUP ***
void setup() {
  pinMode(pin, OUTPUT);
  Serial.begin(9600);
}

// *** LOOPS ***
void loop() {

  Distance=ultrasonic.Ranging(CM); // Get the current result;
  delay(50); // Pause for 1/20 of a sec;
  
  if (Distance < 100) // Centimetres
  {
  digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(50);
  Serial.print("Distance:");
  Serial.print(Distance);
  Serial.println(":cm");
  delay(50);
  }
}

After testing with these sensors, if you have them in a large open space such as a lounge or workshop with nothing in front of the two transducers, you should get consistent measurements out to 5M (500) in the serial port. If you stand in front of them, it takes a few seconds for the beams to consolidate and adjust so the initial measurements as seen on the Serial Monitor, will look flakey.

If your measurements seem to go to 3550 etc, that is the Ultrasonic sensor throwing a wobbly, let it sit and go again. Hope this helps.