Multiple I2C libraries

FiveO
Posts: 27
Joined: Mon Oct 16, 2017 6:41 am

Re: Multiple I2C libraries

Post by FiveO » Sat Dec 16, 2017 9:01 am

Adafruit VL6180 - https://github.com/adafruit/Adafruit_VL6180X
Adafruit SSD1306 stm32 - https://github.com/rogerclarkmelbourne/ ... it_SSD1306

and I2C Anything. Same problem. Hangs with SSD1306 or VL6180
http://www.gammon.com.au/forum/?id=10896&reply=8#reply8

Each of these works well alone.

stevestrong
Posts: 2061
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Multiple I2C libraries

Post by stevestrong » Sat Dec 16, 2017 9:23 am

Have you tried the i2c_scanner example when both are connected?
Are they connected to the same I2C lines?

Have you tested the Adafruit OLED example? https://github.com/adafruit/Adafruit_VL ... x_oled.ino
It seems that it is using the same ICs as you.

FiveO
Posts: 27
Joined: Mon Oct 16, 2017 6:41 am

Re: Multiple I2C libraries

Post by FiveO » Sat Dec 16, 2017 1:06 pm

Yes. I2C scanner finds 0x29 and 0x3C.

Yes. Same lanes B6 and B7 on stm32f103 "bluebill"

Yes. Not working with original ssd1306 or that stm32 version.

stevestrong
Posts: 2061
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Multiple I2C libraries

Post by stevestrong » Sat Dec 16, 2017 4:29 pm

Do you have 4k7 pull-up resistors connected to the I2C lines and 3.3V?

The Adafruit example sketch is different from yours, the VL instance is declared before the OLED: https://github.com/adafruit/Adafruit_VL ... ed.ino#L16

So please try that example and tell us where it hangs.

FiveO
Posts: 27
Joined: Mon Oct 16, 2017 6:41 am

Re: Multiple I2C libraries

Post by FiveO » Sun Dec 17, 2017 10:27 am

Yes I tried. But I tried again, now with pull-up resistors.

The original vl6180x_oled.ino:
Didn't have 4k7
without - hangs after "Adafruit VL6180x test!"
2k - hangs after "Adafruit VL6180x test!"
4k - hangs after "Adafruit VL6180x test!"
10k - hangs after "Adafruit VL6180x test!"

Modified vl6180x_oled.ino with Adafruit_SSD1306_STM32.h (Adafruit_SSD1306.h don't work):
With this code:

Code: Select all

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306_STM32.h>

#include <Wire.h>
#include "Adafruit_VL6180X.h"

Adafruit_VL6180X vl = Adafruit_VL6180X();

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()
{
  Serial.begin(115200);
  while (!Serial) ; // wait for a serial connection via USB
  delay(1000);

  Serial.println("SSD1306 begin"); // check point
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done
  display.display();
  delay(1000);

  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");

  // text display big!
  display.setTextSize(4);
  display.setTextColor(WHITE);
}

void loop()
{
  Serial.println("In loop"); // check point

  float lux = vl.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux: "); Serial.println(lux);

  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print(range);
    display.print("mm");
    display.display();
    Serial.println();
  } else {
    display.display();
    display.clearDisplay();
    return;
  }

  // Some error occurred, print it out!

  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  delay(10);
}
Didn't have 4k7
SAME RESULTS
without - hangs after "Adafruit VL6180x test!"
2k - hangs after "Adafruit VL6180x test!"
4k - hangs after "Adafruit VL6180x test!"
10k - hangs after "Adafruit VL6180x test!"

zmemw16
Posts: 1681
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Multiple I2C libraries

Post by zmemw16 » Sun Dec 17, 2017 1:17 pm

is Wire.h guarded for multiple includes ?

what I2C clock speed is this defaulting too ?

does any of the hardware have regulators, are you feeding them 5v or 3v3 ?

have you got a bus analyzer ?

stephen

FiveO
Posts: 27
Joined: Mon Oct 16, 2017 6:41 am

Re: Multiple I2C libraries

Post by FiveO » Mon Dec 18, 2017 11:45 am

to zmemw16

1. Can you be a bit more specific?
2. I haven't change a thing. In which file I can look it up?
3. Yes, both have 3.3v to 5v. I use 3.3v additional power supply with grounds connected.
4. No I don't.

zmemw16
Posts: 1681
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Multiple I2C libraries

Post by zmemw16 » Mon Dec 18, 2017 4:16 pm

it's usual to wrap the contents of an include file in an if-ndef to prevent double inclusion

Code: Select all

#ifndef _TWOWIRE_H_
#define _TWOWIRE_H_

#include "utility/WireBase.h"
#include "wirish.h"
#include <libmaple/i2c.h>

class TwoWire : public WireBase {
private:
    i2c_dev* sel_hard;
    uint8    dev_flags;
protected:
    /*
     * Processes the incoming I2C message defined by WireBase to the
     * hardware. If an error occured, restart the I2C device.
     */
    uint8 process(uint8);
    uint8 process();
public:
    /*
     * Check if devsel is within range and enable selected I2C interface with
     * passed flags
     */
    TwoWire(uint8, uint8 = 0);
    // blah blah blah rest of file etc
 #endif
unchanged, so i'd expect 100kHz

3.3v 5v, are you connecting the devices to 5v and there's a 3v3 regulator - fine
results of feeding 3.3v as an input to a 5v regulator are not defined
results of feeding 3.3v to the output of a regulator are probably 3.3v, hopefully

logic analyser - would've been helpful to capture and display the i2c states

too summarise then,
they work together on arduino nano/uno ?
do both work separately on arduino nano/uno ?
does either work separately on stm32 ?

i tend to use resistors 3k3 on 3v3 and 5k1 on 5v for i2c pullups

stephen

FiveO
Posts: 27
Joined: Mon Oct 16, 2017 6:41 am

Re: Multiple I2C libraries

Post by FiveO » Tue Dec 19, 2017 10:20 am

The sensor and oled are up to 5v tolerant. I use 3.3v dc power supply on them. The stm32f103 bluebill is using usb voltage and grounds between stm32f103 and 3.3v external power supply.

Yes they work together. I have tested them together with Arduino Nano and Mega, without resistors.
Yes they work also separately on Nano and Mega, without resistors.
Yes they work separately on stm32f103, without resistors.

stevestrong
Posts: 2061
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: Multiple I2C libraries

Post by stevestrong » Tue Dec 19, 2017 11:52 am

I think you would need an oscilloscope or a logic analyzer to find out the root cause.

You should remove

Code: Select all

#include <SPI.h>
from the sketch, since none of libraries uses SPI.

Post Reply