Hey, guys. Can anyone help me with something? In my project, I would like to display the distance data from the laser sensors vl53IoX on the SH1106 display. To control the display I use U8g2lib.h library. Everything is controlled via rp2040, which has two I2C buses. Sensors are connected to the I2C0 bus on pins 5 and 8 and display to the I2C1 on pins 14 a 15. I am programming it through Arduino IDE. I would like the sensors to be on one bus and the display on the other. I have a minor problem with the display functioning on the second I2C1 bus.
If I am using the SW I2C bus (I2C1, pins 15 and 14), sensor data or Hello World is displayed on the screen, but with the wrong display rendering.
U8G2_SH1106_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0,15,14,U8X8_PIN_NONE);
If I am using the HW I2C bus (I2C0) on predefined pins 4 and 5, the display works only with Hello World, not with sensors, but rendering is okay.
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
Here is the code, I need help setting it to use the second bus I2C1 on the pins I need (14 and 15) and to render the display normally. I will be grateful for any advice. Thanks ![]()
#include <Wire.h>
#include <Adafruit_VL53L0X.h>
#include <U8g2lib.h>
#define SDA0_PIN 8
#define SCL0_PIN 5
#define SDA1_PIN 14
#define SCL1_PIN 15
#define SHT_LOX1 7
#define SHT_LOX2 6
#define LOX1_ADDRESS 0x37
#define LOX2_ADDRESS 0x38
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();
U8G2_SH1106_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0,15,14,U8X8_PIN_NONE);
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
//#define U8G2_HAVE_2ND_HW_I2C
int vzdialenostL = 0;
int vzdialenostP = 0;
VL53L0X_RangingMeasurementData_t measure1, measure2;
void setID() {
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
delay(10);
digitalWrite(SHT_LOX1, HIGH);
delay(10);
if (!lox1.begin(LOX1_ADDRESS, &Wire)) {
Serial.println(F("Nepodarilo sa inicializovaĹĄ senzor LOX1!"));
while (1);
}
digitalWrite(SHT_LOX2, HIGH);
delay(10);
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) {
vzdialenostL = measure1.RangeMilliMeter;
Serial.print(vzdialenostL);
} else {
vzdialenostL = -1;
Serial.print(F("Mimo rozsahu"));
}
Serial.print(F(" mm, Senzor 2: "));
if (measure2.RangeStatus != 4) {
vzdialenostP = measure2.RangeMilliMeter;
Serial.print(vzdialenostP);
} else {
vzdialenostP = -1;
Serial.print(F("Mimo rozsahu"));
}
Serial.println(F(" mm"));
}
void displayReadings() {
//u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.firstPage();
do {
u8g2.setCursor(0, 12);
u8g2.print("Senzor 1: ");
if (vzdialenostL != -1) {
u8g2.print(vzdialenostL);
u8g2.print(" mm");
} else {
u8g2.print("Mimo rozsahu");
}
u8g2.setCursor(0, 28);
u8g2.print("Senzor 2: ");
if (vzdialenostP != -1) {
u8g2.print(vzdialenostP);
u8g2.print(" mm");
} else {
u8g2.print("Mimo rozsahu");
}
} while ( u8g2.nextPage() );
//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);
Wire.setSDA(SDA0_PIN);
Wire.setSCL(SCL0_PIN);
Wire.begin();
//Wire1.setSDA(SDA1_PIN);
//Wire1.setSCL(SCL1_PIN);
//Wire1.begin();
u8g2.begin();
Serial.println(F("Inicializácia..."));
setID();
}
void loop() {
read_dual_sensors();
displayReadings();
delay(500);
}

