I'm using a HC-SR04 ultrasonic distance sensor for a school project. I'm using an ATMEGA328 with Arduino bootloader, on a breadboard, to process the data from the sensor. It works perfectly, until I try to measure distances at or beyond one meter. The data sheet says the sensor works up to 4 meters, and I have 3 of them that all aren't working properly. This is the code that triggers the sensor and works out the distance:
long duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
delay(10);
cm = (duration/2) / 29.1;
Any help would be appreciated.
long duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
delay(10);
cm = (duration/2) / 29.1;
Any help would be appreciated.