All I2C functions hanging, need lidar input

Hi! My friend and I are trying to use a TF-Luna LiDAR sensor with our Arduino UNO board.

We are using a popular code segment that seems to pop up everywhere for TF-Luna:

/* File Name: TFLI2C_simple.ino
 * Developer: Bud Ryerson
 * Date:      4 NOV 2021
 * Version:   0.1.2
 * Described: Simplified Arduino example sketch for the Benewake
 *            TF-Luna LiDAR sensor configured for the I2C interface
 */
 
#include <Arduino.h>     // every sketch needs this
#include <Wire.h>        // instantiate the Wire library
#include <TFLI2C.h>      // TFLuna-I2C Library v.0.2.0

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( 9600);  // initialize serial port
    Wire.begin();           // initialize Wire library
    Serial.println( "TFLI2C example code simplified"); // say "Hello!"
    Serial.println( "4 NOV 2021");                     // and add date
}

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

    delay( 50);
}

Unfortunately, this code seems to hang (not timing out, giving an error, or giving an output infinitely) at if( tflI2C.getData( tfDist, tfAddr)) // If read okay...

We have pin 1 of the Luna plugged into 5v, pin 2 into SDA, pin 3 into SCL, pin 4 into GND, pin 5 into GND, and pin 6 unplugged.

We also tried to use an I2C Scanner to find the issue but that hands as well, here is the code:

  // --------------------------------------
//http://playground.arduino.cc/Main/I2cScanner
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
// Watch Video explainign I2C address: https://www.youtube.com/watch?v=bqMMIbmYJS0
//
 
#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("
I2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found
");
  else
    Serial.println("done
");
 
  delay(5000);           // wait 5 seconds for next scan
}

The point at which this hangs is error = Wire.endTransmission();

We are hoping to get an input from this lidar for a hackathon tomorrow (today because it's 1am) and can't figure out what we are doing wrong. Can anyone help?

Please post a link to the specs of your sensor and a schematic diagram showing how you have wired it and how everything is powered.

1 Like

Here is a link with all of the info on my sensor:

If you let me know any specific specs you need I can go through and find them for you, but it may be faster for you to ctrl+f in the manual yourself. Let me know!

I am using this schematic:
image
Where pin 4 and pin 5 are both connected to ground and pin 6 is unconnected.
I am powering it by connecting it to my laptop with a USB cable.

Something is amiss. Post a link to technical information on the sensor. I do not want to spend the time going through everything in the link you posted. Your "schematic" shows a 1 wire sensor you say is a I2C sensor, they are definitely not the same. I2C needs SCL, SDA, + and - ie. 4 wires.

1 Like

The sketch will halt if:

  • SDA is short circuit to GND.
  • SCL is short circuit to GND.
  • SDA is short circuit to SCL.
  • The sensor is not powered.

Do you have a multimeter ?
Why is the sensor not powered with the 5V or the 3.3V pin ?

1 Like

I'm not fully sure what I can post here that you need, but I will try to post everything relevant:


image

Are you copying this website ? https://discuss.ardupilot.org/t/tfluna-i2c-arducopter-installation-guide/71366

1 Like

I do have a multimeter, I will be able to use it in a couple of hours. The sensor is powered to 5v as shown in the schematic. Also, when I look into the sensor I can see the red light that it emits, indicating that it is powered. If I am misunderstanding what you mean, please let me know.

  • SDA is short circuit to GND.
  • SCL is short circuit to GND.
  • SDA is short circuit to SCL.

How would these happen and how can I circumvent them? None of these are connected to ground, pin 4 and 5 are GND and SDA / SCL are in their correct ports. I just now attempted to switch the SDA and SCL pins but nothing changed.

Someone will correct me if I'm wrong but as far as I know there are no built in pull up resistors on a Uno for the I2C port. As your LiDAR sensor has a port that can be serial or I2C my guess would be that it does not have pull up resistors either, you should check this in the documentation. I2C does not work without pull up resistors. You need to add 2 of them, 1 each between 5V and each of SDA and SCL. Anything between maybe 2k2 and 10k should be OK, but I suggest 4k7 if you have them.

If that doesn't work please post some clear photos of your wiring so we can see exactly how you have everything connected.

2 Likes

I have tried this website, running this code:

#include <Wire.h>

void setup()
{
Wire.begin();

Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}

void loop()
{
byte error, address;
int nDevices;

Serial.println("Scanning…");

nDevices = 0;
Serial.println("Test 3");
for(address = 1; address < 127; address++ )
{
Serial.println("test 4");
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
Serial.println("test 1");
error = Wire.endTransmission();
Serial.println("test 2");
if (error == 0)
{
  Serial.print("I2C device found at address 0x");
  if (address<16)
    Serial.print("0");
  Serial.print(address,HEX);
  Serial.println("  !");

  nDevices++;
}
else if (error==4)
{
  Serial.print("Unknown error at address 0x");
  if (address<16)
    Serial.print("0");
  Serial.println(address,HEX);
}    
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");

delay(5000); // wait 5 seconds for next scan
}

Using print statements, I determined that it hangs between test 1 and test 2 or at the line

error = Wire.endTransmission();

Well, it does not show that.
afbeelding
Which pin do you use to power the sensor ?
Can you show a photo ? (a photo of your project, not a photo that you found online).

Sometimes there is a solder blob or a cheap board with a short circuit.
The multimeter should tell more. If you make a test with only Wire.begin() in setup() and nothing in the loop(), then SDA and SCL should be near 5V. But with this sensor, a little above 3.3V is also possible.
The sensor should also get 5V power of course.

1 Like

One way that I'm familiar with to cause Wire.endTransmission to hang is if SDA is being actively pulled low. That can happen for a number of reasons, but the ones that I'd start with are looking for an unpowered device, or one that is not hooked up correctly.

1 Like

I ran into an interesting one yesterday, I was testing a board using an UNO and the I2C Scanner which worked without any problems but the board would not. The board did not have pull up resistors installed so I added them externally and it worked without any problems. This I repeated several times. Two of the boards in the lot worked like this. Putting a scope on the I2C without resisters caused it to fail. Just thought you might have seen this one before.

2 Likes

Here are some images:



I wasnt sure how I could get clearer images - ir you have any suggestions let me know
To be clear, pin 1 is in 5v, pin 2 is in SDA, pin 3 is in SCL, pin 4 is in GND, and pin 5 is in GND.

Thank you for the photos. Nothing wrong there. I hope that your sensor is not damaged.

Thank you for the photos, you appear to have 5V and Gnd reversed, it looks like black is in 5V and red in Gnd.

You have not responded to my comment about pull up resistors.

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