LiDAR-Luna+Arduino mega+WS2812B

Mod edit:
This topic is a variation on WS2812B+HC-SR04+ Arduino Mega 2560, light up each LED to display distance
I'm happy that it's sufficiently different to be a separate topic, but they are clearly related.

Hi everyone.
I am very new to this but have managed to achieve all I can with the HC-SR04 on this project and it is not accurate enough.

I am trying to get the sensor to light up the LED as you move closer or further away, I got the HC-SR04 to light up 300 LEDs quite well except I had to use a box to keep the signal stable and it was not very reliable at around 3 meters.

So I've opted for a better/different sensor.
I took a lot of the code from a you tube video and it pretty much worked, if only it has been a better sensor in the first place.

Has anyone had any experience trying this before as I have not. Once it is complete I will share everything I can to help anyone else attempting a similar project but sadly that may take some time. I will upload some more details one I get started on the code.
Many thanks.

The HC-SR04 is accurate to around +/- 1 cm, although you can expect larger fluctuations on occasion.

If you are experiencing difficulties with it, especially with 300 LEDs, the very first thing to suspect is an inadequate, fluctuating power supply. You may need to power the LEDs completely separately from the Arduino.

Please post a circuit diagram and identify all the parts, pins and connections. Hand drawn is preferred.

1 Like

[quote="jetandy2012, post:1, topic:1173862"]
it is not accurate enough.

Lots of words but all engineering facts/data are missing. You missed to post a crystal ball. Next step:

Please read and use this topic, especially the parts telling about links to datasheets and how to post code: How to get the best out of this forum - Using Arduino / IDE 1.x - Arduino Forum

Thanks.

Sorry for that.
I know people need that stuff. I will be posting that soon. I just wondered if anyone has done anything similar. As it would be handy before I start.

Ok so
I have the Lidar-Luna sensor now.
I am using this code to display distance in cm in the serial monitor and it works fine.

* File Name: TFMP_example.ino
 * Developer: Bud Ryerson
 * Inception: 29JAN2019
 * Last work: 10SEP2021

 * Description: Arduino sketch to test the Benewake TFMini Plus
 * time-of-flight Lidar ranging sensor using the TFMPlus Library.

 * Default settings for the TFMini Plus are a 115200 serial baud rate
 * and a 100Hz measurement frame rate. The device will begin returning
 * measurement data right away:
 *   Distance in centimeters,
 *   Signal strength in arbitrary units,
 *   and an encoded number for Temperature in degrees centigrade.

 * Use the 'sendCommand()' to send commands and return a status code.
 * Commands are selected from the library's list of defined commands.
 * Parameters can be entered directly (115200, 250, etc) but for
 * safety, they should be chosen from the library's defined lists.
 */

#include <TFMPlus.h>  // Include TFMini Plus Library v1.5.0
TFMPlus tfmP;         // Create a TFMini Plus object

#include "printf.h"   // Modified to support Intel based Arduino
                      // devices such as the Galileo. Download from:
                      // https://github.com/spaniakos/AES/blob/master/printf.h

// The Software Serial library is an alternative for devices that
// have only one hardware serial port. Delete the comment slashes
// on lines 37 and 38 to invoke the library, and be sure to choose
// the correct RX and TX pins: pins 10 and 11 in this example. Then
// in the 'setup' section, change the name of the hardware 'Serial2'
// port to match the name of your software serial port, such as:
// 'mySerial.begin(115200); etc.

//#include <SoftwareSerial.h>       
//SoftwareSerial mySerial( 10, 11);   
                                    
void setup()
{
    Serial.begin( 115200);   // Intialize terminal serial port
    delay(20);               // Give port time to initalize
    printf_begin();          // Initialize printf.
   

    Serial2.begin( 115200);  // Initialize TFMPLus device serial port.
    delay(20);               // Give port time to initalize
    tfmP.begin( &Serial2);   // Initialize device library object and...
 

}

// Initialize variables
int16_t tfDist = 0;    // Distance to object in centimeters

// Use the 'getData' function to pass back device data.
void loop()
{
    delay(100);   // Loop delay to match the 20Hz data frame rate

    if( tfmP.getData( tfDist)) // Get data from the device.
    {
      printf( "Dist:%04icm ", tfDist);   // display distance,
      printf( "\r\n");                   // end-of-line.
    }

}

next I tried to introduce the Adafruit_neopixel library. and adapt the code previously used for the HC-SR04.
so it looks like this

#include <SoftwareSerial.h>

#include <Adafruit_NeoPixel.h>

#include "printf.h"
#include <TFMPlus.h>  // Include TFMini Plus Library v1.5.0
 
 #define PIN        6 // Which pin on the Arduino is connected to the NeoPixels?

#define NUMPIXELS 10 // How many NeoPixels are attached to the Arduino?

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

TFMPlus tfmP;         // Create a TFMini Plus object

int16_t tfDist = 0;    // Distance to object in centimeters

void setup()
{
 
 pixels.begin();
 
}

void loop()
{
  
 pixels.clear();                               // Set all pixel colors to 'off'
  tfDist=tfmP.getData( tfDist)*0.8;
 
 
  for(int16_t i=0; i<tfDist; i++)                      // For each pixel in strip...
  { 
    pixels.setPixelColor(i, 0,0,255); 
  }
  
  pixels.show();                                  // Send the updated pixel colors to the hardware.

} 

Ive wired it like this.

The button is 5v power supply.
The Ws2812B is the LED
The sensor is not the Lidar-lunar pins are like this
Pin 1 (red) -----> 5V----- to arduino 5v
Pin 2 (black) ---> D2 (SDA)---- to arduino pin18TX1
Pin 3 (yellow) --> D1 (SCL)---- to arduino 17RX2
Pin 4 (white) ---> GND---- to arduino GND
Pin 5 (green) ---> D4
Pin 6 (blue) ----> unused

So I can get the lights working separately and I can get the lidar-luna to send just distance in cm, but Im not sure why it does not translate.

I used this code for theHC-SR04

#include <Adafruit_NeoPixel.h>
#include <HCSR04.h>

#define PIN        6 // Which pin on the Arduino is connected to the NeoPixels?

#define NUMPIXELS 29 // How many NeoPixels are attached to the Arduino?

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

UltraSonicDistanceSensor distanceSensor(9, 10);  // Initialize sensor that uses digital pins 9 and 10.


int current_Last_LED=0;

void setup() 
{  
  pixels.begin(); 
}

void loop() 
{
  
  pixels.clear();                               // Set all pixel colors to 'off'
  current_Last_LED=distanceSensor.measureDistanceCm()*0.8;
 
 
  for(int i=0; i<current_Last_LED; i++)                      // For each pixel in strip...
  { 
    pixels.setPixelColor(i, 0,0,255); 
  }
  
  pixels.show();                                  // Send the updated pixel colors to the hardware.
  
}

In line 10,

UltraSonicDistanceSensor distanceSensor(9, 10);  // Initialize sensor that uses digital pins 9 and 10.

this line is to initialize the sensor and I done know when or how to do that with the lidar.
any ideas

I am trying to achieve this.

But with a lidar-luna sensor instead of the HC-SR04.

OK so with some help I have this working.
CODE looks like this.

#include <Wire.h>
#include <Adafruit_NeoPixel.h>

// #include <HCSR04.h> replaced by...
#include <TFLI2C.h>

#define PIN        6 // Which pin on the Arduino is connected to the NeoPixels?
#define NUMPIXELS 20 // How many NeoPixels are attached to the Arduino?

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// UltraSonicDistanceSensor distanceSensor(9, 10);  // Initialize sensor that uses digital pins 9 and 10.
// replaced by...
TFLI2C tflI2C;

int current_Last_LED = 0 ; 
int16_t  tfAddr = TFL_DEF_ADR;    // default I2C address
int16_t  tfDist;   // distance in centimeters

void setup() 
{  
  Serial.begin( 115200);  // Initalize serial port
  Wire.begin();           // Initalize Wire library
  pixels.begin(); 
}

