Can't send button press value between two NRF24L01+

I am working on my school robotics club where we want to trigger a servo using a push button from a joystick, where when the button is held down the servo will be activated. We know that the servos and the joysticks are wired correctly as we ran separately so we figured that the problem is due with some issue in the programs between the two boards.
From the Transmitter code we know that the button press is detected, but the Receiver code still prints out that the button isn't pressed and the servo doesn't rotate.

A replication of the boards is in the image:

The Transmitter Code

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

RF24 radio(7, 8);  // CE, CSN
const byte addresses[][6] = { "00001", "00002" };
const int SW_PIN = 6;  // Arduino pin connected to button's pin
boolean buttonState = 0;
// variables will change:
int lastButtonState;     // the previous state of button
int currentButtonState;  // the current state of button

void setup() {
  Serial.begin(9600);             // initialize serial
  pinMode(SW_PIN, INPUT_PULLUP);  // set arduino pin to input pull-up mode
  Serial.println("Pin SW_Pin established");

  Serial.println("Servo attatched");

  currentButtonState = digitalRead(SW_PIN);
  Serial.println("Read first SW_Pin, don't know why I'm here");
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Builtin LED establish");
  Serial.println("Begin radio setup");

  radio.begin();
  radio.openWritingPipe(addresses[1]);     // 00002
  radio.openReadingPipe(1, addresses[0]);  // 00001
  radio.setPALevel(RF24_PA_MIN);
  Serial.println("Writing pipe to adress 1 established");
  Serial.println("Reading pipe to adresses 1 & 2 established");
  Serial.println("RF24_PA_MIN established");
}



void loop() {
  delay(5);
  radio.stopListening();
  buttonState = digitalRead(SW_PIN);
  radio.write(&buttonState, sizeof(buttonState));
  delay(500);
  if (buttonState == LOW) {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println(F("ButtonState == LOW"));
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println(F("ButtonState == HIGH"));
  }
  radio.startListening();
}

Receiver Code

// this is the hook feature code for the drone project
// it will be controlled by a third joystick but only the button will be used

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

const int SW_PIN = A5;
const int SERVO_PIN = 3;

Servo servo;

RF24 radio(7, 8);  // CE, CSN
const byte addresses[][6] = { "00001", "00002" };
boolean buttonState = 0;
void setup() {
  Serial.begin(9600);             // initialize serial
  pinMode(SW_PIN, INPUT_PULLUP);  // set arduino pin to input pull-up mode
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Pin SW_Pin established");
  radio.openWritingPipe(addresses[1]);
  Serial.println("Writing pipe to adress 1 established");
  radio.openReadingPipe(1, addresses[0]);
  Serial.println("Reading pipe to adresses 1 & 2 established");
  radio.setPALevel(RF24_PA_MIN);
  Serial.println("RF24_PA_MIN established");
  servo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
  servo.write(0);
  Serial.println("Servo attatched, set to default zero");
}

void loop() {
  delay(5);
  radio.startListening();
  radio.read(&buttonState, sizeof(buttonState));
  if (buttonState == LOW) {
    Serial.println(F("The button is pressed"));
    servo.write(90);
    digitalWrite(LED_BUILTIN, LOW);
  } else if (buttonState == HIGH) {
    Serial.println(F("The button isn't pressed"));
    servo.write(45);
    digitalWrite(LED_BUILTIN, HIGH);
  }
  radio.stopListening();
}```

I would suggest, if you already have not, to follow the tutorials here:

Follow the basic tutorials at the start in order to establish that your radio modules are wired correctly and can communicate with each other. If you are having problems with SimpleTx and SimpleRx, then run the CheckConnection sketch from post #30 of the tutorial and post the serial monitor output here.

Once you have confidence in your radio modules, you can then build on the tutorial examples to complete your project.

Arduino can not provide enough power for NRF24 module.

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