Pages: [1]   Go Down
Author Topic: Trouble with HC-SR04 Ultrasonic Sensor  (Read 3167 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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

Logged

UK
Offline Offline
God Member
*****
Karma: 10
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

--
My New Arduino Book: http://www.arduinobook.com

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 24
Posts: 1762
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
God Member
*****
Karma: 10
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Put a 100nF non-polarised capacitor across each motor. This will suppress the interference.
Logged

--
My New Arduino Book: http://www.arduinobook.com

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley-sad But, how can i supply the motor and arduino? at same time? with only one batery?
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad
Logged

UK
Offline Offline
God Member
*****
Karma: 10
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

--
My New Arduino Book: http://www.arduinobook.com

Pages: [1]   Go Up
Jump to: