

Inside the setup function, set LED pin as an ouput pin using pinMode() function. This variable x will be used to store the value received from a master Arduino. Also, we declare and initialize a variable x with zero. Pin D13 will turn on and off depending on received value from a master device. x++ ĭelay(200) Complete Master Arduino Code #include įirst, we define the symbolic name for pin D13 of Arduino. Value of x is incremented by step of 1 in loop with a delay of 200milli seconds, if value of x is greater than 6, this value is reset to 0 and again incremented from 0. Finally, the value of x has been transmitted and the bus has been spared using endTranmission () command.

The wire.write() function transmit current value of ‘x’ on I2C bus. After this, the value of x has been queued for transmission over the I2C bus.

Here transmission has been initiated with the slave address of 9. Here wire.begin() function has been called.

The value of this variable will be transmitted to the slave device. Here wire library has been included and variable x is initiated. Now first let’s discuss the Arduino master device code. After making connections, upload the codes of both slave and master to Arduino boards.After that, by using jumper wires common the ground of both the Arduino boards.Next to enable I2C communication, connect I2C pins A4 and A5 of master Arduino to the I2C pins of the slave Arduino.Connect the LED’s anode pin with Arduino digital pin 13 through a 220 ohm current limiting resistor. Also, it shows the connection of an LED with D13 of slave Arduino. The following figure shows the I2C connections between two Arduino boards. I2C connection between two Arduino Boards This picture shows master and slave devices connected with the I2C bus. In this picture, Arduino and Raspberry Pi are acting as a master and MPU6050, sensor devices as a slave. The communication between specific devices using two wires is accomplished by the fact that each device has its own unique device ID or address and using this address master can choose any specific device to communicate. Each slave device which is connected with the bus will have a unique 8-bit address. These lines are open-drain lines which means these need to be connected to pull up resistors if devices are active low. One wire is SCL (serial clock line) which synchronizes the transmission of data between devices and the other wire is SDA (serial data) which carries the actual data to be transmitted. The distinguishing feature of I2C over SPI is that it uses only two wires to carry out communication. It is only applicable for short distance data transmission. Many Sensors use this serial communication protocol to transfer their data to microcontrollers or through this protocol different slave circuits are able to communication with master circuits.
