Go Down

Topic: Trouble with HC-SR04 Ultrasonic Sensor (Read 4240 times) previous topic - next topic

swish123

Hi I recently bought Arduino Mega 1280 and a HC-SR04 Ultrasonic Sensor(Hong Kong) for a project.

I uploaded this code onto the chip:

Code: [Select]

#include "Ultrasonic.h"
Ultrasonic ultrasonic(12,13);
void setup()
{
pinMode(11, OUTPUT);  /* Additional pin setting */
digitalWrite(11, HIGH); /* Additional pin setting */
Serial.begin( 9600 );
}
void loop()
{
Serial.print( ultrasonic.Ranging(CM) );
Serial.println( "cm" );
delay(1000);
}



But for the output from the Serial Monitor, I just keep getting "0 CM...0 CM..0 CM...".

Where am I going wrong?

Thanks


Si

Some suggestions:

* Check your hardware connections.

* Check the supply voltage is reaching the ranging module - with a multimeter.

* Disconnect everything (assuming breadboard) and build it all again.

* Try it on different pins (hardware and software)
--
My New Arduino Book: http://www.arduinobook.com

terryking228

Hi,

Try the information here and the test software sketch:

http://arduino-info.wikispaces.com/UltraSonicDistance

This works fine with HC-SR04 for me...

3 Choices of modules here:  http://goo.gl/dziqY

Disclaimer: Mentioned stuff from my own shop...

Regards, Terry King
...In The Woods In Vermont
terry@yourduino.com

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

santiago.lopez

Hi i'm working with this sensor, i'm making  a car. When i connect the sensor and the motor, the distance from the sensor is not correct, i think the wheels make a lot of noise, maybe it's a problem? Because, if i disconnect the wheels, the distance is correct.

Si

Put a 100nF non-polarised capacitor across each motor. This will suppress the interference.
--
My New Arduino Book: http://www.arduinobook.com

Anachrocomputer

That does sound like a noise problem. It may be electrical noise conducted through the power supply, or it may be acoustic noise (sound) generated by the motor(s). This may be a difficult one to fix!  First of all, do you have any power supply decoupling capacitors? And are you using the same power supply (batteries, presumably) for both motors and the sensor/Arduino?

santiago.lopez


That does sound like a noise problem. It may be electrical noise conducted through the power supply, or it may be acoustic noise (sound) generated by the motor(s). This may be a difficult one to fix!  First of all, do you have any power supply decoupling capacitors? And are you using the same power supply (batteries, presumably) for both motors and the sensor/Arduino?



I supplying all circuit with the Arduino pin 5V, and i'm controling the motor's with a L298N Bridge. All ready, the motor's have a 100nf capacitor

Anachrocomputer

If you're supplying the motors with power from the Arduino's 5V output, then that may be part of the problem. Can you try using a separate power supply for the motors, with just the grounds connected together. That is, power the Arduino and sensor as you have it now, but disconnect the power to the motors. Then, use a second battery to supply the motor power. Remember to leave the grounds connected.

santiago.lopez


If you're supplying the motors with power from the Arduino's 5V output, then that may be part of the problem. Can you try using a separate power supply for the motors, with just the grounds connected together. That is, power the Arduino and sensor as you have it now, but disconnect the power to the motors. Then, use a second battery to supply the motor power. Remember to leave the grounds connected.

ok! it's for this! :( But, how can i supply the motor and arduino? at same time? with only one batery?

Anachrocomputer

You need two batteries. One battery for Arduino and sensor, the other battery for the motors.

santiago.lopez

ok!! i'm going to use a 9V batery and a LM78L05 to supply the motors! Thank's you!

Anachrocomputer

You may find that a 78L05 is too small to power the motors. It can only supply 100mA. The 9V battery will soon be used up, too. A 7805 regulator will be good for 1A. Do you know how much current your motors will draw?

santiago.lopez

i just trying with a L7805, and the motors and the sensor it's ok, I dont know the caracteristics of the motor, it's from a toy :(

Si

If the motor is ok with 5V it is probably also fine with 6V, so you could make up a battery pack with 4 rechargeable AA cells. They will last a lot longer than a small 9V battery and you will not need the voltage regulator.
--
My New Arduino Book: http://www.arduinobook.com

Go Up