Hello,
I have run into a strange issue and I've spent 2 days trying to figure out what is wrong but alas, I have got nothing. My setup is quite simple for my RC boat. I have a 3-6 V DC motor that I want to control using a joystick via nRF24L01. Till last week, I had an L298N in my setup and everything worked great. This week, I decided to try out the TB6612FNG. I have good comms over RF and I'm using the serial monitor to debug issues. When I press the joystick down, the motor reacts as it is supposed to. Moving the joystick in the X-axis moves my servo as well. However, when I move my joystick up, it appears that the motor keeps wanting to push back on the rotation direction and keeps switching directions. If I press the joystick up all the way, the motor keeps sputtering and eventually continues to spin at the highest speed while fully cutting off my RF link. I've followed Robin2's post regarding the nRF24L01 modules, I've soldered a capacitor on there and everything works overall. It's just the one direction that the motor just won't rotate in. I'm hoping this is a super simple issue where I've overlooked something small. Also for reference, my transmitter is an Uno, my receiver is an ESP32 and I'm using 4 AA batteries as external power for the motor.
// Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001"; // Address for communication
int rudder;
int throttle;
uint16_t joystick[2];
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
rudder = analogRead(A0);
throttle = 1023 - analogRead(A1); // Personal preference to read this way
joystick[0] = rudder;
joystick[1] = throttle;
// radio.write(&throttle, sizeof(throttle)); // Send motor speed to receiver
radio.write(&joystick, sizeof(joystick)); // Send rudder commands to receiver
Serial.print(rudder);
Serial.print("|");
// Serial.print(rudder_angle);
// Serial.print("|");
Serial.print(throttle);
// Serial.print("|");
// Serial.print(motorSpeed);
Serial.println();
delay(50);
}
// Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <ESP32Servo.h>
RF24 radio(4, 5); // CE, CSN
const byte address[6] = "00001"; // Address for communication
// TB6612FNG Motor Driver Pins
const int IN1 = 32; // Motor direction pin 1
const int IN2 = 33; // Motor direction pin 2
const int PWMA = 25; // Motor speed control (PWM)
const int STBY = 26; // Motor stby pin
const int servo1_pin = 14; // Servo pin
Servo servo1;
uint16_t joystick[2];
int throttle;
int rudder;
int servo_angle;
// uint16_t joystick[0] = rudder;
// uint16_t joystick[1] = throttle;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(STBY, OUTPUT);
servo1.attach(servo1_pin);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
int rudder = joystick[0];
int throttle = joystick[1];
radio.read(&joystick, sizeof(joystick)); // Receive motor speed
Serial.print(rudder); Serial.print("|"); Serial.print(throttle); Serial.println();
if (throttle > 562) { // Forward; 50 added as deadzone to center position of 512 for stability
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, map(throttle, 562, 1023, 0, 255));
} else if (throttle < 462) { // Reverse
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(STBY, HIGH);
analogWrite(PWMA, map(throttle, 0, 462, 255, 0));
} else { // Stop
// digitalWrite(IN1, LOW);
// digitalWrite(IN2, LOW);
digitalWrite(STBY, LOW);
// analogWrite(PWMA, 0);
}
servo_angle = map(rudder, 0, 1023, 0, 180);
servo1.write(servo_angle);
}
}
I've tested this out with an L293D as well but I still get the same behavior. As always, I would appreciate any and all advice before I go fully insane. Thank you very much!
Edit: forgot to add earlier - the motor will also spin on its own when the joystick is at rest. At times, it’ll rev up and down while there should be no PWM signal going in when the joystick is in the neutral position.