1

I connected 2 motors with an L293Nn driver. When I control it with my joystick, one motor works fine (Out 1 and 2 of driver.) It rotates in one direction and the other very smoothly.

The problem occurs in the second motor (Out 3 and 4 of driver.) It rotates smoothly in one direction (clockwise) but when rotated in the other(anti-clockwise), the 4000 RPM motor rotates at around 60 RPM. My voltmeter shows a reading of 1V. When I reverse the polarity at the driver, it rotates slowly in the clockwise direction and at proper speed in the anticlockwise direction.

I tried it with another L298N driver and had the same problem. I tried with an L293D driver and still had the same error. The code is the same for both drivers. The motors work fine when just connected to the 12V battery (both directions.)

I do not understand the problem.

My code:

struct Signal{
  uint16_t y1; // Joystick-1 Y 0 to 1023
  uint16_t y2; // Joystick-2 Y 0 to 1023
};
int x1 = 0; // Joystick-1 Y 0 to 255
int x2 = 0; // Joystick-2 Y 0 to 255
Signal data;
#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
RF24 radio(7,8);
const byte address[6] = "HexaT";
void setup() {
  Serial.begin(9600);
  pinMode(1, OUTPUT); // motor b
  pinMode(2,OUTPUT); // motor b
  pinMode(6,OUTPUT); // motor b key
  
  pinMode(5,OUTPUT); // motor a key
  pinMode(9, OUTPUT); // motor a
  pinMode(10,OUTPUT); // motor a
  
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();

}

void loop() {
  while(radio.available()){
    radio.read(&data, sizeof(Signal));
        

//motor A
    if(data.y1 > 530){
      x1 = map(data.y1,530,1023, 0, 255);
      analogWrite(5,x1); // motor a key
      digitalWrite(9,LOW); // motor a
      digitalWrite(10,HIGH); // motor a
    }else if(data.y1 < 500){
      x1 = map(data.y1,500,0, 0, 255);
      analogWrite(5,x1); // motor a key
      digitalWrite(9,HIGH); // motor a
      digitalWrite(10,LOW); // motor a
    }else{
      digitalWrite(5,LOW); // motor a key
      digitalWrite(9,LOW); // motor a
      digitalWrite(10,LOW); // motor a
    };

//Motor B    
    if(data.y2 > 530){
      x2 = map(data.y2,530,1023, 0, 255);
      analogWrite(6,x2); // motor b key
      digitalWrite(1,LOW); // motor b
      digitalWrite(2,HIGH); // motor b
    }else if(data.y2 < 500){
      x2 = map(data.y2,500,0, 0, 255);
      analogWrite(6,x2); // motor b key
      digitalWrite(1,HIGH); // motor b
      digitalWrite(2,LOW); // motor b
    }else{
      digitalWrite(6,LOW); // motor b key
      digitalWrite(1,LOW); // motor b
      digitalWrite(2,LOW); // motor b
    };

    Serial.println(x1);
    Serial.println(x2);
  }
}

In the serial monitor, I get proper values from 0 to 255 for both the joysticks and send that PWM signal to the driver.

1
  • 1
    I can't reproduce it now but may I suggest you to don't use PIN 1 as output while using hardware Serial? It uses PIN 1 to transmit and this could cause interference with motor (it should do the same in both directions....)
    – Marco Cogo
    Mar 16, 2022 at 15:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.