setI2CAddress()
[Inter Integrated Circuit]
void setI2CAddress(unsigned char address);Description
Sets the I2C address of the chip in follower mode.
This can be used in cases where the chip also determines the address of the device based on the state of its pins.
Parameters
address: the address of the chip.
Returns
Nothing.
Example Code
The code initializes the I2C communication with the SDA pin connected to pin 4, the SCL pin connected to pin 5, and the address of the chip set to 0x20.
Clears the first four bits of the address variable and sets them to the values of the pins 1, 2, 3, and 4.
static int address = 0x20;
static unsigned char mask = 0xFF;
void setup() {
beginI2C(4, 5, address); // initializes the I2C with the SDA at pin 4, the SCL at pin 5, and the address of the chip set to 0x20
}
void loop() {
address = address & mask; // clears the first four bits of the address variable
address |= digitalRead(1) << 0; // sets the least significant bit of the address to the value of the pin 1
address |= digitalRead(2) << 1; // sets the second least significant bit of the address to the value of the pin 2
address |= digitalRead(3) << 2; // sets the third least significant bit of the address to the value of the pin 3
address |= digitalRead(4) << 3; // sets the most significant bit of the address to the value of the pin 4
setI2CAddress(address); // sets the I2C address of the chip to the value of the address variable
}Notes and Warnings
The setI2CAddress() won’t work if the chip is acting as
a leader device.
