r/ArduinoProjects • u/Article_Prior • Mar 11 '24
My ultrasonic sensor not working
Hello guys, i am new here and still learning. I tried to connect my ultrasonic sensor but it never works. I tried diff codes but it never works. Sometimes it prints totally random number (like my hand is max5cm from sensor and i shake it a little and it bumps from 0 to 2000m but never shows correct data). Can i be doin something wrong or is just sensor broken ?
2
u/henriquemattosesilva Mar 11 '24
Try to change the definitions about the pinMode of ultrasonic for void setup.
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);
1
1
u/Physical-Acadia-283 Mar 12 '24
Those “long” functions are not executing cuz once the program reaches void loop it stays there cuz its a loop, you should move them so they’re before void setup. Also, how exactly are you converting time( duration) to distance?
1
u/otieno88 Mar 13 '24
I’d move lines 9, 10, and 16 in set up. Between line 17&18, I’d insert a code stating
If (duration == null) { Serial.print(“null value detected”); }
Alternatively, you can print the value of duration before passing it to the next two methods. You can do that by writing:
Serial.print(“Duration is: “ + duration);
2
u/Spectraman Mar 11 '24
I would start by moving the pinMode() functions to the inside of your setup() function, because now you are setting it every loop.