Skip to content

13. Networking and communications

Group assignment:

  • Send a message between two projects

To see our group assignment click here

Individual assignment:

  • Design, build and connect wired and wireless nodes with network or bus addresses and a local interface

For this week’s assignment, we’ll be communicating the board we designed in week 4 with an arduino nano board to perform different types of communication. We’ll be using the SPI, I2C and MQTT protocols respectively.

SPI protocol

SPI (Serial Peripheral Interface) is a synchronous communication protocol used to transfer data between electronic devices, usually on a printed circuit board or between several boards.

SPI generally uses four wires for communication:

  • MOSI (Master Out Slave In): Data line from master to slave.
  • MISO (Master In Slave Out): Data line from slave to master.
  • SCLK (Serial Clock): Clock synchronizing data transmission.
  • SS (Slave Select): Line used to select the slave with which the master communicates.

SPI protocol schematic diagram

The figure below shows a simplified schematic diagram of an SPI data link, with its main components.

Two shift registers are generally used, either hardware or software, depending on the devices used. For example, the RasP implements its shift register in software, while the MCP3008 uses a hardware shift register.

Example use of SPI protocol

In this example, we’ll use the SPI protocol to communicate with a xiao rp2040 and a nano.

In the following images, we can see the pins that will help us circled in red.

To establish SPI communication between an XIAO RP2040 and an Arduino Nano, follow these steps:

XIAO RP2040 side (SPI master):

  • Connect the SPI pins on the XIAO RP2040 (MOSI, MISO, SCK, and SS) to the corresponding ones on the Arduino Nano.

  • Make sure that the voltages used are compatible between the two devices (5V for the Arduino Nano and 3.3V for the XIAO RP2040).

  • Initialize SPI communication on the XIAO RP2040 as master.

  • Write the code to send data via SPI.

Arduino Nano side (SPI slave):

  • Connect the SPI pins on the Arduino Nano (MISO, MOSI, SCK, and SS) to the corresponding pins on the XIAO RP2040.

  • Make sure that the voltages used are compatible between the two devices.

  • Initialize SPI communication on the Arduino Nano as a slave.

  • Write the code to receive data via SPI.

Here’s an example of the code for each side:

Code for XIAO RP2040 (SPI Master) :

import machine
import time

# Configuration des broches SPI
spi = machine.SPI(0, baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(2), mosi=machine.Pin(3), miso=machine.Pin(4))
ss = machine.Pin(5, machine.Pin.OUT)

# Fonction pour envoyer des données via SPI
def send_spi(data):
    ss.value(0) # Activer la ligne SS
    spi.write(bytes([data])) # Envoyer les données
    ss.value(1) # Désactiver la ligne SS

# Exemple d'utilisation
while True:
    send_spi(0xAA) # Envoyer des données (par exemple, 0xAA)
    time.sleep(1)

Code for Arduino Nano (SPI slave) :

#include <SPI.h>

#define SS_PIN 10 // Broche SS pour la communication SPI

void setup() {
  SPI.begin(); // Initialiser la communication SPI
  pinMode(SS_PIN, INPUT); // Définir la broche SS comme entrée (mode esclave)
  SPI.attachInterrupt(); // Activer les interruptions SPI
  Serial.begin(9600); // Initialiser la communication série pour le débogage
}

void loop() {
  // Vérifier si des données SPI sont disponibles
  if (SPI.transfer(0) != 0xFF) {
    // Lire les données SPI
    byte receivedData = SPI.transfer(0x00);
    // Afficher les données reçues via la communication série
    Serial.print("Donnees recues: ");
    Serial.println(receivedData, HEX);
  }
}
In this example:

  • The XIAO RP2040 periodically sends data via SPI.

  • The Arduino Nano is configured as an SPI slave and reads the data sent by the XIAO RP2040.

SPI protocol project Code

Code SPI Master

Code SPI Slave


I2C protocol

The I2C (Inter-Integrated Circuit) protocol is a synchronous serial communication protocol that enables several electronic devices to communicate with each other over a single two-wire data bus. It was developed by Philips (now NXP Semiconductors) in the 1980s.

The I2C bus uses two communication lines:

  • SDA (Serial Data Line): This line is used for bidirectional data transmission between connected devices. Data is transmitted serially on this line.

  • SCL (Serial Clock Line): This line is used to synchronize communication between devices. The signal on this line tells the peripherals when to read or write data on the SDA line.

The important features of the I2C protocol are as follows

  • Master/Slave: The I2C bus uses a master/slave scheme, where a master device controls the bus and initiates transactions, while slave devices respond to commands from the master.

  • Device addresses: Each device connected to the I2C bus has a unique address, usually 7 bits long. This address is used by the master to select the device with which it wishes to communicate.

  • Synchronous communication: Communication on the I2C bus is synchronous, meaning that it is synchronized by a common clock (the SCL line). Data is transmitted bit by bit, with the clock defining the transmission rate.

  • Start/Stop: Data transmission on the I2C bus begins with a start signal sent by the master, followed by the device address and transmission mode (read or write). The transaction ends with a stop signal.

Example use of I2C protocol

In this example, we will establish I2C communication between a XIAO RP2040 and an OLED display.

Oled pinout

Hardware connection

  • Connect the SDA (Data) pin of the XIAO RP2040 to the SDA pin of the OLED display.

  • Connect the SCL (Clock) pin of the XIAO RP2040 to the SCL pin of the OLED display.

  • Make sure that the voltages used are compatible between the two devices (3.3V for the XIAO RP2040 and 3.3V or 5V for the OLED display, depending on your display’s specifications).

the following diagram will help you

Download the following code

from ssd1306 import SSD1306_I2C
from machine import Pin, I2C
from time import sleep

i2c = I2C(1, scl=Pin(7), sda=Pin(6), freq=200000)#Grove - OLED Display 0.96" (SSD1315)
oled = SSD1306_I2C(128, 64, i2c)

while True:  
    oled.fill(0)#clear
    oled.text("Salut! moi c_est sephora!",0,0)
    oled.show()
    #sleep(0.5)

I2C protocol project Code

Code


END


Last update: May 14, 2024