I connected two motors with an L293N 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 with 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 anti-clockwise 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 in both directions when connected directly to the 12V battery.
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 of 0..255 for both joysticks and send that PWM signal to the driver.
analogWrite(6,255); // motor a key
digitalWrite(1,HIGH); // motor a
digitalWrite(2,LOW); // motor a
delay(1000);
analogWrite(6,0); // motor a key
digitalWrite(1,LOW); // motor a
digitalWrite(2,LOW); // motor a
delay(2000);
analogWrite(6,255); // motor a key
digitalWrite(1,LOW); // motor a
digitalWrite(2,HIGH); // motor a
delay(1000);
analogWrite(6,0); // motor a key
digitalWrite(1,LOW); // motor a
digitalWrite(2,LOW); // motor a
delay(2000);
Tried this piece of code on all my drivers removing the clever bit. What I found was that it rotated in only one direction. and didn't rotate the other way. But when I tried it with another channel (Out 1 and 2), it rotated both ways. So is there a problem with channels? How can it happen on 3 different drivers even though they are different models and were bought at different time from different retailers?!