Go Down

Topic: Issue using HC-SR04 with Uno (Read 1 time) previous topic - next topic


Aug 25, 2013, 04:48 am Last Edit: Aug 25, 2013, 04:51 am by zenlar Reason: 1
I have an ultrasonic distance sensor (HC-SR04) attached to an Arduino Uno. Pins 11, 12, and 13 are attached to VCC, Trigger, and Echo respectively and the Ground pin is grounded. All the ping sketches I've found online result in a distance reading of zero no matter where i point the sensor. In particular the following sketch:

Code: [Select]

#define trigPin 12
#define echoPin 13
#define VCC 11

void setup() {
 pinMode(VCC, OUTPUT);
 digitalWrite(VCC, HIGH);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Serial.begin (9600);

void loop() {
 digitalWrite(VCC, HIGH);
 long duration, distance;
 // Send trigger pulse
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin, LOW);
 // Get pulse length
 duration = pulseIn(echoPin, HIGH);
 // Convert and print distance
 Serial.print(" us - ");
 distance = (duration/2) / 29.1;
 Serial.println(" cm");
 // Every 100 ms

Also the sketch found here http://forum.arduino.cc/index.php/topic,106043.0.html results in similar output, but with 19 microseconds being the most common return value as opposed to 23 us as seen in the attached screenshot.

The sensors sounds like it's working - I hear a high frequency clicking noise coming from the speakers. I have also tried setting the trigger pulse length to various values including 3, 5, 10, 12, and 15 microseconds but all have the same result. The product page indicates that a pulse of 10 microseconds is required to activate the ping.


.. attached to an Arduino Uno. Pins 11, 12, and 13 ...

Pin 13 is connected to the LED and that can disturb your readings. (IIRC the last UNO version behaves better)

Please try it without using PIN13.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Using pins 2-5 as VCC, Trig, Echo, and GND respectively I get similar output but with 28 microseconds constantly.

Also, It's a brand new Arduino Uno R3 and HC-SR04. At the moment I do not have a breadboard and jumper wires, but once I have them I plan on connecting GND and VCC directly to the GND and 5V pins instead of coding them on pins. Is using the following code for my setup method any different from connecting GND and VCC directly into the GND and 5V pins on the board?:

Code: [Select]

#define trigPin 3
#define echoPin 4
#define VCC 2
#define GND 5

void setup() {
  pinMode(VCC, OUTPUT);
  digitalWrite(VCC, HIGH);
  pinMode(GND, OUTPUT);
  digitalWrite(GND, LOW);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin (9600);


I think that your problem is using signal pins as supply pins, they have a maximum current rating and the pulsing of the sensor will not ensure it is getting full voltage.
Wait until your proto board arrives and I think your problem will be solved.
Everything runs on smoke, let the smoke out, it stops running....

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131