5

I am doing a basic project in Arduino UNO connecting an Ultra Sonic sensor (HC-SR04) which should print in the serial monitor the distance of the closest object but it always print 0.

This is my code:

long distance;
long time;

void setup(){
  Serial.begin(9600);
  pinMode(4, OUTPUT); 
  pinMode(2, INPUT); 
}

void loop(){
  digitalWrite(2,LOW);
  delayMicroseconds(5);
  digitalWrite(2, HIGH);
  delayMicroseconds(10);

  time = pulseIn(4, HIGH);
  distance = int(0.017*time); 

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm.");
  delay(1000);
}

And this is the breadboard:

enter image description here

ianaya89
  • 4,153
  • 3
  • 26
  • 34
  • just curious, do you have an `int()` function defined somewhere? – KevinDTimm Jun 29 '15 at 21:24
  • No I don't. Should I? – ianaya89 Jun 29 '15 at 21:26
  • your computation `distance = int(.0.017*time)` would suggest so. see https://www.arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor for an example code. – KevinDTimm Jun 29 '15 at 21:26
  • Here http://www.instructables.com/id/Ultrasonic-Range-detector-using-Arduino-and-the-SR/ too – KevinDTimm Jun 29 '15 at 21:31
  • @KevinDTimm If I remove the ```int``` function the issue persists. I tried to follow that post but the author uses a different sensor (it has 3 pins). I have also tried with different pieces of codes that I found in blogs and the issue is the same. – ianaya89 Jun 29 '15 at 21:31
  • @KevinDTimm I will try with those instructions. Thanks! – ianaya89 Jun 29 '15 at 21:33
  • @KevinDTimm, The Arduino library has the `int()` function. Which "Converts a value to the int data type". It's weird, but it does actually exist. – embedded_guy Jun 29 '15 at 23:44
  • @embedded_guy - thanks. I've done a little arduino but, being a C guy, I wouldn't call the function I'd cast the result :) – KevinDTimm Jun 30 '15 at 12:53
  • @KevinDTimm - I am with you on that. I think that using a function like `int()` just adds confusion. I have coded a lot in C and I think it is more straightforward to cast the result. – embedded_guy Jun 30 '15 at 15:10

2 Answers2

2

The primary issue that I see is that your code doesn't match your wiring diagram.

For example, your diagram shows Trig connected to pin 4. The Trig should be the output from your Arduino but you have it defined as an input.

The Echo is connected to pin 2 and it should be an input, but you have it defined as an output.

Finally, in your loop(), you are not even using pin 2 or pin 4, but pins 9 and 8. Another issue is the timing you use in setting the trigger pulse - it does not match the datasheet. I would do something like this (assuming that you are actually connected to the pins shown in your diagram):

#define sensorTrigPin    4
#define sensorEchoPin    2

void setup()
{
    Serial.begin(9600);
    pinMode(sensorTrigPin, OUTPUT);
    pinMode(sensorEchoPin, INPUT);
}

void loop()
{
    int pulseWidth = 0;

    digitalWrite(sensorTrigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(sensorTrigPin, LOW);

    pulseWidth = pulseIn(sensorEchoPin, HIGH);

    Serial.print("Pulse Width: ");
    Serial.print(pulseWidth);
    delay(1000);
}

Note that pulseWidth is just the amount of time that it takes from the beginning of the Echo pulse going high to the end of the same pulse (when it goes low). You would still have to calculate the distance based on the value of pulseWidth.


UPDATE BASED ON RECENT EDIT TO THE QUESTION

If you change a portion of your loop() code to this, it should work:

void loop(){
    digitalWrite(4, HIGH);   //was (2, LOW)
    delayMicroseconds(10);   //was (5)
    digitalWrite(4, LOW);    //was (2, HIGH)
    //REMOVED EXTRA DELAY

    time = pulseIn(2, HIGH);  //was (4,HIGH);
    ...  //Keep the rest of your code the same.
} 
embedded_guy
  • 1,939
  • 3
  • 24
  • 39
  • Thanks, for your answer. I tried your code but seems to be missing the duration declaration. – ianaya89 Jun 29 '15 at 22:32
  • I can uploaded without errors but now nothing is printed in serial monitor :s – ianaya89 Jun 29 '15 at 22:37
  • @ianaya89 This is just a suggestion, but it helps if you do not change the code in your original question if it can be helped. You have made modifications to your code that have created a moving target when it comes to answering your question. – embedded_guy Jun 29 '15 at 23:52
  • Yes I know that, but I changed the code because I have copied it wrong. This is my original code and the issue is the same. Sorry for that! – ianaya89 Jun 29 '15 at 23:59
  • I tried and the behavior is the same: pulseWidth is always 0 – ianaya89 Jun 30 '15 at 00:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81917/discussion-between-embedded-guy-and-ianaya89). – embedded_guy Jun 30 '15 at 00:04
  • As you suggested was an issue in the wires, anyway your code was very helpful. Thanks for the help! – ianaya89 Jul 01 '15 at 20:01
  • @ianaya89, Great! Glad you were able to get it working. – embedded_guy Jul 01 '15 at 21:37
1

Try connecting your VCC of the sensor to 3V3 instead of 5V. This might sound odd, but I tried it and it worked well. Also, please make sure that your echo and trig pin match the code.

Rachen
  • 11
  • 1