SF Motor driver prevents any further communication with NRF24L01 or code function

I have read everything I can find, and tried to correct this myself for longer then I'd like to admit so any help is greatly appreciated. I am using two NRF24L01 modules with the objective of controlling a small robot car with two motors. The NRF modules communicate fine. I have confirmed this many times via the monitor. The issue here, is that upon receiving the instruction to call upon the 'move' function to activate the motors, the motors do activate, however following that, communication is lost with the NRF module and the program essentially freezes in that state (the motors keep running and it seems to keep looping). I have tried both switch case and if statements, but it seems to happen either way. I have also used this same 'move' function in the past for a bluetooth controlled robot, and it works fine. Therefore I assume this must have something to do with the NRF module, however I am at a loss. I have duplicated this build also on a bread board and the same communication loss/freeze occurs (not a soldering issue). The motor controller is a sparkfun tb6612fng, connected to an aftermarket arduino nano, 9v power supply. The Transmitter sends a single character, in this case the number '1' to call the 'move' function. In the below code, once the '1' is received, the motors begin to run, and then I continue to get the "No Data" line from the getData function, repeatedly with no ability to stop the program.

Please let me know if I have missed anything important. Thanks in advance for any assistance.

Receiver Code:

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

#define CE_PIN 9
#define CSN_PIN 10

// NRF Details

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
bool newData = false;

// Integers for motor commands

int STBY = 8; //standby
char command;
//Motor A
int PWMA = 5; //Speed control
int AIN1 = 6; //Direction
int AIN2 = 7; //Direction
//Motor B
int PWMB = 3; //Speed control
int BIN1 = 2; //Direction
int BIN2 = 4; //Direction

void setup() {

  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();

  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

void loop() {
  //stop();
  getData();
  if (newData == true ) {
    newData = false;
    switch (command) {
      case '1':
        move(1, 255, 0); //motor 1, full speed, left
        move(2, 255, 1); //motor 2, full speed, right
        Serial.println("Forward Movement");
        break;
    }
  }
}

void getData() {
  if ( radio.available() ) {
    char dataReceived;
    radio.read( &dataReceived, sizeof(dataReceived) );
    command = dataReceived;
    newData = true;
    Serial.print("Data received ");
    Serial.println(dataReceived);
  }
  else {
    Serial.print("No Data");
  }
}

void move(int motor, int speed, int direction) {
  //Move specific motor at speed and direction
  //motor: 0 for B 1 for A
  //speed: 0 is off, and 255 is full speed
  //direction: 0 clockwise, 1 counter-clockwise

  digitalWrite(STBY, HIGH); //disable standby

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if (direction == 1) {
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if (motor == 1) {
    digitalWrite(AIN1, inPin1);
    digitalWrite(AIN2, inPin2);
    analogWrite(PWMA, speed);
  } else {
    digitalWrite(BIN1, inPin1);
    digitalWrite(BIN2, inPin2);
    analogWrite(PWMB, speed);
  }
}

void stop() {
  digitalWrite(STBY, LOW);
}

ignoring the fact that i see no code to do anything but turn the motors on, are you saying you don't see any further "Data received:"?

Hi gcjr, the code is stripped down to help determine the problem. Following the 'move' function turning the motors on, the program continues to loop and indicates 'No Data' from the getData function continuously. The motors are running, with no ability to turn them off. When turned on, the serial monitor confirms data received from the transmitter as 0, and continues to loop and do nothing. Once the serial monitor shows Data Received: '1', the switch case properly changes to case 1, the motors begin to move, the serial monitor displays 'Forward Movement', then immediately following that a continuous loop of 'No Data'. Somehow, whenever the motor driver is activated, the NRF module seems to permanently loose function, until reset where it works perfectly again. Thanks again for your help.

could it be RF interference from the motor.

Does it work without the motors, have you tried different motors, what kind of motors, have you considered RF interference or transients...
:slight_smile: sry didnt see gcjr, my thoughts exactly

I'm not sure, currently the RF module is 3-4 inches away from the motors and arduino. So unless there is feedback in the power supply? I will check.

As long as there are no glaring code mistakes that you can see, I will continue to work away at this. I just assumed I was missing some obvious coding no no.

I will remove the motors and see if communication can be maintained. The motors are small 300rpm micro motors from amazon.

try just measuring outputs without motors connected, if it works consider a snubber or a better supply

i was going to suggest using aluminum foil to shield the motor, but the interference may travel across the leads without some toroid to possibly block it

could solder a small cap across the motor terminals

small cap can lower collector arc, or snub, or a diode if its unipolar dc

DC motors with brushes ALWAYS need capacitors across the motor terminals to limit RF radiation (noise) from the sparking brushes.

what happens is capacitor conducts very fast self induction current transients, of the opposite direction when the brushes change polarity, and uses the energy to rearrange its electrons. If the rotor coil has nowhere to discharge the magnetic energy, it produces a high voltage spark on the brushes that radiates, inducing stray currents all over the place, and erodes the collector over time...

Similar transient can also fry the supply electronics on turnoff, due to the stator coil, the driver should be good with anti-parallel diodes inside, but it can then travel and oscillate the supply voltage, that's why it is very good to snub it on the motor side

I will solder on a cap to each motor this evening. I'll use a 10uF 25v cap and see how it goes, with no change I'll jump up to 100uF @ 25v, and see if there is any change. Thanks all!

you should use a 0.1 range ceramic probably too, or a shotky diode, if the polarity doesn't change, or all, as it can "short out" very fast transients faster
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
found this by Grumpy Mike

Hi all, thanks for all of your comments. Unfortunately... She's still dead Jim. I tried both caps 10uF and 100uF no change. Then I removed both motors and installed two LEDs w resistors. So basically the same thing happens. Now the serial monitor confirms communication between transmitter and receiver, showing a '0'. Then when the '1' char is sent to the receiver, both LEDs turn on, 'Forward Motion' is indicated in the serial monitor, and then 'NoData' repeats with no ability to switch case following this. What the actual heck? I know its not a hardware issue as I rebuilt this with completely separate components and the same issue occurred. Is it possible that I require a specific library for the Spark Fun Motor Driver? Its the tb6612fng. However, note that I have used it successfully with other robots without any library called. I'm at a loss! Any continued help is appreciated.

those are much too large. did you try 0.1uF?

No I didnt. However, even w the LEDs the same loss of communication w the NRF module occurs. Its so odd, I reviewed the serial monitor after installing the LEDs and it seems to have run through the loop 3 times properly before losing communication. Indicating "forward motion" 3 times. Ill test this a few more times because if this is the case it must be a hardware issue. Iv also ordered a different motor controller. Fingers crossed that works.

I can't believe it. Soldered a 10uF cap directly to the NRF module and it works perfectly.

Thanks everyone.

1 Like

you know, its still a good idea to get that 0.1 ceramic cap across the motor XD
Anyways congrats on your new driver :slight_smile:

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