Conflict between Servo/PWMServo and RF24

Hi,

First a bit of context :
I am building a RC boat model using the following components :

  • an Arduino Pro Mini 5V 16 MHz;
  • a nRF24L01 for the transceiver;
  • a Feetech FT90B servo to control the rudder;
  • a DRV8838 to control the propeller;
  • a dfplayer mini in order to add sound effects.

Before using the nRF24L01 I used a wireless PS2 gamepad (that also uses SPI to communicate with the Arduino) from Joy-It to control the boat and it worked flawlessly. I decided to replace the wireless gamepad because of its limited range.

Issue :
When I use the "attach" command from either the PWMServo library or the default Servo library, the nRF24L01 stop receiving reliably.

What I have already done :
The same sketch with only the "attach" command commented works.

At first I thought it might be a conflict with the attribution of pin D10 (my servo is, usually, attached to this pin in order to work with PWMServo) but it is not declared as the CSN for the NRF24 (I use A0 for this purpose) so it shouldn't be an issue.
Code :

#include <SPI.h>
#include <RF24.h>
#include <PWMServo.h>

//Pin definitions
#define ENABL_PIN     6
#define PHASE_PIN     5
#define SERVO_PIN     10

#define MAX_DEFLECT   60
#define NEUTRAL       90
#define DEADZONE      10

struct payload
{
  int joy_x;
  int joy_y;
  byte buttons;
};
payload data_to_receive;

PWMServo rudder;

uint8_t address[][6] = { "1Node", "2Node" };
RF24 radio(A1,A0);

int     motor_input = 0;
int     rudder_input = 90;

void setup()
{
  Serial.begin(9600);
  Serial.println(F("Let the show begin !"));
  
  if (!radio.begin())
  {
    Serial.println(F("radio hardware is not responding!!"));
    while (1) {}  // hold in infinite loop
  }
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address[1]);
  radio.openReadingPipe(1, address[0]);
  radio.startListening();
  
  //From datasheet : 500 µs = 0°, 2500 µs = 180°
  rudder.attach(SERVO_PIN, 500, 2500);
  
  //Set Motor
  pinMode(PHASE_PIN, OUTPUT);
  pinMode(ENABL_PIN, OUTPUT);
  
  //init data
  data_to_receive.joy_x = 512;
  data_to_receive.joy_y = 512;
  data_to_receive.buttons = 0;
}

void loop()
{ 
  uint8_t pipe;
  if(radio.available(&pipe))
  {
    radio.read(&data_to_receive, sizeof(payload));
    Serial.print(F("Message reads : "));
    Serial.println(data_to_receive.joy_y);
  }

  if(data_to_receive.joy_y<(511 - DEADZONE))
  {
    digitalWrite(PHASE_PIN, LOW);
    motor_input = (512 - data_to_receive.joy_y)/2;
  }
  else if(data_to_receive.joy_y>(511 + DEADZONE))
  {
    digitalWrite(PHASE_PIN, HIGH);
    motor_input = (data_to_receive.joy_y - 511)/2;
  }
  else
  {
    motor_input = 0;
  }
  motor_input = constrain(motor_input, 0, 255);
  analogWrite(ENABL_PIN, motor_input);
    
  rudder_input = map(data_to_receive.joy_x, 0, 1023, NEUTRAL-MAX_DEFLECT, NEUTRAL+MAX_DEFLECT);
  rudder.write(rudder_input);
}

Then I moved the servo to pin D7 using the Servo library instead of PWMServo without solving the issue (I got rid of SoftwareSerial and of the MP3 player to try this). The code below is the one from this last test :
EDIT : removed it because turns out Servo don't go well with RF24 after all even with the servo pin not attached.

I am a bit short on ideas on how to solve this issue and could really use an outside perspective. I thank you in advance for doing so.

I think I found the answer :smile:
There are three voltages in this setup :

  • the battery provides 9V;
  • a linear regulator converts the 9V from the battery into 5V;
  • the NRF24L01 comes with its own regulator that converts 5V in 3.3V.

The issue was that each time the Arduino sent a pulse to the servo (every 20 ms), a 1 Vpp ripple propagated on the 5V until it reached the NRF24L01. This messed up the communication somehow.

Now the thing is, the Arduino starts to send pulse to the servo as soon as it's attached to it (in order to send it to its neutral position I think). That's why it was working with the "attach" instruction commented.

In the end I added another voltage regulator in order to provide two 5V supply to the setup : one is for the NRF24L01 and one is for the MP3 player, the Arduino, the motor driver and the servo.

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