As I have explained my understanding about I2C Communication before, how to deal and make a communication between microcontroller and I2C device. Here I would like to share a little tutorial about Arduino I2C.
Today, there are literally thousands of devices using I2C interface. Many application like RTC, digital compass, temperature and so on using I2C. It is because, I2C provide simplicity in wiring and communication. Even within 2 pins, SDA and SCL, we can control with up to 128 devices as slaves. Here, Arduino gives us an easy way to communicate with these I2C devices.
On Arduino boards, depends on Arduino type, there are two pins out that are used for I2C. For Arduino UNO, I2C pins out using Analog pin 4 (A4) as SDA and Analog pin 5 (A5) as SCL. But Arduino Mega, This board use another pin as I2C pins, pin 20 as SDA and pin 21 as SCL. So this will deferent too with other Arduino board.
Arduino Board | I2C pins |
UNO, Ethernet | A4(SDA), A5(SCL) |
Mega | 20(SDA), 21(SCL) |
Leonardo | 2(SDA), 3(SCL) |
Due | 20(SDA), 21(SCL), SDA1, SCL1 |
To communicate with I2C devices, Arduino has spoiled us with it's library. With this Arduino's library, we can communicate easily with all I2C devices. The Arduino's library that allow us to communicate with I2C devices called Wire.h. In this library, there are some functions to help us to make an I2C communication.
In this opportunity, I would like to make a tutorial how to use these functions. In this library, I separate the functions into 4 sections: Initiating, Transmitting, Receiving, and Responding in Slave Mode.
Initiating
This section is to initiate the Wire.h library and to define our Arduino as a master or a slave.
Wire.begin()
Initiating the Arduino in a master mode, with this function we can control the data transfer when connecting with other I2C devices.
Wire.begin(address)
Initiating the Arduino in a slave mode, filled with 7-bit slave address. We can define our I2C unique address here. For more detail about I2C addressing you can find it here.
Transmitting
This section is to communicate (starting, writing, and stopping) between master and slave. Used at master side.
Wire.beginTransmission(address)
Begin a transmission to I2C slave device with device's unique address.
Wire.write(data)
Write data from I2C slave device in response to a master's request, or queue bytes for transmission from master to slave. Place it between beginTransmission() and endTransmission().
Wire.endTransmission()
End a transmission to I2C slave device, and transmit the queue data.
Receiving
This section is to receive the buffered bytes of data from slave to master.
Wire.requestFrom(address, count)
Request "count" of data from I2C slave device at "address". Used at master side
Wire.available()
Return the number of bytes available to be retrieved by read(). This function should be called after function requestFrom() at master side or inside onReceive() handler at slave side.
Wire.read()
Read bytes of data that was transmitted from slave to master after asking for request in requestFrom() function or transmitted from master to slave.
Responding in Slave Mode
This section is only used at I2C slave device to communicate with master device (if we want to make our other Arduino act as a slave).
Wire.onReceive(handler)
Register a function to be called when a slave device receives a transmission from master or when master sends data. handler is the function to be called when the slave receives data, this should take a single int parameter and return nothing.
Wire.onRequest(handler)
Register a function to be called when a slave device sends a transmission to master or when master request for data. handler is the function to be called, takes no parameters and return nothing.
No comments:
Post a Comment