【Arduino】NRF24L01无线模块6通道通信
前言
在我们使用NRF24L01 2.4G无线通讯模块时往往不只是发送一个数据,这里我以我的项目总使用的6通道来分享。
使用到的材料:
两块NRF24L01模块
两块Arduino开发板(型号UNO/NANO)
杜邦线若干
一、NRF24L01接线
这里我用到的是这款2.4G无线模块,通讯距离说是可以达到1000m,实测还是挺远的,满足日常使用需求。
与单片机连接如下图
对于Arduino接线如下:
VCC -> 3.3V
GND -> GND
CSN -> 7
CE -> 8
MOSI -> 11
MISO -> 12
SCK -> 13
注意:无线模块供电用3.3V
二、代码
1.发射端代码
这里我用到了两个按键两个摇杆来作为数据输入
代码如下(示例):
//发射端程序
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
/*摇杆*/
#define PIN_X1 0
#define PIN_Y1 1
#define PIN_Y2 2
#define PIN_X2 3
/*数据包*/
int value;
typedef struct Signal {
byte x11=1;
byte y11=1;
byte x21=1;
byte y21=1;
byte gyr=0;
byte pit=1;
}Signal;
Signal signal;
/*按键*/
int buttonPin1 = 5; // 左按键的管脚定义
int buttonPin2 = 2; // 右按键的管脚定义
bool a1=0;
//bool a2=0;
void setup()
{
Serial.begin(115200);
pinMode(PIN_X1, INPUT);
pinMode(PIN_Y1, INPUT)