NRF24L01 and Stepper Motor

Hello,

I'm having issues with my Arduino project. The setup is the following:

Arduino 1 (Transmitter)
Arduino Nano Every + 2 momentary switches + NRF24L01

Arduino 2 (Receiver)
Arduino Uno + TB6600 + Nema23 Stepper motor + NRF24L01

The aim of the project is to get the NRF24L01 connected to the Nano Every board to communicate with the NRF24L01 on the Arduino Uno board and move the stepper motor either clockwise or anticlockwise based on which momentary switch on transmitter (Nano Every) is held down.

After lots of serial monitor analysis I can see that the NRF24L01's are communicating with each other and is registering the button presses however the stepper motor is not moving.

I tried some test code with the NEMA23 stepper motor with the same connection setup and it works.

As a result I'm at a loss and have tried for 2 days to get this to work but to no avail. Please see the code below:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte address[6] = "00001";
boolean button1_state = 0;
boolean button2_state = 0;
int state = 0;

int stepPin = 4;
int dirPin = 3;
int enablePin = 2;



void setup()
{
  Serial.begin(9600);
  radio.begin();
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}



void loop()
{
  if (radio.available())
  {
    radio.read(&state, sizeof(state));
    if (state == 0)
    {
      Serial.println("No button is pressed");
    }
    else if (state == 1)
    {
      Serial.println("BUTTON 1 PRESSED!!");
      Serial.println("Stepper forward");
      digitalWrite(dirPin, HIGH);
      digitalWrite(stepPin, LOW);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(60);
    }
    else if (state == 2)
    {
      Serial.println("BUTTON 2 PRESSED!!");
      Serial.println("Stepper reverse");
      digitalWrite(dirPin, LOW);
      digitalWrite(stepPin, LOW);
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(60);
    }
    else if (state == 3)
    {
      Serial.println("Invalid input..Both buttons pressed");
    }
    Serial.println();
  }
}

Any help would be appreciated. Thanks again.

how often do you send the button pressed message from the sender?


please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

Done, Thank you. I believe the button pressed message is being sent at a frequency of 9600 but i could be looking at the wrong thing.

9600 sounds like the baud rate of something.

what I meant to ask is : if you keep the button pressed, does the Arduino keeps firing the button pressed message or you send the message only once?

can you post the button code?

can you share what you see in the Serial monitor?

I'm not sure what you mean but i sourced information you asked for. Below is the transmitter code (Arduino Nano Every).

 #include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte address[6] = "00001";
int button1_pin = 2;
int button2_pin = 3;
boolean button1_state = 0;
boolean button2_state = 0;
int state = 0;



void setup()
{
  pinMode(button1_pin, INPUT_PULLUP);
  pinMode(button2_pin, INPUT_PULLUP);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}


void loop()
{
  button1_state = digitalRead(button1_pin);
  button2_state = digitalRead(button2_pin);

  if ( (button1_state == 1) && (button2_state == 1) )
  {
    state = 0;
  }
  else if ( (button1_state == 0) && (button2_state == 1) )
  {
    state = 1;
  }
  else if ( (button1_state == 1) && (button2_state == 0) )
  {
    state = 2;
  }
  else if ( (button1_state == 0) && (button2_state == 0) )
  {
    state = 3;
  }

  radio.write(&state, sizeof(state));
  delay(1000);
}

Below is the serial monitor information received by the Arduino Uno (Receiver). Hopefully this helps.

When you read the button states from on the transmitter side, it could be done much easier:

int state;
void loop()
{
  state = (digitalRead(button2) << 1) | digitalRead(button1);
  //State == 0: both down
  //State == 1: 1 down
  //State == 2: 2 down
  //State == 3: None down
  //if ((state == 0) || (state==3)) state ^= 3; //Invert 3 to 0 or 0 to 3
  //Transmit
}

On the receiver side, you only handle the stepper when something is received. You should consider to use the Stepper library to handle the stepper instead.

Now we see that the emitter is sending the button Status once per second

So on the receiving side, you would issue steppers orders only every second since those orders are within the “if I got a message” part. Your stepper won’t move fast…(even worse if you are configured for micro steps)

You should re Architect the code so that messages say start stepping forward or backward or stop. That will define the state on the receiver and it will keep on going until a new state is sent

I see. Is there anyway i could hire you to complete this on my behalf?

I have a nice glass of Wine in hands and enjoying a great summer night and I’ve a busy Saturday on the sea so probably not :slight_smile:

There is the Jobs and Paid Consultancy forum If you want to compensate someone to help you out

That being said, It’s not that difficult and if you study state machines a bit you can probably write the code yourself

1 Like

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