void loop() 
{
  pixels.clear(); 
  if( tflI2C.getData( tfDist, tfAddr))                              // Set all pixel colors to 'off'
  current_Last_LED=(tfDist)*0.8;
  //replaced by...
  //tfDist=tflI2C.getData( tfDist, tfAddr);
  
  if( current_Last_LED <=NUMPIXELS)   // If reading is out of range, then...
  {  
	  for(int i=0; i<current_Last_LED; i++) // For each pixel in strip...
	  { 
		pixels.setPixelColor(i, 25,25,00); 
	  }  
	  pixels.show();	  // Send the updated pixel colors to the hardware.
  }
  else pixels.clear();    // Otherwise clear the display
}

Only thing I am having issue with now is that sometimes when I move out of range the LEDS stay on, so it's working but its a bit unreliable.
Any ideas.
I will post a video soon.

What is the sensor readinbg when you do this? You could add some serial printing in there and I am sure is doing what the code directs...

a7

so again not sure, sorry.
but as I see it the range is based on the NUMPIXEL value so 20 leds.

it was not working until line 37 was added:

 if( current_Last_LED <=NUMPIXELS)   // If reading is out of range, then...

before this the LEDs would display the distance as expected but when there was no obstruction they where blinking all over the place. I suspected that was because the sensor has a range of 8meters and my room is only 4meters and I have only 20 LEDs displaying.
So I wanted to create a distance limit so that the sensor would not trigger the lights unless there was an obstruction within range.
This is working well now but like I said sometimes the LEDs just stay on.

I assume the sensor is still reading the full range distance but the code is telling it to stop the display when there is nothing within the NUMPIXEL range.

Also
not sure how or where to add the
Serial.print();
to see the results on the serial monitor
every time I try to put it in I get:

error: no matching function for call to 'HardwareSerial::print()'

Flag you own post, mention the other thread and ask the moderators to combine them. Or kill one of them.

As for printing, you have to tell it what to print.

In setup()

  Serial.begin(115200);

then like

  if( tflI2C.getData( tfDist, tfAddr))                              // Set all pixel colors to 'off' <-- ????
    current_Last_LED=(tfDist)*0.8;

  Serial.println(tfDist);

Make sure the band rate in the serial monitor window is set to 115200.

You can get fancier with the printing. Maybe print currentLastLED also.

HTH

a7

Got the serial print to work.
It just sits on the relevant number.
so when I move out of range at NUMPIXEL 13 it just stays there.
but if I change the sketch to just read in distance its fine.

its hardly working at all now with the serial monitor.
It just sticks on the last distance it read before the obstacle was removed.
Could it be a connection issue.
it does seem strange because if I reset the code to just read distance it works fine.

this is the code for just distance:


#include <Arduino.h>     // Every sketch needs this
#include <Wire.h>        // Instantiate the Wire library
#include <TFLI2C.h>      // TFLuna-I2C Library v.0.1.1

TFLI2C tflI2C;

int16_t  tfDist;    // distance in centimeters
int16_t  tfAddr = TFL_DEF_ADR;  // Use this default I2C address or
                                // set variable to your own value

void setup()
{
    Serial.begin( 115200);  // Initalize serial port
    Wire.begin();           // Initalize Wire library
}

void loop()
{
    if( tflI2C.getData( tfDist, tfAddr)) // If read okay...
    {
        Serial.println(tfDist);          // print the data...
    }
    else tflI2C.printStatus();           // else, print error.

    delay( 50);
}

the reading go up and down as you would expect them to.

printing in current_last_led
makes the Tx LED on the arduino turn on and of
On when there is obsticale
Off when out of NUMPIXEL range.
so thats working fine.
the power supply is from my USB port at the moment, could that have something to do with it?

No Rx led to speak of in any case.

and it has literally not worked once since we started talking.
the sensor just wont go to 0 (zero) any more.
well i say that it just did for the first time in ages.

when it went off the Rx LED did not come on so i know that is not a thing now.