Wireless robot control using the LoRa protocol.

LoRa robot project

The wireless robot control project using the LoRa protocol and a joystick is an interesting and practical project. In the following, we will explain the overall architecture and the related code for you.

Project Requirements:

  • LoRa Modules: Such as SX1278 or RA-02 (one for the transmitter and one for the receiver).
  • Joystick Controller: Standard modules like XY Joystick.
  • Microcontrollers: Examples include Arduino UNO or Arduino Nano (one for the transmitter and one for the receiver).
  • Robot Motors: For instance, two DC motors with a motor driver (e.g., L298N).
  • Battery: For power supply.

Project Functionality:

  • The joystick is connected to the transmitter microcontroller, which reads the X and Y axis movements and sends the data via LoRa.
  • The receiver microcontroller receives the data through LoRa and controls the robot’s motors based on the received data.

Transmitter Code (Joystick + LoRa):

#include <SPI.h>
#include <LoRa.h>

const int joystickX = A0; // X axis pin
const int joystickY = A1; // Y axis pin

void setup() {
Serial.begin(9600);

// LoRa settings
if (!LoRa.begin(433E6)) { // 433 MHz frequency setting
Serial.println(“LoRa init failed!”);
while (1);
}
Serial.println(“LoRa init OK!”);

pinMode(joystickX, INPUT);
pinMode(joystickY, INPUT);
}

void loop() {
int xValue = analogRead(joystickX); // Read the value of the X axis
int yValue = analogRead(joystickY); // Reading the value of the Y axis

//Send data to receiver
LoRa.beginPacket();
LoRa.print(xValue); // Send X value
LoRa.print(“,”);
LoRa.print(yValue); // Send Y value
LoRa.endPacket();

delay(100); // Delay between packets
}


Receiver Code (LoRa + Motor Control):

 

#include <SPI.h>
#include <LoRa.h>

const int motor1Pin1 = 5; // Motor pin 1
const int motor1Pin2 = 6;
const int motor2Pin1 = 9; // Motor pin 2
const int motor2Pin2 = 10;

void setup() {
Serial.begin(9600);

// LoRa settings
if (!LoRa.begin(433E6)) {
Serial.println(“LoRa init failed!”);
while (1);
}
Serial.println(“LoRa init OK!”);

//Setting the motor pins
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}

void loop() {
int xValue = 0, yValue = 0;

// Receive data
int packetSize = LoRa.parsePacket();
if (packetSize) {
String data = “”;
while (LoRa.available()) {
data += (char)LoRa.read();
}
Serial.println(data);

// Separate X and Y values
int commaIndex = data.indexOf(‘,’);
if (commaIndex > 0) {
xValue = data.substring(0, commaIndex).toInt();
yValue = data.substring(commaIndex + 1).toInt();
}

// Motion control based on X and Y values
if (yValue > 600) { // front
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
} else if (yValue < 400) { // back
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
} else if (xValue > 600) { // right
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
} else if (xValue < 400) { // left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
} else { // stop
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
}
}


Important Notes:

  • Use internal or external pull-up resistors for the joystick.
  • If the LoRa communication is unstable, reduce the distance between the transmitter and receiver or add a suitable antenna.
  • Add a message acknowledgment mechanism to handle errors.

However, note that this code does not implement in-place rotation for turning left or right. In other words, the robot cannot rotate in place by moving one wheel forward and the other backward. If you want to add this feature, you’ll need to modify the motor control section of the code slightly.

To add in-place rotation:
Instead of stopping one wheel, we can rotate one forward and the other backward. Add the following changes to the “motion control” section of the receiver code:

Modified Code for In-Place Rotation:

if (yValue > 600) { // front
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
} else if (yValue < 400) { //back
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
} else if (xValue > 600) { // In situ rotation to the right
digitalWrite(motor1Pin1, HIGH); // Left wheel forward
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW); // Right wheel backwards
digitalWrite(motor2Pin2, HIGH);
} else if (xValue < 400) { // In situ left turn
digitalWrite(motor1Pin1, LOW); //Left wheel backwards
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH); // Right wheel forward
digitalWrite(motor2Pin2, LOW);
} else { // stop
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}


Now, the movements will be as follows:
Forward: Both wheels forward.
Backward: Both wheels backward.
In-place rotation to the right: Left wheel forward, right wheel backward.
In-place rotation to the left: Right wheel forward, left wheel backward.
Stop: Both wheels off.


Important note:
If the robot’s motors are not moving correctly or the directions are reversed, check the motor wiring or the order of the motorPins. Also, for precise testing, check the X and Y joystick values using Serial.print in the transmitter.


Why did we use Arduino in this project?

The reason we use Arduino alongside LoRa modules in this project is that these modules are only responsible for transmitting and receiving data and cannot process the data or control other hardware components like joysticks, motors, etc., on their own.

More details:

LoRa Modules (e.g., SX1278 or RA-02):

  • Their only function is to send and receive raw data via wireless communication.
  • They do not have an internal processor to process the data or control hardware like joysticks or motors.

Arduino or Microcontrollers:

  • Responsible for reading data from the joystick on the transmitter side.
  • Converting the joystick’s analog data into digital values (like X and Y axes).
  • Sending this data to the LoRa module.
  • On the receiver side, it reads the received data and controls the motors accordingly.

Why don’t we use LoRa directly?

LoRa modules cannot, without the help of a microcontroller:

  • Interpret or manage data.
  • Read the joystick.
  • Control motors or drivers.

What if we remove Arduino?

  • We would need a microcontroller like STM32 or ESP32 that can handle these tasks.
  • However, for simple projects like this one, combining LoRa and Arduino is more cost-effective and convenient because:
    • LoRa modules generally don’t have processors.
    • Arduino is well-suited and efficient for simple processing and hardware control.

The LoRa module is only responsible for wireless communication, while Arduino acts as the brain of the system, managing everything. Without Arduino or a microcontroller, the system won’t function. 😊

Leave a Reply

Your email address will not be published. Required fields are marked *