Skip to content

14. Networking and communications

Introduction

This week is one of the unfinished weeks from 2020, so I can say that I don’t have any experience with networking boards. For 2024, I plan to make things work. Thanks to the previous weeks, I have a board that can be used for this week. The plan is to do an I2C connection with my board and the ESP8266, but after a brief discussion with our instructor Maxime, we decided to use an Arduino Uno. There are some basics for this week that I have to know and understand. The first thing that I need to understand what is i2c and why do I need SDA and SCL pins.

What is I2C

I2C, or Inter-Integrated Circuit, is a communication protocol that allows multiple electronic devices to communicate with each other using just two wires: one for data (SDA) and one for the clock signal (SCL). It’s commonly used for connecting sensors, displays, and other peripherals to microcontrollers. Each device on the I2C bus has a unique address, making it easy to communicate with multiple devices using the same two wires. It’s simple and efficient for short-distance communication within devices.

Primary and secondary

In a networking setup with two boards, the primary board acts as the master, controlling the communication and sending commands. The secondary board, responding to the master’s requests and sending data back. This setup allows the primary board to manage and coordinate tasks across multiple connected devices.

Experience

The reason is that during the output device week, we changed the 3.3V regulator to 5V to make the pump work. So, on my board, we have 5 volts, and the ESP8266 uses 3 volts. We didn’t want to risk it and decided to use the Arduino Uno, which can handle 5V.

Starting with arduino uno first of all I need pinout.

week14

A4 and A5 pins are needed for connecting to my board.

week14

Then I connecting the programmer to my board and started writing the program. The goal of the program is to make two laptops communicate. I want to write in my computer’s dialog box, and the other computer will receive the message. The Arduino will be the primary board, and mine will be the secondary.

Connection: I connected SDA and SCL, provided a common ground for the two boards, and connected them to the computers.

week14

Start coding. Firs example of code is here. this is the receiver code.

week14

As you can see here, I have a problem: the message is received in numbers. I have found a few good tutorials on how to send and receive data via I2C in connected Arduinos. My limitation now is how to transmit different types of data. The communication via I2C using the Wire library only allows the transmission of numbers between 0 and 255. While researching solutions for this problem, I found out that it is a pretty common question in the Arduino community. The solution is to convert the received numbers to characters.

week14

#include <Wire.h>
#define I2C_Secondary1_ADDRESS 0x9 
bool ledState; 
int led=3; 
void setup() {
  Wire.begin(I2C_Secondary1_ADDRESS);                
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);  
  Serial.println("Starting");   
}
void loop() {
 receiveEvent(1);

}
void receiveEvent(int numBytes) {
  byte recivedData=0;
  if(Wire.available()){
    recivedData=Wire.read();
    Serial.print((char)recivedData);
  }
}

Here is screenshot of code that is uploaded in arduino(the master one).

week14

#include <Wire.h>
String message;
void setup() {
  Wire.begin();
  Serial.begin(9600);
}
void loop() {
  if (Serial.available()) {
    message = Serial.readString();
    // char messageArray[message.length()+1];
    // message.toCharArray(messageArray, sizeof(messageArray));
    Serial.print(message);
    Wire.beginTransmission(0x9);
    Wire.write(message.c_str());
    Wire.endTransmission();
  }
}

A serial connection was made between two computers. The first version of the code used a char messageArray, which was working just fine, but with our team’s suggestion, I tried using Serial.readString(). It also worked well. It was faster and the code was shorter.

Group assignment

For group assignment we tried I2C communication interface between our boards. We used ATtiny1614 boards, UPDI programmer with RP2040, Joistick, 2 LEDs.

week14

Conclusion

This week was an interesting experiment with using more than one board. To fully understand how useful networking skills are, I need to use more than two boards. I’m sure I will use these skills in the future. Networking is especially useful if I want to make some kind of machine that has several outputs and inputs, all of which have to communicate with each other.