My primary issue involves a set of LED strip lights that remain dimly lit instead of completely turning off as intended, in a setup controlled by an Arduino using an ultrasonic distance sensor.
Despite implementing code designed to dynamically adjust the LEDs' state based on the measured distance—intending for the LEDs to turn off as the distance increases—the LEDs exhibit dim lighting instead of fully powering down.
This behavior persists even after integrating hardware solutions like a 1000µF capacitor across the power lines for voltage smoothing and considering the addition of a 100-500 ohm resistor on the data line to mitigate signal noise and reflections. The code is really simple but not sure anymore if it is a code issue. Maybe a library issue ?
Intended Behavior Recap:
- Initial State (No Object Detected or Object Beyond Max Distance): All LEDs off.
- Object Approaches Sensor: The number of lit LEDs increases, directly proportional to the object's proximity to the sensor.
I need some experts to HELP me .... The use-case is for a Car Parking Aid.
Here is the code in case I am missing something.
#include <Adafruit_NeoPixel.h>
#include <HCSR04.h>
#define PIN 6 // Pin connected to NeoPixels
#define NUMPIXELS 24 // Number of NeoPixels
#define MAX_DISTANCE_CM 30 // Maximum distance of interest, in centimeters
#define ULTRASONIC_TRIG_PIN 9 // Trigger pin for the ultrasonic sensor
#define ULTRASONIC_ECHO_PIN 10 // Echo pin for the ultrasonic sensor
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
UltraSonicDistanceSensor distanceSensor(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN);
void setup() {
pixels.begin(); // Initialize the NeoPixel strip
Serial.begin(9600); // Start serial communication at 9600 baud rate
}
void loop() {
float distance = distanceSensor.measureDistanceCm(); // Measure the distance to the nearest object
// Adjust the distance to be within the maximum distance of interest
distance = min(distance, MAX_DISTANCE_CM);
// Calculate the number of LEDs to turn ON based on the proximity (closer = more LEDs on)
int ledsOn = static_cast<int>((1 - (distance / MAX_DISTANCE_CM)) * NUMPIXELS);
ledsOn = max(0, ledsOn); // Ensure ledsOn is never less than 0
// Initialize all LEDs to off
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Turn off the LED
}
// Turn on LEDs based on calculated value
for (int i = 0; i < ledsOn; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 255)); // Set pixel color to blue
}
pixels.show(); // Apply the updated colors to the NeoPixels
}



