Waveshare rp2040 I2C buses

Hey, guys. Could someone help me with something please ? I would like to use both I2C buses with waveshare rp2040. I have two vl53IoX laser distance sensors and one sh1106 display. The sensors are on the I2C0 bus on pins 8 and 5 and the display on I2C1 on pins 14 and 15. Scanner finds them on the right buses but this code of mine doesn't work. For any advice I will be glad. Thanks :slight_smile:

#include <Wire.h>
#include <Adafruit_VL53L0X.h>
#include <U8g2lib.h>

// I2C0 (VL53L0X sensors) -> GPIO8 (SDA0), GPIO5 (SCL0)
#define SDA0_PIN 8
#define SCL0_PIN 5

// I2C1 (OLED display) -> GPIO14 (SDA1), GPIO15 (SCL1)
#define SDA1_PIN 14
#define SCL1_PIN 15

// Shutdown pins for VL53L0X sensors
#define SHT_LOX1 7
#define SHT_LOX2 6

// I2C addresses for VL53L0X sensors
#define LOX1_ADDRESS 0x37
#define LOX2_ADDRESS 0x38

Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();

// Use software I2C for OLED (so it can use Wire1)
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, SCL1_PIN, SDA1_PIN, U8X8_PIN_NONE);


int distanceL = 0;
int distanceR = 0;
VL53L0X_RangingMeasurementData_t measure1, measure2;

void setID() {
    // Reset both sensors
    digitalWrite(SHT_LOX1, LOW);
    digitalWrite(SHT_LOX2, LOW);
    delay(10);

    // Activate LOX1 first
    digitalWrite(SHT_LOX1, HIGH);
    delay(10);

    // Initialize LOX1 on I2C0
    if (!lox1.begin(LOX1_ADDRESS, &Wire)) {
        Serial.println(F("Nepodarilo sa inicializovaĹĄ senzor LOX1!"));
        while (1);
    }

    // Activate LOX2
    digitalWrite(SHT_LOX2, HIGH);
    delay(10);

    // Initialize LOX2 on I2C0
    if (!lox2.begin(LOX2_ADDRESS, &Wire)) {
        Serial.println(F("Nepodarilo sa inicializovaĹĄ senzor LOX2!"));
        while (1);
    }
}

void read_dual_sensors() {
    lox1.rangingTest(&measure1, false);
    lox2.rangingTest(&measure2, false);

    Serial.print(F("Senzor 1: "));
    if (measure1.RangeStatus != 4) {
        distanceL = measure1.RangeMilliMeter;
        Serial.print(vzdialenostL);
    } else {
        distanceL = -1;
        Serial.print(F("Mimo rozsahu"));
    }

    Serial.print(F(" mm, Senzor 2: "));
    if (measure2.RangeStatus != 4) {
        distanceR = measure2.RangeMilliMeter;
        Serial.print(vzdialenostP);
    } else {
        distanceR = -1;
        Serial.print(F("Mimo rozsahu"));
    }

    Serial.println(F(" mm"));
}

void displayReadings() {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_6x10_tr);

    u8g2.setCursor(0, 12);
    u8g2.print("Senzor 1: ");
    if (distanceL != -1) {
        u8g2.print(distanceL);
        u8g2.print(" mm");
    } else {
        u8g2.print("Mimo rozsahu");
    }

    u8g2.setCursor(0, 28);
    u8g2.print("Senzor 2: ");
    if (distanceR != -1) {
        u8g2.print(distanceR);
        u8g2.print(" mm");
    } else {
        u8g2.print("Mimo rozsahu");
    }

    u8g2.sendBuffer();
}

