Accelstepper and RF24 compatibility

I am having trouble making those two libraries work together. I've been browsing the internet for an hour now and still can't find anyone with similar trouble. And since I haven't really worked with either of those libraries I can't find the problem myself.

Basically, when I divide the code into two parts with one library each(Accelstepper, Rf24) everything works just fine, but as soon as I try to combine those two my radio module doesn't pick up any signal. In the main code you can see I have only defined the stepper motor pins and that was enough for the NRF24l01 module to stop working.

//Libraries
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
#include <AccelStepper.h>
//

//Code variables
const int stepPin = 12;
const int dirPin = 14;
const int stepPin1 = 10;
const int dirPin1 = 8;
int del;

int motorSpeed1;
int motorSpeed2;
//

//Stepper motors setup
AccelStepper leftWheel(1, stepPin, dirPin);
AccelStepper rightWheel(1, stepPin1, dirPin1);
//

//Radio module setup and variables
RF24 radio(53, 49);
const byte address = "00001";
byte data[14];
//


void setup()
{
  Serial.begin(9600);

  pinMode(2, OUTPUT);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();

  pinMode(47, OUTPUT);
}

void loop()
{
  if (radio.available())
  {
    radio.read(&data, sizeof(data));
    digitalWrite(47, HIGH);
  }
  for (int x = 0; x < 14; x++)
  {
    Serial.print(data[x]);
    Serial.print(",");
  }
  Serial.println();

}

Here is the transmitter code. I don't know if it will help you troubleshoot my code though.

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

RF24 radio(53, 49);
const byte address = "00001";
byte data[14];

void setup()
{
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();
}

void loop()
{
  if (digitalRead(1) == 1)
  {
    data[0] = 0;
  }
  else {
    data[0] = 1;
  }

  if (digitalRead(2) == 1)
  {
    data[1] = 0;
  }
  else {
    data[1] = 1;
  }

  data[2] = digitalRead(3);
  data[3] = digitalRead(4);
  data[4] = digitalRead(5);
  data[5] = digitalRead(6);
  data[6] = digitalRead(7);
  data[7] = digitalRead(8);

  data[8] = map(analogRead(0), 0, 1023, 0, 255);
  data[9] = map(analogRead(1), 0, 1023, 0, 255);
  data[10] = map(analogRead(2), 0, 1023, 0, 255);
  data[11] = map(analogRead(3), 0, 1023, 0, 255);
  data[12] = map(analogRead(4), 0, 1023, 0, 255);
  data[13] = map(analogRead(5), 0, 1023, 0, 255);

  radio.write(&data, sizeof(data));

}

Thank you in advance for your responses.

I have used the AccelStepper and rf24 libraries together before with no problems.

What Arduino board are you using?

Please post a schematic of the transmitter and receiver.

Many members, including myself, will not download code. It is too much trouble. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Thank you informing me about that. I have now adjusted it so that you don't have to download it.

Nicely done. Thank you.

What Arduino board, please?

Arduino mini mega.
This one: https://aliexpi.com/ycd5

Also about the schematic that you asked for. The problem isn't there since everything works just fine separately. It causes troubles when I try to combine those two libraries

Is the rf24 wired to pins 50, 51, 52 (MISO, MOSI, SCK)?

The transmitter code does not send anything.

Yes, it is. As I said. It works without the Accelstepper just fine. This means that if I delete the Accelstepper library and the lines where I define the motors it works. Also if I delete the rf24 library and write the code for the stepper motor to move in the loop section its works as well.

Oh sorry, i uploaded the wrong sketch I fixed it.

I have a Mega Pro and rf24 radios. So I connected a rf24 to my Mega Pro according to the pin usage in your code and loaded your code. I made a couple changes to your code. I put the printing of the incoming data inside of the if radio available if block so that printing only happens if there is radio data and made the sender send every 1 second instead of continuously. I use an Uno as a sender. I am able to duplicate what you are seeing. If I include the AccelStepper library without creating any AccelStepper objects the code works fine. Construct even one instance of a stepper and it no longer receives data. I do not know why.

So then I took one of my rf24 test programs and modified the receive code to work on the Mega Pro. I use an Uno to send data. I included your Accelstepper code into my know to work test receiver code and it works fine. I do not know why it works with my code and not yours. Just for information, here is my code. It is derived from code from Robin2's simple rf24 tutorial.

Transmit code:


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


const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct LdrValues
{
  int ldr_1;
  int ldr_2; 
}ldrValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{
   Serial.begin(115200);
   Serial.println("SimpleTx Starting");
   pinMode(LDR1, INPUT_PULLUP);
   pinMode(LDR2, INPUT_PULLUP);
   
   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.setRetries(3, 5); // delay, count
   radio.openWritingPipe(slaveAddress);
}

void loop()
{
   currentMillis = millis();
   if (currentMillis - prevMillis >= txIntervalMillis)
   {
      send();
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      prevMillis = millis();
   }
}

//====================

void send()
{
   ldrValues.ldr_1 = analogRead(LDR1);
   ldrValues.ldr_2 = analogRead(LDR2);
   radio.write( &ldrValues, sizeof(ldrValues) );
}

Receive code:



// SimpleRx - the slave or the receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <AccelStepper.h>

const int stepPin = 12;
const int dirPin = 14;
const int stepPin1 = 10;
const int dirPin1 = 8;
int del;

int motorSpeed1;
int motorSpeed2;
//

//Stepper motors setup
AccelStepper leftWheel(1, stepPin, dirPin);
AccelStepper rightWheel(1, stepPin1, dirPin1);

const byte CE_PIN = 53;
const byte CSN_PIN = 49;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct LdrValues
{
   int ldr_1;
   int ldr_2;
} ldrValues;

bool newData = false;

//===========

void setup()
{

   Serial.begin(115200);

   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &ldrValues, sizeof(ldrValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      newData = false;
   }
}

Thank you a lot for investing so much time into my problem. I was about to write here what the issue was since I found it out just now. The mistake is so stupid yet I can't understand how it could cause so much trouble.

I was using this to declare my address:

const byte address = "00001";

Then after I was searching the internet the whole day I saw that everyone was also setting the length. So it looks like this:

const byte address[6] = "00001";

So I tried and changed it in both the receiver and the transceiver code and this did the trick. Because you were setting the address correctly it worked in your code but not in mine. Thank you again for investing so much time in this.

I had just started looking that when I got notification of your post. That line of code was throwing a warning but it was working without AccelStepper objects so I kind of ignored it. Then, since I long ago learned to not ignore warnings, I began to investigate when your message showed up. Just goes to show, don't ignore warnings. You do have compiler warnings enabled in the IDE File, Preferences menu, don't you? You should.

Glad you got it to work. I have been racking my brain for hours trying to figure it out. Still not sure why that warning in combination with an AccelStepper object would have that effect. Maybe a more knowledgeable member will shed some light.

1 Like

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