11. Embedded Networking and Communications
Here you can see what we did in our group assignment. Group Assignment
This week I wanted to create a bidirectional UART communication system between two Seeed Studio Xiao RP2040. One acts as a master, reading a potentiometer value, and the other as a slave, controlling a NEMA stepper motor using an A4988 driver. The slave can also send a command back to the master when a button is pressed.
My first idea was to comunicate via BLE between 2 Xiao C3 but unfortunetly one Xiao C3 burned during testing. So I had to use the Xiao RP2040 due to the lack of time. Overall I had never used UART to comunicate 2 Xiaos so still was a new experience for me.
System Overview
- Master: Reads a potentiometer and sends values to the slave via UART. Listens for button press messages from the slave.
- Slave: Receives speed values and controls the stepper motor. When a button is pressed, it notifies the master.
I had to use a protoboard because im doing the testing with the PCB I made for the CNC (Next week assignment) And I want to avoid any malfunction on the pcb as well as doing test on it
How to connect
- TX (Master) → RX (Slave)
- RX (Master) ← TX (Slave)
- GND ↔ GND (Must have common ground)
- Potentiometer → Master D0
- Button → Slave D7
- A4988 DIR (D2), STEP (D3)
Master Code
void setup() {
Serial1.begin(9600); // UART communication
}
void loop() {
int potValue = analogRead(A0);
int speedValue = map(potValue, 0, 1023, 500, 2000);
Serial1.println(speedValue);
if (Serial1.available()) {
String message = Serial1.readStringUntil('\n');
if (message == "BUTTON_PRESSED") {
Serial.println("Button was pressed on the slave!");
}
}
delay(100);
}
Slave Code (Xiao RP2040)
#define DIR_PIN 2
#define STEP_PIN 3
#define BUTTON_PIN 7
bool motorDirection = true;
int speedValue = 1000;
void setup() {
Serial1.begin(9600);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
if (Serial1.available()) {
speedValue = Serial1.parseInt();
}
static bool lastButtonState = HIGH;
bool currentButtonState = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && currentButtonState == LOW) {
Serial1.println("BUTTON_PRESSED");
motorDirection = !motorDirection;
digitalWrite(DIR_PIN, motorDirection);
}
lastButtonState = currentButtonState;
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(100);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(speedValue);
}
- Both devices comunicate at 9600 baud for UART.
- The button uses the internal pull-up and must be wired to GND.
- The A4988 driver must be powered at 12V and have common ground with the Xiao.
I had an issue with the code in which it wont allow me to recieve the information from the potenciometer I had an error in the code so I asked chatGPT to help me solve the issue.