15. Networking and Communications

In this week I tried to communicate between two Arduinos using I2C protocol, where the master will be attached with a pushbutton and the signal will be transferred to the slave Arduino to light up the LED.

Group Assignment

Our group work you can find it in the LINK

I2C protocol

Before I start working on documentation with any week I always do my own research to understand more also it helps me to combine the information from different resources for better understanding, for this week I referred to the Arduino Community which is one of the main resources I refer to when I’m working in electronics, following Tutorial Master Writer/Slave Receiver was helpful to start with this week.

What is I2C

I2C is a serial protocol for two-wire interface to connect low-speed devices like microcontrollers, EEPROMs, A/D and D/A converters, I/O interfaces and other similar peripherals in embedded systems. It was invented by Philips and now it is used by almost all major IC manufacturers.I2C uses only two wires: SCL (serial clock) and SDA (serial data). Both need to be pulled up with a resistor to +Vdd. There are also I2C level shifters which can be used to connect to two I2C buses with different voltages as it is defined by https://i2c.info/.

2 Arduino Communications

In this week I tried to communicate between two Arduinos using I2C protocol, where the master will be attached with a pushbutton and the signal will be transferred to the slave Arduino to light up the LED.

I used tinkercad to draw the wiring,

As you can see I will be using the internal pullup resistor to operate the pushbutton in the master Arduino,

Once I was satisfied with the wiring I started writing the code as following;

#include to call the library which allows to communicate with I2C devices.

Wire.begin(); to initiate the Wire library and join the I2C bus as a master or slave. This should normally be called only once.

Wire.beginTransmission(); to begin a transmission to the I2C slave device with the given address.

Wire.write() send data from a master to slave device.

Wire.endTransmission(); to end a transmission to a slave device

Wire.onReceive(); to register a function to be called when a slave device receives a transmission from a master.

Wire.read(); to Read data that was transmitted from a master to a slave.

The Master code

For the master code the following main modification were made as follow:

  • Declaring sensorPin variable for Push button pin number .

  • Declaring sensorValue variable for Push button readings.

  • pinMode(sensorPin, INPUT_PULLUP); syntax is used to configure the sensorPin to behave as an input with internal pullup resistor.
  • sensorValue = digitalRead(sensorPin); syntax is used to read the value from a sensorPin and save it as sensorValue.
  • Wire.write(sensorValue); syntax is used to push button data the slave .
#include <Wire.h>

int x = 0;
int sensorPin = 2;
int sensorValue = 0;

void setup() {
// Start the I2C Bus as Master
 pinMode(sensorPin, INPUT_PULLUP);
Wire.begin();
Serial.begin(9600);

}

void loop() {

sensorValue = digitalRead(sensorPin);
Serial.println(sensorValue);
Wire.beginTransmission(9);

// transmit to device #9

Wire.write(sensorValue);

delay(100);

Wire.endTransmission();

// stop transmitting

}

The Slave

For the slave code the following main modification were made as follow:

  • Declaring LED variable for the LED pin number .
  • pinMode (LED, OUTPUT); syntax is used to configure the LED pin to behave as an OUTPUT.
  • if & else syntax is used which is a Conditional Statement to light up the led whenever the slave arduino receives 1 and turn of the led when it receives 0.

  • digitalWrite(LED, LOW or HIGH); Write a HIGH or a LOW value to the LED pin.

 #include <Wire.h>
 // Include the required Wire library for I2C

 int LED = 10;

 int x = 0;

 void setup() {

 // Define the LED pin as Output

 pinMode (LED, OUTPUT);

 // Start the I2C Bus as Slave on address 9
 Wire.begin(9);
 // Attach a function to trigger when something is received.
 Wire.onReceive(receiveEvent);
 //bit rate for data transfer over Serial communication
 Serial.begin(9600);

 }

 void receiveEvent(int bytes) {
 x = Wire.read(); // read one character from the I2C
 }

 void loop() {

 Serial.print("X is: ");
 Serial.println(x);

 if (x==0){
 digitalWrite(LED, LOW);
 }
 else

 {
 digitalWrite(LED, HIGH);

 }

 delay(100);
 }

The video

Attached files

Master Code

Slave Code