You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino Uno与Nano搭配NRF24L01模块:接收器继电器未触发的故障排查请求

Troubleshooting NRF24L01 Transmitter/Receiver Relay Issue

Hey there, let's dig into why your NRF24L01 setup isn't triggering the relay on the receiver side. I've gone through your code and spotted several key issues to fix, along with some optimizations for stable communication:

1. Critical Relay Pin Mode Mistake

First and foremost, your receiver code has an invalid pinMode call that's blocking the relay from working entirely:

pinMode(RELAY,LOW);

The second argument for pinMode() must be INPUT, INPUT_PULLUP, or OUTPUT—LOW is not a valid mode here. Since the relay needs to be controlled as an output, update this to:

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW); // Initialize relay to OFF state (adjust based on your relay's trigger logic)

This is the biggest blocker—without setting the pin as an output, digitalWrite won't have any effect on the relay.

2. Enable Serial Debugging to Verify Communication

You commented out the serial initialization in the receiver code. Turning this on will help you confirm if the receiver is actually getting messages from the transmitter:

  • Uncomment Serial.begin(9600); in the receiver's setup()
  • Add a print statement right after reading the message:
Serial.print("Received value: ");
Serial.println(receivedMessage[0]);

Check the Serial Monitor to see if you're getting the 111 value—this will tell you if the problem is with NRF24 communication or relay control.

3. Standardize NRF24 Configuration for Stable Communication

Even with correct wiring, NRF24 modules can be finicky if configurations don't match between transmitter and receiver:

  • Set the same power level (PALevel) on both devices. For example, add radio.setPALevel(RF24_PA_HIGH); to both transmitter and receiver setups (adjust to RF24_PA_MIN if modules are very close together)
  • Add a consistent data rate to both sides, like radio.setDataRate(RF24_250KBPS);—lower rates are more reliable for short-range setups
  • Explicitly set the transmitter to stop listening with radio.stopListening(); (it's the default, but making it explicit avoids confusion)

4. Fix Data Transmission Length

You're sending an int array, but specifying a length of 1 in radio.write() and radio.read(). Since int in Arduino is 2 bytes, using sizeof() ensures you're sending/receiving the full size of the data:

// Transmitter:
radio.write(SentMessage, sizeof(SentMessage));
// Receiver:
radio.read(receivedMessage, sizeof(receivedMessage));

Right now, you're sending the lower byte of 111 (which still works since it's less than 255), but using sizeof() makes the code robust for future changes.

Revised Working Code Examples

Transmitter (Arduino Nano)

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

int SentMessage[1] = {111};
RF24 radio(7, 8); // CE, CSN pins for Nano
const byte address[6] = "node1";

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening(); // Ensure we're in transmit mode
}

void loop() {
  SentMessage[0] = 111;
  radio.write(SentMessage, sizeof(SentMessage));
  delay(100);
}

Receiver (Arduino UNO)

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

#define RELAY 4
RF24 radio(7, 8); // CE, CSN pins for UNO
int receivedMessage[1] = {0};
const byte address[6] = "node1";

void setup() {
  Serial.begin(9600); // Enable debug output
  radio.begin();
  radio.openReadingPipe(1, address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();
  
  pinMode(RELAY, OUTPUT); // Correctly set relay pin as output
  digitalWrite(RELAY, LOW); // Start with relay off
}

void loop() {
  if (radio.available()) { // Use if instead of while to avoid blocking other code
    radio.read(receivedMessage, sizeof(receivedMessage));
    Serial.print("Received: ");
    Serial.println(receivedMessage[0]);
    
    if (receivedMessage[0] == 111) {
      digitalWrite(RELAY, HIGH);
      Serial.println("Relay activated!");
    } else {
      digitalWrite(RELAY, LOW);
      Serial.println("Relay deactivated!");
    }
  }
}

内容的提问来源于stack exchange,提问作者Shreyas Sahu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:54:07