Hi everybody,
I'm working on my first robot project: a simple 2WD wall avoider using an Arduino Uno and the Adafruit motorshield v1 and I have a problem with the HC-SR04 sensor I purchased. Everywhere on the web I find that the maximum range is a couple of meters, however I can only get 6-8 cm's from it.
My code looks like this:
/*
*UltrasonicDemo.pde - Ultrasonic sensor Ranging measure Demo
*@Author:dragon
*@DATA:2013-8-7
*Company website:www.elecfreaks.com
*/
#include <Ultrasonic.h>
//download via http://www.elecfreaks.com/store/download/product/Sensor/HC-SR04/HCSR04Ultrasonic_demo.zip
int trigpin = A1; //appoint trigger pin
int echopin = A2; //appoint echo pin
// I used the analog pins since these are the only available ones when using the motorshield
Ultrasonic ultrasonic(trigpin,echopin);
void setup()
{
Serial.begin(9600);//set Serial Baud rate
Serial.println("Ultrasonic sensor starting!!!!!");
}
void loop()
{
float cmdistance,indistance;
long microsec = ultrasonic.timing();
Serial.print("microsec: ");
Serial.print(microsec);
cmdistance = ultrasonic.CalcDistance(microsec,Ultrasonic::CM);//this result unit is centimeter
indistance = ultrasonic.CalcDistance(microsec,Ultrasonic::IN);//this result unit is inches
Serial.print(" cmdistance: ");
Serial.print(cmdistance);
Serial.print(" indistance: ");
Serial.println(indistance);
delay(1000);
}
This is the serial output I get when I move a book from 20 cm slowly up until the sensor itself:
Ultrasonic sensor starting!!!!!
microsec: 413 cmdistance: 7.48 indistance: 2.94
microsec: 413 cmdistance: 7.48 indistance: 2.94
microsec: 419 cmdistance: 7.58 indistance: 2.99
microsec: 419 cmdistance: 7.58 indistance: 2.99
microsec: 419 cmdistance: 7.58 indistance: 2.99
microsec: 419 cmdistance: 7.58 indistance: 2.99
microsec: 413 cmdistance: 7.48 indistance: 2.94
microsec: 413 cmdistance: 7.48 indistance: 2.94
microsec: 419 cmdistance: 7.58 indistance: 2.99
microsec: 482 cmdistance: 8.72 indistance: 3.43
microsec: 431 cmdistance: 7.80 indistance: 3.07
microsec: 266 cmdistance: 4.81 indistance: 1.90
microsec: 197 cmdistance: 3.57 indistance: 1.40
microsec: 95 cmdistance: 1.72 indistance: 0.68
microsec: 117 cmdistance: 2.12 indistance: 0.83
microsec: 137 cmdistance: 2.48 indistance: 0.98
microsec: 132 cmdistance: 2.39 indistance: 0.94
microsec: 138 cmdistance: 2.50 indistance: 0.98
microsec: 137 cmdistance: 2.48 indistance: 0.98
microsec: 137 cmdistance: 2.48 indistance: 0.98
I also checked the voltage on the HC-SR04 GND and VCC pins, it's 4,64 V. At the moment the Arduino is still powered via USB. My testsetup looks like this:
http://imagizer.imageshack.us/v2/800x600q90/819/oboi.jpg
Can somebody help me?