Skip to content

13. Networking and communications

Hero Shots

Assignement requirements

individual assignment:

  • design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s)

group assignment:

  • send a message between two projects

Group Assignement

Here you can fin the link to the group assignement.

Individual Assignement

I2C Protocol. How does it work ?

Basic Concept

The image depicts a connection diagram for a communication protocol known as I²C (Inter-Integrated Circuit), which is commonly used to connect microcontrollers to various sensors or devices.

Communication Lines

I²C is a serial communication protocol that uses two lines:

  • SDA (Serial Data Line): This line is used for data transfer between devices.
  • SCL (Serial Clock Line): This line provides a clock signal to synchronize the data transmission between devices.

Components

  • Master: This is the device that controls the I²C bus. It generates the clock signal (SCL) and initiates communication with the slave devices.
  • Slave 1 and Slave 2: These are devices controlled by the master. They receive commands from the master and send back responses.

Operation

  • Connection: As seen in the image, the Master is connected to the slaves via the SDA and SCL lines. All devices are also connected to ground lines (GND).
  • Addressing: Each slave on the I²C bus has a unique address which allows the master to communicate with each slave individually without any confusion.
  • Communication: The master sends a communication request by specifying the address of the slave it wants to communicate with. Once the slave is selected, the master can either send data to the slave or request data from it. Data is transferred bit by bit over the SDA line, while SCL paces the transmission.

Pull-up Resistors

Pull-up resistors on SDA and SCL are crucial for pulling the line to a high voltage level when no device is pulling the line low (low state). This ensures that the line is stable at a high level when no one is transmitting data.

I²C is highly favored for its simplicity and the ability to connect multiple devices over a single set of lines, making it economical and efficient for projects that require communication between various electronic components.

Boards used for I2C communicaion

For Networking Assignement I choose to used my final project Input board because to have a complete project my input and output boards have to communicate, and the most effective communication means i can use in this case is I2C protocol to garantee a fluid communication without interference.

For this application, i will make an LED blink once receving signal from my final project input board. In function of the character send by my input board the LED will turn on or turn off. There will two XIAO ESP32C3 boards communicate.

Input Board (Master)

Here is my input board :

Features
  • Two (02) I2C ports : These lines are connected to the sensor and are responsible for serial data transfer and clock synchronization.
  • Three Pins at the left side for connecting a specific sensor with s(signal), + and - pins;
  • A 5V regulation system to reduce the voltage from 12V to 5V : This area with a voltage regulator chip converts incoming voltage to a stable 5V output, necessary for powering the components on the board that require 5V.
  • A 12V power supply : This part of the circuit is likely responsible for handling higher voltage power supply, essential for powering the board and possibly other peripheral components.
  • A XIAO ESP32C3 to control all the boards components;

XIAO ESP32C3 Pinout Diagram

Output board (Slave)

Features
  • An LED : To show physically when which information is send by the input Board ;
  • A Seed XIAO ESP32C3 ;

XIAO ESP32-C3 pinout Diagram

Programming I2C Protocol communication

Testing the I2C communication

To program and establish communication between two XIAO ESP32C3 boards using the I2C protocol, you’ll need to follow several steps including hardware setup and software programming. Here’s a detailed guide:

Hardware Setup Connect the I2C Lines:

  • SDA (Serial Data Line): Connect the SDA pin of the XIAO RP2040 to the SDA pin of the XIAO ESP32C3.
  • SCL (Serial Clock Line): Connect the SCL pin of the XIAO RP2040 to the SCL pin of the XIAO ESP32C3.
  • Common Ground: Connect the GND (Ground) pin of the XIAO RP2040 to the GND pin of the XIAO ESP32C3 to establish a common ground.
  • Pull-up Resistors: Attach pull-up resistors (typically 1kΩ to 10kΩ) to both the SDA and SCL lines connected to a positive voltage supply (usually 3.3V), as the XIAO boards operate at 3.3V.

Software Setup

XIAO ESP32C3 Input board as Master

  • Programming Environment: Use the Arduino IDE or any compatible ESP32C3 development environment.
  • Library: Include the Wire.h library which supports I²C operations. A sample Code:
#include <Wire.h>

void setup() {
  Wire.begin(); // Initialize I2C as master
  Serial.begin(115200); // Start serial communication at 115200 baud rate
  Serial.println("ESP32C3 Master: Starting...");
}

void loop() {
  static bool ledState = false; // Keep track of LED state

  Wire.beginTransmission(9); // Begin transmission to I2C slave address 9
  if (ledState) {
    Wire.write('b'); // Send 'b' to turn off the LED
    Serial.println("Command sent: b"); // Print debug message
  } else {
    Wire.write('a'); // Send 'a' to turn on the LED
    Serial.println("Command sent: a"); // Print debug message
  }
  Wire.endTransmission(); // End the transmission

  ledState = !ledState; // Toggle LED state
  delay(2000); // Wait for 2 seconds before sending the next command
}

XIAO ESP32C3 OUTPUT Board as Slave

  • Programming Environment: Use the Arduino IDE or any compatible ESP32C3 development environment.
  • Library: Include the Wire.h library which supports I²C operations. A sample Code:
#include <Wire.h>

const int LED_PIN = D2;

void setup() {
  pinMode(LED_PIN, OUTPUT);      // Set LED pin as output
  Wire.begin(9);                 // Initialize I2C as slave with address 9
  Wire.onReceive(receiveEvent);  // Register the receive event
  Serial.begin(115200);          // Start serial communication at 115200 baud rate
  while (!Serial) {
    ;  // Wait for serial port to connect
  }
  Serial.println("ESP32C3 Slave: Serial initialized, waiting for commands...");
}

void loop() {
  delay(100);  // Add a small delay to avoid flooding the serial monitor
}

void receiveEvent(int howMany) {
  Serial.print("RP2040 Slave: Data received, number of bytes: ");
  Serial.println(howMany);
  while (Wire.available()) {
    char c = Wire.read();  // Read the received character
    Serial.print("RP2040 Slave: Command received: ");
    Serial.println(c);              // Print the received character
    if (c == 'a') {                 // If the received character is 'a'
      digitalWrite(LED_PIN, HIGH);  // Turn on the LED
      Serial.println("RP2040 Slave: LED turned ON");
    } else if (c == 'b') {         // If the received character is 'b'
      digitalWrite(LED_PIN, LOW);  // Turn off the LED
      Serial.println("RP2040 Slave: LED turned OFF");
    }
  }
}

Testing


Last update: July 15, 2024