Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Wireless Servo and DC Motor System

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Wireless Servo and DC Motor System

Circuit Documentation

Summary

This circuit is designed to control a DC motor and a servo motor using two Arduino boards (UNO and Nano) and wireless communication via NRF24L01 modules. The Arduino UNO acts as the receiver, controlling the motors based on the data received from the transmitter, which is the Arduino Nano. The Nano reads analog values from a dual-axis joystick module and sends these values wirelessly to the UNO. The UNO then maps these values to control the speed and direction of the DC motor using an L298N motor driver and the position of the servo motor.

Component List

Arduino UNO

NRF24L01 (Receiver)

DC Motor

L298N DC Motor Driver

Battery AAx4 6V

Servo

Arduino Nano

NRF24L01 (Transmitter)

KY-023 Dual Axis Joystick Module (x2)

Wiring Details

Arduino UNO

NRF24L01 (Receiver)

DC Motor

L298N DC Motor Driver

Battery AAx4 6V

Servo

Arduino Nano

NRF24L01 (Transmitter)

KY-023 Dual Axis Joystick Module (x2)

Documented Code

Arduino UNO Code (Receiver)

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>

Servo myservo;

// Set the CE and CSN pins
RF24 radio(9, 10);

// Create a pipe for communication
const byte address[6] = "00001"; 

int motorPin1 = 4;
int motorPin2 = 5; 
int mappedServoValue;
int mappedMotorValue;

// Structure to hold the received variables
struct DataPackage {
  int motorValue;
  int servoValue;
};

DataPackage data;

void setup() {
  Serial.begin(9600);
  myservo.attach(3);

  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);

  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  
  // Initialize the NRF24L01 module
  radio.begin();
  
  // Open the same pipe as the transmitter for reading
  radio.openReadingPipe(0, address);
  
  // Set the radio in receiver mode
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening(); // Start listening for data
}

void loop() {
  if (radio.available()) { // Check if data is available
    radio.read(&data, sizeof(data)); // Read the incoming data structure

    mappedMotorValue = map(data.motorValue,500,1023,0,255);
    if (data.motorValue > 510) {
      analogWrite(6,mappedMotorValue);
    }
    else {
      analogWrite(6,0);
    }

    mappedServoValue = map(data.servoValue,0,1023,180,0);
    if (mappedServoValue >= 0){
      myservo.write(mappedServoValue);
    }
    else {
      myservo.write(90);
    }

    // Print the received variables
    Serial.print("MOTOR: ");
    Serial.print(data.motorValue);
    Serial.print(" -> ");
    Serial.print(mappedMotorValue);
    Serial.print("   \tSERVO: ");
    Serial.print(data.servoValue);
    Serial.print(" -> ");
    Serial.println(mappedServoValue);
  }
}

Arduino Nano Code (Transmitter)

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

// Set the CE and CSN pins
RF24 radio(9, 10);

// Create a pipe for communication
const byte address[6] = "00001"; 

// Structure to hold the received variables
struct DataPackage {
  int motorValue;
  int servoValue;
};

DataPackage data;

#define MOTOR_PIN  A4 // Arduino pin connected to VRX pin
#define SERVO_PIN  A3 // Arduino pin connected to VRY pin

int xValue = 0; // To store value of the X axis
int yValue = 0; // To store value of the Y axis

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

  // Initialize the NRF24L01 module
  radio.begin();
  
  // Set the address to the pipe for writing
  radio.openWritingPipe(address);
  
  // Set the radio in transmitter mode
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  // read analog X and Y analog values
  data.motorValue = analogRead(MOTOR_PIN);
  data.servoValue = analogRead(SERVO_PIN);

  // Set module to transmit mode
  radio.stopListening();

  // Send the data structure
  bool success = radio.write(&data, sizeof(data));
  
  if (success) {
    Serial.print("[SENT]\t");
  } else {
    Serial.print("[NOT SENT]\t");
  }

  // print data to Serial Monitor on Arduino IDE
  Serial.print("MOTOR: ");
  Serial.print(data.motorValue);
  Serial.print("\tSERVO: ");
  Serial.println(data.servoValue);

  delay(100);
}

This documentation provides an overview of the circuit, including a description of each component, wiring details, and the embedded code for the microcontrollers. The code sections are documented to explain the functionality of the receiver and transmitter in the wireless control system.