NRF24L01无线模块的使用

NRF2401芯片pin定义

NRF24L01模块pin定义

  1. VCC 脚接电压范围为 1.9V~3.6V 之间, 不能在这个区间之外, 超过 3.6V 将会烧毁模块, 推荐电压 3.3V 左右
  2. 除电源 VCC 和接地端, 其余脚都可以直接和普通的 5V 单片机 IO口直接相连, 无需电平转换. 当然对 3V 左右的单片机更加适用了.
  3. 硬件上面没有 SPI 的单片机也可以控制本模块, 用普通单片机 IO口模拟 SPI. 不需要单片机真正的串口介入, 只需要普通的单片机 IO口就可以了, 当然用串口也可以了.

NRF24L01的USB串口调试设备

连接方式为 NRF24L01的天线端朝向远离USB口的方向, 8pin对齐插入. 连接后是一个Z字形, 不是U字形.

Ubuntu下连接USB串口设备后检测NRF24L01模块

apt-get install cutecom安装cutecom, 运行打开界面. 通过dmesg得到串口设备名(例如 /dev/ttyUSB0)后连接. 默认波特率为9600, 点击Open Device打开

1. 查询设备配置: AT? 回车
2. 设置接收地址: AT+RXA=0xAA,0xBB,0xCC,0xDD,0xEE
3. 设置目标地址: AT+TXA=0x11,0x22,0x33,0x44,0x55

Arduino接线方式

24L01     Arduino UNO
GND       GND
VCC       3.3V
CE        digIO 7
CSN       digIO 8
SCK       digIO 13
MOSI      digIO 11
MISO      digIO 12
IRQ       - 

发送端测试代码

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

const uint64_t pipe = 0xE8E8E8E8E8LL;
RF24 radio(7,8);

void setup(void){
  Serial.begin(115200);
  Serial.println("RF24 Sender Started");

  radio.begin();
  radio.openWritingPipe(pipe);
  radio.printDetails();
}

void loop(void) {
  Serial.print("Sending...");
  unsigned long start_time = micros();
  Serial.print(start_time);
  Serial.print(". ");
  // Take the time, and send it. This will block until complete
  if (!radio.write( &start_time, sizeof(unsigned long) )){
    Serial.println("failed");
  } else {
    Serial.println("succeeded");
  }
  delay(1000);
}

接收端测试代码

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
 
const uint64_t pipe = 0xE8E8E8E8E8LL;
RF24 radio(7,8);
 
void setup(void){
  Serial.begin(115200);
  Serial.println("RF24 Receiver Started");

  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  radio.printDetails();
}

void loop(void){
  unsigned long got_time;
  if (radio.available()){
    Serial.print("Receiving...");
    radio.read(&got_time, sizeof(unsigned long) );
    Serial.print(got_time);
    Serial.println(".");
  }
  delay(100);
}

测试的时候, 先启动接收端, 再启动发送端. 如果接收端未启动, 发送端的radio.write()方法会返回失败.

原文地址:https://www.cnblogs.com/milton/p/8807436.html