How to define two different SDA and SCL?

Hi, I have such a problem, namely I need two SDA and SCL.
STM32F401 I have:
PB7 - SDA1
PB6 - SCL1

And when I use the Wire.begin () call as standard, it all works. The problem arises when I want to use other SDA / SCL
PA8 - SCL3
PB4 - SDA3

Therefore, I have a question how to correctly define two SDA / SCL in the code ??

Thank you in advance for your help

SDA/SCL is a BUSS. you only need the wire.h driver. Then the individual device invocations will (must) have unique device ID. Since each device only responds when it sees its specific address, the devices are unaware of each other. There are problems when too many devices have pullups, but two or three usually work without issue.

1 Like

Hi, @PYJTER

You are dealing with I2C, why do you need two I2C busses?

What are the devices that you are communicating with?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

as long as the devices can be adjusted to have different I2C-adresses it works straight forward. Different kinds of sensors / devices usually do have different I2C-adresses anyway.

a lot of I2C-devices have adress-pins for adjusting the I2C-adress.

In case you have two devices with unchangeable but identical I2C-adress you could use a I2C-multiplexer.

best regards Stefan

1 Like

I wanted to display what the compass shows on the lcd display. The codes work separately. I don't know how to connect them. That's why I wanted to use a different SDA / SCL.
Sorry, but I don't know much about arduino.

compass code

#include <Wire.h>
#include <LSM303.h>
LSM303 compass;

void setup()
{
Serial.begin(9600);
Wire.begin();
compass.init();
compass.enableDefault();
Serial.println("Magnetometer Uncalibrated (Units in Nanotesla)"); 
}

void loop()
{
compass.read();
float Xm_print, Ym_print, Zm_print;

Xm_print = compass.m.x; // Gain X [LSB/Gauss] for selected sensor input field range (1.3 in these case)
Ym_print = compass.m.y; // Gain Y [LSB/Gauss] for selected sensor input field range
Zm_print = compass.m.z;  // Gain Z [LSB/Gauss] for selected sensor input field range

Serial.print(Xm_print, 10); Serial.print(" "); Serial.print(Ym_print, 10); Serial.print(" "); Serial.println(Zm_print, 10);
delay(50);
}

LCD 4x20 code

#include <SoftWire.h>

#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>

// somehow, this part is needed this to make it print
//             SCL, SDA
SoftWire Swire(PB6,PB7);

// Check the address via I2C Scanner
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {
// put your setup code here, to run once:
Wire.begin();
Wire.beginTransmission(0x27);
 lcd.begin(20, 4); // initialize the lcd
}

void loop() {
// put your main code here, to run repeatedly:
lcd.setBacklight(100);
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("First Line ");
lcd.setCursor(0, 1);
lcd.print("...");
lcd.setCursor(0, 2);
lcd.print("...");
lcd.setCursor(0, 3);
lcd.print("...");
delay(1000);
}

I found information that you can define a second wire, but unfortunately it does not work for me.

If I used one i2c line, it would be enough to connect all SD lines with each other and all SCL lines and resitors 10k?? Do I need to add anything else?

Yes. They are called SDA and SCL, but the most important wire of the I2C bus is the GND wire :wink:

Each I2C device (such as sensors and displays) has a I2C address. There can be many devices on the I2C bus. The libraries take care that they talk only to their device and not to other devices.
Sometimes I2C devices have the same I2C address. I think your devices don't have that, so that is no problem.

Use the hardware I2C bus in its default mode, don't use the SoftWare if you don't have to. Don't include libraries that you don't use.

Can you give links to your STM32 board and to the modules that you use (preferably links to where you bought them). Some modules are low quality, some modules have voltage regulators, some modules have already pullup resistors, and so on.

1 Like

I don't think you can put links to the store, so I will write that I bought it in the Mouser and Kamami store.

  1. Stm32f401ccu6 blackpill
  2. LSM303D -> https://www.pololu.com/product/2127
  3. LCD 2004A 20x4 HD44780 Blue

I will try to comprehend it as you write on the same line i2c

I believe there is a thing called wire1.h for devices with more than 1 I2C bus.

Yes, you can chain devices on a I2C bus. Each device have its own address that you call, that's how you differentiate between them.

Only in the case of conflicting addresses or when the amount of devices on the bus is too much will you need to use a separate bus.

1 Like

The Pololu sensor has I2C voltage level shifters on the module. You should power it with 3.3V to VIN. It has 2350Ω pullup for SDA and SCL (4k7 on both sides of the level shifter). You don't need more pullup.

Pololu says that when using 3.3V, the 3.3V can be connected to VDD and the VIN left open. That is weird, I think there must be additional pullup resistors on the I2C bus to make that more reliable.

The display is probably a 5V display, that is not 100% compatible with your 3.3V I2C bus. It might have 10k pullup resistors that try to lift the voltage of SDA and SCL towards 5V.

1 Like

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