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.
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.
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
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.
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.