DC motor control issues

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.

SCHEMATIC Please ?

Hi @lastchancename, I'm not at my workstation right now but I can give you a proper schematic tomorrow. For now, I'll just describe to you what I have if that helps.

My ESP32 is powered off a 9V adapter. The nRF24L01 is connected to the ESP32 in the regular, standard manner. It has a 100uF capacitor soldered between GND and VCC. Between the ESP32 and the motor driver, the following connections are made:

Pin 25 >>> PWMA
Pin 32>>>>AIN1
Pin 33>>>>AIN2
Pin 26>>>>STBY
5V>>>>VCC
GND>>>>GND
6V (4AA)>>>>VM
6V GND>>>>GND

A01 and A02 on the driver are connected to the motor.

Update you tomorrow with the schematic. Thanks.

Hi,
Are the grounds connected together?
e.g. 5v & 6v 0v¬0v
Good luck...........

Attached is my schematic on the receiver side. Fyi - I'm using the power supply module that came with my Mega kit. I put that on a breadboard - one side has a 5V rail and another has a 3.3 V rail. The grounds are connected between the two. I haven't shown the nRF24L01 connections or the transmitter - let me know if you want me to upload that as well.

Hi @notrauma, I believe so. That's the one thing that I've been checking in all of this, I think I've got all the grounds connected as one.

A little bit of debugging and I'm wondering now if it's the usual power issues with the nRF24L01. The servo response seems a little sluggish but the mapped values on the serial monitor seem fine. The mapped values that I see on the serial monitor when I move the joystick up and down on the Y-axis make me think that there's something going on with the RF. Attached screenshot is from the serial monitor. It's set to show mapped rudder values between 0 and 1023, then a "|" followed by mapped values for the throttle. While moving the joystick on the Y-axis, the motor responds normally while going back but then sputters towards the neutral position, continues to run and then eventually comes to a stop. Moving forward continues to make the motor behave very weirdly.

The capacitor is usually added to the 3.3 volts powering the NRF. Adding it to the VCC does nothing to to help the NRF supply as it is AFTER the regulator.

Sorry, I'll clarify. I have a capacitor between the VCC and GND pins of the nRF module. Before I did that, I had no links. Once I soldered on that capacitor after going through a few tutorials, I could successfully run basic nRF sketches.

I´m not following your reasoning in the map functions below:

I believe this line:

analogWrite(PWMA, map(throttle, 0, 462, 255, 0));

should be:

analogWrite(PWMA, map(throttle, 462, 0, 0, 255));

I wrote it like that because for me, going down to zero (reversing) while increasing speed made sense to have it laid out like that but the way you have it vs how I have it is the same thing, no? 462 maps to 0 and 0 maps to 255.

Yup, you're right, my bad.

1 Like

Update: The problem isn’t fixed but I’ve moved on from doubting the nRF24L01 to now having doubts about the motor maybe. So at some point today, while moving the joystick up and down on the Y-axis, I noticed that the motor was behaving perfectly as expected. The serial monitor also showed no signs of craziness and the expected analog values were seen on the receiver as I moved the joystick on the transmitter. The only thing different at this point was that I was holding the motor and its two wires a certain way and that seemed to do it. The moment I placed it down and picked it back up to try to replicate the issue, I was unsuccessful and the motor went back to behaving strangely.

Next, I took a multimeter and checked the voltage across the AO1 and AO2 pins as I moved the joystick. Got good readings there - +6V as I moved to the extreme forward position and -6V at the extreme reverse position. So the signals are fine. Tomorrow I’ll try a different motor and see how things go.

Your schematic shows you are powering the motor from the driver but have no capacitor cross the motor leads to limit the noise generated by the motor brushes. This is not good.

Thanks for that, @Paul_KD7HB. I admit I didn't have a capacitor across the motor leads but now I do (before you mentioned it, I was not aware of the noise issues, read up on it now and I understand). I've soldered on a 104 pF ceramic capacitor. The problem still persists. Without the motor connected, on the serial monitor, I'm seeing joystick analog values from the transmitter show up without issue on the receiver side. It's only the forward direction (joystick up) that keeps somehow inducing 0 analog values so the motor keeps sputtering trying to change direction while I want it to go the opposite way.

Hi, @theswaggyd

Can you please post some images of you project?
So we can see your component layout.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Sure, @TomGeorge. It is a little messy right now but I’m in the process of redoing my wires in a neat way. In the image, I’ve taken out of the servo and the servo power supply.


What are the raw outputs of the joystick before it gets to the transmitter? Maybe the system works, and the misinterpreted joystick outputs are doing as instructed.

When I look at the serial monitor on the transmitter side, I see the expected analog values from the joystick. I see no delay in transmission and no weird outputs. On the receiver side, the serial monitor shows me the same transmitted values coming in normally as you'd expect as long as the joystick isn't moving and the motor isn't being commanded to do stuff. The moment I move the joystick and the motor starts to move, I start observing a bunch of random values.

Disconnect the motor, move the joystick around and check the received values.
Reconnect the motor, put a know series of values (0 - 1023) through the transmitter.

Compare results.