LED Strip Remains Dim Instead of Turning Off with Ultrasonic Sensor Control

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
}
 

Do they completely turn off if you remove the below?

How are you powering your strip?
Which Arduino?

I would add some serial print statements to show the distance and the ledsOn variables and see if there is anything unexpected in there. Temporarily add a delay(2000) at the end of loop() so you don't get spammed with messages,

Very clever troubleshooting technics.... I appreciate the thought process @sterretje

  • Verifying that the hardware setup (LED strip and Arduino) is capable of achieving the desired behavior (LEDs fully turning off).
    I did remove the
    image

and all LED were turned OFF when deleting that part of the code.

  • Checking that the power supply is adequate for your setup.
    I am using a Arduino Nano, and the power supply is a 12V, 2A that comes to my Breadboard Power Supply Board Module 3.3V/5V Dual Voltage. Voltage set up for 5V verified.

  • Using debug prints to gain insights into the program's behavior during execution.
  • Making the debug output manageable to read and interpret.

I made the change in the code as suggested.

 pixels.show(); // Apply the updated colors to the NeoPixels

  // Print the distance and the number of LEDs on to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm, LEDs On: ");
  Serial.println(ledsOn);

  delay(1000); // Add a delay to prevent spamming the Serial Monitor with messages
}

I must not understand the impact of these lines in terms of the LED behavior, but by adding this lines, the LED lights are off when expected and there is no more the semi-lit/ behavior when they were supposed to be off.

What did if fix the behavior ? The response on the LED's when moving the object closer to the sensor is slower than expected but for my use case that might do it.

Can you educate me on what you have done here ?

THANK YOU so much for trouble shooting from far.

Here is the final code as I left it:
If there are other suggestions to make the code more robust please let me know:

#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

  // Print the distance and the number of LEDs on to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm, LEDs On: ");
  Serial.println(ledsOn);

  delay(1000); // Add a delay to prevent spamming the Serial Monitor with messages
}

How are you grounding your (1) power supply, (2) Arduino, (3) LED strip? They should share a common ground.

My observation is; your sketch turns on all LEDs at 400cm and 399 cm, then LEDs are all "off" from 398 cm to 30 cm, then at 29cm LEDs begin to light an increasing number for each cm toward 2 cm (22 out of 24 LEDs lit) - the HCSR04 does not register under 2cm.

@xfpd My apologies on the first code, please refer to the last code that I shared.

Based on that last code this what is suppose to happen

  • Beyond 30 cm or No Detection: All LEDs off.
  • 0 to 30 cm Proximity: Number of lit LEDs increases as the object gets closer, with all LEDs potentially on, when the object is just above the sensor's minimum detectable distance (~2 cm).
  • Dynamic Visualization: LEDs provide a real-time visual representation of the object's distance, with more LEDs lighting up as the distance decreases.
  • This setup effectively uses the LED strip as a proximity meter, visualizing the distance to the nearest object within a 30 cm range.

Now, I used @sterretje suggestion to troubleshoot by adding the following lines:


  pixels.show(); // Apply the updated colors to the NeoPixels

  // Print the distance and the number of LEDs on to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm, LEDs On: ");
  Serial.println(ledsOn);

  delay(1000); // Add a delay to prevent spamming the Serial Monitor with messages
}

**What I do not understand now, why the LED behavior of LEDs remaining dimly is now fixed ????**
The lines that I added should not changed any behavior, or am I wrong ?
Also, per your comment on the **ground** I am using the breadboard ground in each side of the rail..... but you might be right, there could be a ground variation so I place all the grounds in the same side of the breadboard, just to be safe.

Thank you for providing more insights. 


My first observation of the sketch from Post #5 are:

  1. "400 cm" input registers as "(minus)1 cm" output
  2. "399 cm" to "30 cm" input register as "30cm" output
  3. Same LED pattern as post #4 above from 29cm to 2cm.

I would take the LEDs out of your sketch and make the HC-SR04 output register close to the input. After that works, then try again with the LEDs.

Wow your input made me realize that there is something going in the set up I have. The sensor is reading, but the maximum distance is to be about 173 cm, it does not read beyond that which is far bellow from the spec 400 cm.

mmmm ... I need to keep diving in... more to come.

/*
  trouble_shooting-01

  This sketch is designed to test the HC-SR04 Ultrasonic Distance Sensor's output
  independently of any other components like LEDs. It aims to ensure that the sensor
  accurately measures and reports distances.

  This step is crucial for verifying that the foundational sensor data is precise before
  integrating more complex logic or visual feedback mechanisms, such as an LED display.

  Components required:
  - Arduino board
  - HC-SR04 Ultrasonic Distance Sensor
  - Wires for connection

  The sketch simply reads the distance measured by the HC-SR04 sensor and prints
  this value to the Serial Monitor. This allows for real-time observation and verification
  of the sensor's accuracy in measuring distances.
*/

#include <HCSR04.h>

#define ULTRASONIC_TRIG_PIN 9 // Trigger pin for the ultrasonic sensor
#define ULTRASONIC_ECHO_PIN 10 // Echo pin for the ultrasonic sensor

// Initialize the ultrasonic distance sensor
UltraSonicDistanceSensor distanceSensor(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN);

void setup() {
  Serial.begin(9600); // Start serial communication at 9600 baud rate
}

void loop() {
  float distance = distanceSensor.measureDistanceCm(); // Measure the distance to the nearest object
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000); // Add a delay to prevent spamming the Serial Monitor with messages
}

  • If you are trying to power 24 pixels with the breadboard power supply, the supply is insufficient.

  • The signal wire to the LED strip should include a series 470R resistor.

Thank you @LarryD, when I get home tomorrow I will change the power source just to verify.

Thank you again

Hello @LarryD finally I am back home from my business trip.
I was trying to understand your comment.

Let me know if my logic is not correct but here is what I thought:

  • For 24 WS2812B LED pixels at maximum brightness:
  • Power (Watts) = 24 LEDs × 0.06 Amps/LED × 5 Volts
  • Power (Watts) = 1.44 Amps × 5 Volts
  • Power (Watts) = 7.2 Watts

The power supply that goes to the Breadboard is 12V and 2A
and this goes to the Breadboard power simply that takes that to a 5v that is what is needed to power each component.
So the breadboard power supply will be at 5V up to 2 amps so around ~10W available. So should I have enough Power for the LED's ????

Let me know if I am missing anything. Thank you

  • These are simple linear voltage regulators, i.e. 3v3 and 5v.

  • If you had infinite heat sinking on the regulator cases, you could handle large current loads.

  • However, there isn’t a heat sink on the cases, yet alone infinite heat sinking.

  • At 12v in, you would be lucky to even drive 200mA without the regulator shutting down due to high temperature.

  • This type of breadboard power supply should be avoided if the circuit draws more than 150-200mA.

EDIT

BTW, power supplies like these are available on the internet.

5v @ 4A

Suggest you read through this discussion:

No paste needed on these...
https://www.amazon.com/Jienk-Aluminum-Conductive-Adhesive-Regulators/dp/B09M5XJC1S/

  • Assume we need 1A current.

Power 5v regulator uses is (12v - 5v) * 1A = 7 watts

The heat sinks shown are just a bit small. :wink:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.