In application design, it is very common and straightforward to drive multiple I2C devices with one I2C bus. This project will connect the following I2C devices:
1x 16×2 I2C LCD display, address 0x27
1x 128×32 I2C OLED display, address 0x3C
2x PCF8574 I2C IO expanders, addresses 0x20, 0x21
The above components can be controlled using the following libraries through Arduino Uno:
LiquidCrystal_I2C.h to control the LCD screen;
Wire.h and PCF8574.h to control the I2C IO expanders;
Adafruit_GFX, Adafruit_SSD1306.h, and SPI.h to control the SSD1306 128×32 OLED display.
Since all components are not far apart, it is unnecessary to use pull-up resistors on the I2C bus, as these Dupont wires are part of the components.
The circuit of this project is very intuitive; the difference is that an I2C port is added near the USB adapter at the top of the Uno board, and we use it together with A4 and A5.
First, connect the SDA pins of all I2C components in series, and then connect them to the Arduino SDA pin (usually A4).
Second, connect the SCL pins of all I2C components in series, and then connect them to the Arduino SCL pin (usually A5).
Third, connect all 5v (Vcc) wires to the 5v pin on the Arduino board, and connect all ground wires (GND) to the GND pin on the Arduino board.
Fourth, connect one pin of the 4 LEDs through a resistor with a value between 640-1000 ohms to the P0 and P1 pins of the two PCF8574 IO expanders, and connect the other pin to ground.
This project uses an external 5v power supply because, according to the actual needs of the Uno, it is unnecessary to obtain too much current from the voltage regulator.
After the connections are complete, check all the wiring, then open the I2C scanner tool in the Arduino IDE, and you will see the Wire.h library in the example.
Power on, upload the library file to the Uno, and then open the Serial Monitor to see that 4 I2C devices have been detected.
If 4 devices are not detected, check the wiring and device addresses to see where the issue is; it is likely a wiring problem or the device address is incorrect.
If everything is correct, copy the following code into the Arduino IDE, and refer to the comments in the middle for related explanations.
To copy the code, please click to read the original text.