Controlling Two Motors with L298N with a Joystick via Nrf24l01

Hello, i am working on a simple project that is a remote controlled car and i encountered with a problem. I am using two 6v 250 rpm motors, to control them a L298N motor driver, to power up the driver an 11.1v battery and nrf24l01 module to get information from remote controller. Arduino uno is connected to the power seperately. Now, the problem is:

when i use this code and get data from remote controller, left motor turns slower than the right motor. The code is unfinished and I just wanted to try only the positive Y axis of the joystick.

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

// radio
#define CE_PIN   7
#define CSN_PIN  8
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int joystick[3];
int mapX,mapY;
int SW_state=0;
int xPosition ;
int yPosition ;
// radio end

#define rightMotorSpeed 6
#define leftMotorSpeed 2
#define rightMotorFwd 5
#define rightMotorBwd 4
#define leftMotorFwd 1
#define leftMotorBwd 3
int actualX;
int actualY;

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

 pinMode(rightMotorSpeed, OUTPUT);
 pinMode(leftMotorSpeed, OUTPUT);
 pinMode(rightMotorFwd, OUTPUT);
 pinMode(rightMotorBwd, OUTPUT);
 pinMode(leftMotorFwd, OUTPUT);
 pinMode(leftMotorBwd, OUTPUT);




// radio
  radio.setPALevel(RF24_PA_MIN);
  radio.begin(); 
  radio.openReadingPipe(1, pipe);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
//radio end 

}

void loop() {
 // radio
 if ( radio.available())
  { 
  radio.read( joystick, sizeof(joystick) ); 
  int xpos=joystick[0];
  int ypos=joystick[1];
  int button = joystick[2];
  xPosition = analogRead(xpos);
  yPosition = analogRead(ypos);
  SW_state = digitalRead(button);
  mapX = map(joystick[0], 0, 1023, -512, 512);
  mapY = map(joystick[1], 0, 1023, -512, 512);
  Serial.print("RX: ");
  Serial.print(mapX);
  Serial.print(" | RY: ");
  Serial.print(mapY);
  Serial.print(" | Button: ");
  Serial.println(joystick[2]);
  //radio end 

  actualX = constrain(xpos,0,255);
  actualY = constrain(mapY,0,255); 

  if(mapY>= 50 ){ 
    forward();
    
  }

}


void forward(){  
  digitalWrite(rightMotorFwd,1);
  digitalWrite(rightMotorBwd,0);

  digitalWrite(leftMotorFwd,1);
  digitalWrite(leftMotorBwd,0);
  Serial.print(actualY);
  analogWrite(leftMotorSpeed,actualY);
  analogWrite(rightMotorSpeed,actualY);
  
}

On the other hand, when i use this code, two motors works at equal speed and fine.

#include <SPI.h>


#define rightMotorSpeed 6
#define leftMotorSpeed 2
#define rightMotorFwd 5
#define rightMotorBwd 4
#define leftMotorFwd 1
#define leftMotorBwd 3

void setup() {
  Serial.begin(9600);
 pinMode(rightMotorSpeed, OUTPUT);
 pinMode(leftMotorSpeed, OUTPUT);
 pinMode(rightMotorFwd, OUTPUT);
 pinMode(rightMotorBwd, OUTPUT);
 pinMode(rightMotorSpeed, OUTPUT);
 pinMode(leftMotorFwd, OUTPUT);
 pinMode(leftMotorBwd, OUTPUT);
}

void loop() {

  digitalWrite(rightMotorFwd,1);
  digitalWrite(rightMotorBwd,0);

  digitalWrite(leftMotorFwd,1);
  digitalWrite(leftMotorBwd,0);
 analogWrite(leftMotorSpeed,255);
 analogWrite(rightMotorSpeed,255);
}

No surprice that simple hobby motors are not presicely made. Use an adjusting factor to the pwm value.
11 volt power supplied for 6 volt motors is slightly high. Don't run them att full throttle for a long period of time.

Where did you get the original sketch?

As i said before it is not finished, i am changing many things and i deleted many things before opening this post. Obviously, i forget that part.

I found nrf functions from someone else who did similar project. The rest is mine.

Sorry. My mistake for noting it. I will stop.

#define leftMotorFwd 1

second sketch working flawless because no Serial output is made. better not involving pins that are for serial communication while it is in use. that pins are D0 and D1.

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

#define CE_PIN   9
#define CSN_PIN  8
#define rightMotorSpeed 7
#define leftMotorSpeed 6
#define rightMotorFwd 5
#define rightMotorBwd 4
#define leftMotorFwd 3
#define leftMotorBwd 2
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int joystick[3] = {512};

void setup() {
  pinMode(rightMotorSpeed, OUTPUT);
  pinMode(leftMotorSpeed, OUTPUT);
  pinMode(rightMotorFwd, OUTPUT);
  pinMode(rightMotorBwd, OUTPUT);
  pinMode(leftMotorFwd, OUTPUT);
  pinMode(leftMotorBwd, OUTPUT);

  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {

  if ( radio.available())
  {
    radio.read( &joystick, sizeof(joystick) );
    int actualX = joystick[0] >> 1 - 255; //turning
    int actualY = joystick[1] >> 1 - 255; //moving
    if (actualY > 15)forward();

  }

  void forward() {
    digitalWrite(rightMotorFwd, 1);
    digitalWrite(rightMotorBwd, 0);

    digitalWrite(leftMotorFwd, 1);
    digitalWrite(leftMotorBwd, 0);

    analogWrite(leftMotorSpeed, abs(actualY) + actualX < -15 ? actualX / 5 : 0); //
    analogWrite(rightMotorSpeed, abs(actualY) - actualX > 15 ? actualX / 5 : 0);
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.