void setup() {
    Serial.begin(115200);

    pinMode(SHT_LOX1, OUTPUT);
    pinMode(SHT_LOX2, OUTPUT);

    // Reset sensors
    digitalWrite(SHT_LOX1, LOW);
    digitalWrite(SHT_LOX2, LOW);
    delay(10);

    //  Initialize I2C0 (VL53L0X sensors)
    Wire.setSDA(SDA0_PIN);
    Wire.setSCL(SCL0_PIN);
    Wire.begin();  // Start I2C0

    //  Initialize I2C1 (OLED display)
    Wire1.setSDA(SDA1_PIN);
    Wire1.setSCL(SCL1_PIN);
    Wire1.begin();  // Start I2C1

    //  Initialize OLED using software I2C
    u8g2.begin();

    Serial.println(F("Inicializácia..."));
    setID();
}

void loop() {
    read_dual_sensors();
    displayReadings();
    delay(500);
}

What do you mean by "doesn't work"?

I don't know exactly that how to use and set two buses on different pins. The distance from the sensors is not shown on the display. I've tested each device myself but it doesn't work.

See post #2. Additionally, a picture of a hand-drawn circuit, clearly labelled. links to datasheets for sensors, etc, any error logs posted in code tags. Then we can begin to look at helping.

hmm idk what do you want sorry

is this okay ?

void setup() {
    Serial.begin(115200);

    pinMode(SHT_LOX1, OUTPUT);
    pinMode(SHT_LOX2, OUTPUT);

    // Reset sensors
    digitalWrite(SHT_LOX1, LOW);
    digitalWrite(SHT_LOX2, LOW);
    delay(10);

    //  Initialize I2C0 (VL53L0X sensors)
    Wire.setSDA(SDA0_PIN);
    Wire.setSCL(SCL0_PIN);
    Wire.begin();  // Start I2C0

    //  Initialize I2C1 (OLED display)
    Wire1.setSDA(SDA1_PIN);
    Wire1.setSCL(SCL1_PIN);
    Wire1.begin();  // Start I2C1

    //  Initialize OLED using software I2C
    u8g2.begin();

    Serial.println(F("Inicializácia..."));
    setID();
}

That code is incomplete, but never mind for now. Have you run the example sketches for the VL53LOX, that will prove they work, also for the display?

Standard debugging approach, use a known correct piece of code like a sample sketch for the TOF, that will prove the I2C is set up right, and that the TOF work. Same with the display. Update when you complete that.

Yes, I'll try a few examples. These sensors and the dispel should work together. It's just that it's not working for me on the new buses I've set up.

Okay I will try. Thank you

The sensors work but the display doesn't. Which library would you recommend for controlling the display?

What display are you using? Manufacturer and common name.

SH1106_128X64.

On predefined pins 5 and 4 the display works, but when I put it on 14 and 15 it doesn't work.

#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>  // Include Wire

// Define custom I2C pins (SDA = 15, SCL = 14)
#define SDA_PIN 15
#define SCL_PIN 14

// Initialize the display using I2C (no need to specify custom pins in constructor)
U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

void setup(void)
{
  // Set custom I2C pins for RP2040 (Waveshare)
  Wire1.setSDA(SDA_PIN);
  Wire1.setSCL(SCL_PIN);

  // Initialize I2C
  Wire1.begin();

  u8x8.begin();
  u8x8.setPowerSave(0);
}

void loop(void)
{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.drawString(0,1,"Hello World!");
  u8x8.setInverseFont(1);
  u8x8.drawString(0,0,"012345678901234567890123456789");
  u8x8.setInverseFont(0);
  u8x8.refreshDisplay();  // Refresh the display
  delay(2000);
}

That is a I2C display. Just connect it to the boards I2C pins. Since I don;t know the precise board you are using, I can't help more. If you post a picture of the front and back of the board I might be able to tell more. Since I think you said Waveshare, they might have 2040 boards custom made so looking at the 2040 soec is no help. MOST waveshare products come with a WiKi link, can you supply that, that is generally very good doc'n. Do you want me to read it or can you do that. Looking for I2C available pins.


The sensors are on the I2C0 bus on pins 8 and 5 and the display on I2C1 on pins 14 and 15.
Sensors working but display not. Display work only on pins 4 and 5

What board do I select for that? There are dozens.

waveshare rp2040 zero this: