1

I have an issue using an ultrasonic sensor. The problem is that when I connect the ultrasonic sensor to my Arduino Nano, it always gives some random values, usually 130 cm, I don't know why.

I bought a second ultrasonic sensor and the problem reoccured. One I bought last year, and the other one I bought yesterday.

I tried several programs like the traditional one:

const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.println(distance); } 

This didn't work, so I searched for useful libraries such as NewPing.h:

#include <NewPing.h> const int trigPin = 9 ; const int echoPin = 10; const int maxDistance = 2000; unsigned int distance; NewPing sonar(trigPin, echoPin, maxDistance); void setup() { Serial.begin(9600); } void loop() { delay(1000) ; distance = sonar.ping_cm(); Serial.print("Distance : "); Serial.print(distance); Serial.println(" cm !"); } 

This also didn't work. This is a simple screenshot of what happens in the monitor: (the 0 cm ! value is caused because I turned off the ultrasonic, and the object was actually 4 cm away):

The serial monitor output, the zeros are because I disconnected the sensor

Any help would be appreciated.

5
  • What are you using as object to test the sensors? Ultrasonic distance measurement is not really accurate and has the big flaw, that it depends on the reflectivity of the object. Soft objects can dampen the reflection significantly until you don't get a reliable measurement anymore and hard flat objects, that are placed at an angle, can reflect the sound wave in a direction away from the sensor, again impacting the measurement. Commented Jan 18, 2024 at 9:47
  • my hand is the object I was using, also a black plastic, and sometimes, thr wall Commented Jan 18, 2024 at 9:48
  • 1
    Please try with something like a book at right angle to the sensors line of view. That should at least rule out that problem for now. I'm not sure, if that solves your problem. I don't see any error in your code. Please also check your wiring (you didn't show it here). It should be rather simple, if you have one of the standard ultrasonic sensor modules for Arduino. Vcc and ground connections between sensor and Arduino and then trigger and echo to the corresponding digital pins (9 and 10). Commented Jan 18, 2024 at 9:52
  • I tried, everything is well connected, nothing is happening, same error Commented Jan 18, 2024 at 10:19
  • 1. Perhaps use ‘float’ data type for the ‘duration’ and ‘distance’ variables. 2. Short distances are problematic, so I would not worry about results under a couple of cm. Maybe filter these out using code. 3. Who knows what the angle of view is for these iinexpensive sensors? 4. A closer approximation to regular and reflective surface and a free-in-air geometry may make for less concerning results. It would be a good exercise to start with idealized setup and then depart from regularity one aspect at a time. Commented Apr 23 at 11:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.