help with distance gauging proximity meter thingamabob

so I'm making this thing that displays the distance of an object away from an HCSRO4 using 9 leds working as essentially a proximity counter. the closer the object is, the more LEDs light up (1 LED lights up at distance of 9cm, 2 at a distance of 8cm, 3 at 7cm..), but I'm having some problems. basically, either the LEDs flicker on and off randomly at random brightnesses, or just don't turn on at all, and i can't seem to figure out what's wrong. here's the code: #define TRIG_PIN 13 #define ECHO_PIN 12 const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]); void setup() { pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); for (int i = 0; i < numLeds; i++) { pinMode(ledPins[i], OUTPUT); } } float getDistance() { digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); long duration = pulseIn(ECHO_PIN, HIGH, 20000); if (duration == 0) return -1; return duration * 0.034 / 2; } void loop() { float distance = getDistance(); int ledsToLight = 0; if (distance >= 1 && distance <= 9) { ledsToLight = 10 - floor(distance); } for (int i = 0; i < numLeds; i++) { digitalWrite(ledPins[i], i < ledsToLight ? HIGH : LOW); } delay(100); }
No description
No description
13 Replies
PenPengu
PenPengu2mo ago
Ultrasonic distance LED bar - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
PenPengu
PenPengu2mo ago
code looks ok to me. And seems to work in wokwi. One LED is flickering because the distance is "flickering", the sensor doesn't have 100% repeatability. Without changing the distance, for example it keeps randomly switching between 2.91 and 3.01
distance: 3.01
ledsToLight:7

distance: 2.91
ledsToLight:8

distance: 3.01
ledsToLight:7

distance: 3.01
ledsToLight:7
distance: 3.01
ledsToLight:7

distance: 2.91
ledsToLight:8

distance: 3.01
ledsToLight:7

distance: 3.01
ledsToLight:7
How does it behave in real life? Did you try Serial.printing the distance values? Basically always when using this kind of sensor, you probably want some kind of input smoothing. I'm sure there are various libraries for it and some science behind what's the best way. For example you could use a moving average. @warriorcreeper Can the sensor even read 1cm reliably? It's been a while since I used it, but I think it does have a minimum distance
warriorcreeper
warriorcreeperOP2mo ago
it does? well, even if it does, its not reading any other distance reliably either way
PenPengu
PenPengu2mo ago
what object are you using to read the distance of?
warriorcreeper
warriorcreeperOP2mo ago
I'll try serial printing the distance l8r, can't use the arduino rn
PenPengu
PenPengu2mo ago
I recommend trying an A4 college block or similar
warriorcreeper
warriorcreeperOP2mo ago
i tried using my hand mostly i tried using my hand mostly
PenPengu
PenPengu2mo ago
The sensor sends out an ultrasonic sound pulse through one of its silver cans. The sound reflects on the object and then gets read by the second can. That basically explains a lot of things. If the object isn't large enough or is angled or such that the sound doesn't get reflected well then the sensor can't sense it
warriorcreeper
warriorcreeperOP2mo ago
as for the behaviour, the LEDs flicker on and off randomly if they're supposed to be on, or just don't turn on at alp
PenPengu
PenPengu2mo ago
Definitely serial.print the distance before debugging more. Garbage input -> Garbage output. So we first need to check if the sensor values are good. Then you can decide if you need filtering or such
warriorcreeper
warriorcreeperOP2mo ago
i know that much, but I tried using various objects at varying distances (facing the sensor straight), but the LEDs behaved weirdly every time yeah I'll try that rq
PenPengu
PenPengu2mo ago
the breadboard is a bit of a mess. So I can't see it well and maybe you made mistakes. But where is the connection between the LED's cathodes (grounds) which is the breadboard ground-rail and the arduino's ground pad? There is none. So I wonder why any led lights up at all
warriorcreeper
warriorcreeperOP2mo ago
wait yeah there's no connection to GND for the led how the fuck was it turning on then? what the hell

Did you find this page helpful?