beginI2C()
[Inter Integrated Circuit]
void beginI2C (unsigned short sda, unsigned short scl, unsigned short address = 32768);Description
Initializes the I2C communication.
It is possible to set an address for the chip, in which case the chip will act as a follower.
Parameters
sda: the pin for the SDA line.
scl: the pin for the SCL line.
address: the address of the chip. This parameter is
optional and only necessary if the chip is acting as a
follower.
Returns
Nothing.
Example Code
For a Leader Device
The code initializes the I2C communication with the SDA pin connected to pin 4, and the SCL pin connected to pin 5.
void setup() {
beginI2C(4, 5); // initializes the I2C communication with the SDA pin connected to pin 4, and the SCL pin connected to pin 5
}
void loop() {
}For a Follower Device
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.
void setup() {
beginI2C(4, 5, 0x20); // 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
}
void loop() {
}Notes and Warnings
- The
beginI2C()function must be called only once in the setup. - The pins for the SDA and SCL lines must be specified, and will be set up as open-drain outputs.
