Pages: [1]   Go Down
Author Topic: Issue using HC-SR04 with Uno  (Read 377 times)
0 Members and 1 Guest are viewing this topic.
Boston
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(12);
  digitalWrite(trigPin, LOW);
  
  // Get pulse length
  duration = pulseIn(echoPin, HIGH);
  
  // Convert and print distance
  Serial.print(duration);
  Serial.print(" us - ");
  distance = (duration/2) / 29.1;
  Serial.print(distance);
  Serial.println(" cm");
  
  // Every 100 ms
  delay(100);
}

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.


* Screenshot from 2013-08-24 22:05:10.png (45.81 KB, 618x814 - viewed 15 times.)
« Last Edit: August 24, 2013, 09:51:05 pm by zenlar » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
.. 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.

Logged

Rob Tillaart

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

Boston
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}


* 2013-08-25 11.16.31.jpg (2785.22 KB, 3264x2448 - viewed 22 times.)
Logged

Victoria, Australia
Offline Offline
God Member
*****
Karma: 24
Posts: 989
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Tom
Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Pages: [1]   Go Up
Jump to: