RF-PWM Communication System for Robotic Applications

Controlling robots remotely is a common requirement in many applications, from industrial automation to DIY robotics projects. This blog post presents an innovative RF-PWM Communication System for robotic applications that leverages Radio Frequency (RF) communication and Pulse Width Modulation (PWM) to control a robot’s movement. We'll use Arduino to implement both the transmitter and receiver circuits and code, allowing for easy and effective wireless control.

Why Use RF-PWM for Robotic Control?

RF communication provides a robust, long-range, and low-power method for transmitting commands wirelessly. When combined with PWM, which is widely used to control motor speed and direction, we get a powerful system for controlling robotic movements wirelessly.

Components Needed

  • Transmitter Side:

    • Arduino Nano or Uno
    • RF Transmitter Module (e.g., 433 MHz Transmitter)
    • Jumper Wires
  • Receiver Side:

    • Arduino Nano or Uno
    • RF Receiver Module (e.g., 433 MHz Receiver)
    • L298N Motor Driver Module
    • DC Motors
    • Power Supply for Motors
    • Jumper Wires

Circuit Diagrams

The following shows the connection for the RF PWM control of dc motors of remotely Arduino controlled RC car acting as our robot.
 

 

Transmitter Circuit:

  1. Connect the RF Transmitter Module:

    • DATA pin of the RF transmitter module to digital pin 12 on the Arduino.
    • VCC to 5V and GND to GND on the Arduino.
  2. Connect the Arduino to your PC via USB to use the Serial Monitor to send commands.

Receiver Circuit:

  1. Connect the RF Receiver Module:

    • DATA pin of the RF receiver module to digital pin 11 on the Arduino.
    • VCC to 5V and GND to GND on the Arduino.
  2. Motor Driver Module Connections (L298N):

    • Connect IN1, IN2, IN3, IN4, ENA and ENB to Arduino digital pins 2,3, 4, 5, 9 and 10 respectively.
    • Connect OUT1 and OUT2 to the terminals of Motor 1, and OUT3 and OUT4 to the terminals of Motor 2.
    • Connect the motor power supply to the L298N VCC and GND.
     
  3. For other connection see the circuit diagram above. Don't forget to connect the ground of the shield and the arduino.

Arduino Code for the Transmitter

This code sends commands from the Serial Monitor via RF to control the robot.

#include <RH_ASK.h> // Include the RadioHead ASK library
#include <SPI.h> // Include SPI library (not used directly, but required for RadioHead)

RH_ASK rf_driver; // Create an instance of the RF driver

void setup() {
Serial.begin(9600); // Start the Serial Monitor
Serial.println("RF Transmitter Ready. Enter commands (f, b, l, r, s):");

rf_driver.init(); // Initialize the RF driver
}

void loop() {
if (Serial.available() > 0) { // Check if data is available from the Serial Monitor
char command = Serial.read(); // Read the input command

// Convert the command to a string to send via RF
char msg[2];
msg[0] = command;
msg[1] = '\0'; // Null-terminated string

rf_driver.send((uint8_t *)msg, strlen(msg)); // Send the command via RF
rf_driver.waitPacketSent(); // Wait until the message is sent

Serial.print("Sent command: ");
Serial.println(command); // Print the sent command for confirmation
}
}

Arduino Code for the Receiver

This code receives RF commands and controls the robot's motors using PWM signals.

#define ENA 9 // Enable/speed motors Right (PWM pin)
#define ENB 10 // Enable/speed motors Left (PWM pin)
#define IN_1 2 // L298N in1 motors Right
#define IN_2 3 // L298N in2 motors Right
#define IN_3 4 // L298N in3 motors Left
#define IN_4 5 // L298N in4 motors Left

#include <RH_ASK.h>
#include <SPI.h> // Include SPI library (not used directly, but required for RadioHead)

// Create an instance of the RF driver
RH_ASK rf_driver(2000, 11, 255); // Initialize RF driver with custom data pin

String command; // String to store command state.
int speedCar = 800; // Speed of the car (0-1023).
int speed_Coeff = 3;

void setup() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);

Serial.begin(9600);

// Initialize RF driver
if (!rf_driver.init()) {
Serial.println("RF Driver Initialization Failed");
}
}

void loop() {
if (rf_driver.available()) {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);

if (rf_driver.recv(buf, &len)) {
buf[len] = '\0'; // Null-terminate the received message
command = (char *)buf; // Convert buffer to string

// Execute command
if (command == "f") goAhead();
else if (command == "b") goBack();
else if (command == "l") goLeft();
else if (command == "r") goRight();
else if (command == "i") goAheadRight();
else if (command == "g") goAheadLeft();
else if (command == "j") goBackRight();
else if (command == "h") goBackLeft();
else if (command == "0") speedCar = 400;
else if (command == "1") speedCar = 470;
else if (command == "2") speedCar = 540;
else if (command == "3") speedCar = 610;
else if (command == "4") speedCar = 680;
else if (command == "5") speedCar = 750;
else if (command == "6") speedCar = 820;
else if (command == "7") speedCar = 890;
else if (command == "8") speedCar = 960;
else if (command == "9") speedCar = 1023;
else if (command == "s") stopRobot();
}
}
}

void goAhead() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}

void goBack() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

void goRight() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}

void goLeft() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

void goAheadRight() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar / speed_Coeff);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}

void goAheadLeft() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar / speed_Coeff);
}

void goBackRight() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar / speed_Coeff);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

void goBackLeft() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar / speed_Coeff);
}

void stopRobot() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

How the System Works

  1. Transmitter Side:

    • Commands are entered via the Serial Monitor (f for forward, b for backward, l for left, r for right, s for stop and others). This is how the motor speed and direction control with L298N motor shield works.
    • The transmitter Arduino reads the command and sends it as an RF signal using the RH_ASK library.
  2. Receiver Side:

    • The receiver Arduino receives the RF signal, decodes the command, and controls the motors accordingly.
    • Motor speed and direction are controlled using PWM signals generated by the Arduino based on the received command.

Conclusion

This RF-PWM communication system provides a simple yet powerful way to control a robot wirelessly using RF signals and PWM motor control. The system is suitable for various robotic applications, such as remote-controlled cars, robotic arms, and other projects where wireless control is desired. By understanding and applying the concepts in this project, you can expand it to more complex robotic systems with additional sensors and control features.

Further Reading

For more details on related topics, you can explore these resources:

These articles provide further insights into frequency counting, PWM applications, and DC motor control with Arduino, enhancing your understanding and skills in building such systems.

Post a Comment

Previous Post Next Post