i2C Sensor not working

I have an i2C sensor that works fine on an Arduino (please see attached).

.

With the same wiring and code (with changing pins to Dx etc.), I am not getting any readings on a Photon.

I also tried connecting 4.7KOhm resistors (attached) but I still no readings.

i2C%20Resistors%202

I would appreciate any input as to what the reason may be

@Jimmie, posting your code and a link to the sensor specs might be a good start.

In addition to the code as @peekay123 said, it does not look like your connections match the diagram above for the Arduino. In particular GND seems to on the other side of the data connections.

If you needed a 680uF cap for the Arduino, you almost certainly need one for the Particle device as well.

EDIT: And you should try an i2c scanner app to see if it can find the device at all.

3 Likes

You seem to be powering via 3v3 - that’s too little if I belive the table you posted (4.75V ~ 5.5V)

As long your Electron is powered from USB, you can use Vin to power the sensor.

Also without the LiPo connected, your device may not be able to reliably connect to the cloud and hence not start running your code (unless you are using non-AUTOMATIC or multi threaded mode - hence the request to see your code).

1 Like

I2C Scanner: https://go.particle.io/shared_apps/5ac7a964926c9fe8cb00049e

Normally, you do need pull-up resistors for I2C (afaik), especially if the sensor doesn’t already have them internally, so leave that in your setup.

Also, you state your sensor needs between 4.75 to 5.5V to operate, but if you have wired it the same I did (as shown in the image), you’d be only supplying 3.3V right now.

Thank you @peekay123, @bko, @Scruffr and @Vitesze.

I am using the Garmin Lidarlite sensor.

My code is below. The earlier circuit picture is not my installation, it is from an online image showing the resistors. My circuit is shown below and I am definitely supplying the sensor with 5V. The Arduino circuit does not need the capacitor. I did try a capacitor with the photon but still no readings.

Here is my code

#include "Wire.h"
#include "LIDARLite.h"

LIDARLite myLidarLite;

void setup()
{
myLidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz
myLidarLite.configure(0); // Change this number to try out alternate configurations
}

void loop()
{
Particle.publish("Distance: ", String(myLidarLite.distance()));
delay(3000);

// Take 99 measurements without receiver bias correction and print to serial terminal
for(int i = 0; i < 99; i++)
{
myLidarLite.distance(false);
}
}

Have you tried the I2C scanner?
I can’t see pull-ups so you assume the sensor does have its own pull-ups?
Have you got a link to the library used?
A link to the datasheet of the sensor?

Hi @ScruffR

Yes, I tried the i2C scanner (forgot to mention it). The scanner does locate the sensor at address 98.

While my picture does not show the resistors, I did try with resistors between the +ve and both clock and data but no difference.

BTW, here is the original Arduino code which works (without a capacitor):

/*------------------------------------------------------------------------------

LIDARLite Arduino Library
GetDistanceI2c

This example shows how to initialize, configure, and read distance from a
LIDAR-Lite connected over the I2C interface.

Connections:
LIDAR-Lite 5 Vdc (red) to Arduino 5v
LIDAR-Lite I2C SCL (green) to Arduino SCL
LIDAR-Lite I2C SDA (blue) to Arduino SDA
LIDAR-Lite Ground (black) to Arduino GND

(Capacitor recommended to mitigate inrush current when device is enabled)
680uF capacitor (+) to Arduino 5v
680uF capacitor (-) to Arduino GND

See the Operation Manual for wiring diagrams and more information:
http://static.garmin.com/pumac/LIDAR_Lite_v3_Operation_Manual_and_Technical_Specifications.pdf

------------------------------------------------------------------------------*/

#include <Wire.h>
#include <LIDARLite.h>

LIDARLite myLidarLite;

void setup()
{
Serial.begin(115200); // Initialize serial connection to display distance readings

/*
begin(int configuration, bool fasti2c, char lidarliteAddress)

Starts the sensor and I2C.

Parameters
----------------------------------------------------------------------------
configuration: Default 0. Selects one of several preset configurations.
fasti2c: Default 100 kHz. I2C base frequency.
  If true I2C frequency is set to 400kHz.
lidarliteAddress: Default 0x62. Fill in new address here if changed. See
  operating manual for instructions.

*/
myLidarLite.begin(0, true); // Set configuration to default and I2C to 400 kHz

/*
configure(int configuration, char lidarliteAddress)

Selects one of several preset configurations.

Parameters
----------------------------------------------------------------------------
configuration:  Default 0.
  0: Default mode, balanced performance.
  1: Short range, high speed. Uses 0x1d maximum acquisition count.
  2: Default range, higher speed short range. Turns on quick termination
      detection for faster measurements at short range (with decreased
      accuracy)
  3: Maximum range. Uses 0xff maximum acquisition count.
  4: High sensitivity detection. Overrides default valid measurement detection
      algorithm, and uses a threshold value for high sensitivity and noise.
  5: Low sensitivity detection. Overrides default valid measurement detection
      algorithm, and uses a threshold value for low sensitivity and noise.
lidarliteAddress: Default 0x62. Fill in new address here if changed. See
  operating manual for instructions.

*/
myLidarLite.configure(0); // Change this number to try out alternate configurations
}

void loop()
{
/*
distance(bool biasCorrection, char lidarliteAddress)

Take a distance measurement and read the result.

Parameters
----------------------------------------------------------------------------
biasCorrection: Default true. Take aquisition with receiver bias
  correction. If set to false measurements will be faster. Receiver bias
  correction must be performed periodically. (e.g. 1 out of every 100
  readings).
lidarliteAddress: Default 0x62. Fill in new address here if changed. See
  operating manual for instructions.

*/

// Take a measurement with receiver bias correction and print to serial terminal
Serial.println(myLidarLite.distance());

// Take 99 measurements without receiver bias correction and print to serial terminal
for(int i = 0; i < 99; i++)
{
Serial.println(myLidarLite.distance(false));
}
}

Library:

Sensor:

You may need to change some code in the library
https://github.com/garmin/LIDARLite_v3_Arduino_Library/blob/master/src/LIDARLite.cpp
e.g.

    #if ARDUINO >= 157
      Wire.setClock(400000UL); // Set I2C frequency to 400kHz, for Arduino Due
    #else
      TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
    #endif

I could imagine that this might not do what is needed, but I’m not sure what the Particle Arduino compatibility layer has defined as ARDUINO version.

Checking the USB Serial output of the library and maybe adding more debug print statements might be helpful.

1 Like

Thanks @Scruffr. Will try it but here is that part of the code in my sketch. Are you saying I should comment out?

void LIDARLite::begin(int configuration, bool fasti2c, char lidarliteAddress)
{
  Wire.begin(); // Start I2C
  if(fasti2c)
  {
    #if ARDUINO >= 157
      Wire.setClock(400000UL); // Set I2C frequency to 400kHz, for Arduino Due
    #else
      TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
    #endif
  }
  configure(configuration, lidarliteAddress); // Configuration settings
} /* LIDARLite::begin */

The docs mention that Wire.setClock() should actually be Wire.setSpeed() and wants to be called before Wire.begin()
https://docs.particle.io/reference/firmware/photon/#setspeed-

There might be other portions that may need attention too, but that was the first I noticed.

2 Likes

Thank you @ScruffR. Tried it but no change …

Tried that too?

Thank you, it is working now without resistors or a capacitor.

I followed the advice and made fresh copies of the library files. There may have been an error in the code that still compiled correctly.

Thanks again everyone.