当前位置:网站首页>【nRF24L01 connects with Arduino to realize wireless communication】
【nRF24L01 connects with Arduino to realize wireless communication】
2022-08-02 04:33:00 【WENJIE Technology】
nRF24L01 与 Arduino Connect wireless communication
前言
在本教程中,You will use two examples to understand nRF24L01 Arduino 接口.在第一个示例中,我们将发送“Hello world”And a command to blink to connect to another Arduino 的 LED.在第二个示例中,We'll have a two-way control,And from the first Arduino Send commands to the second Arduino 上闪烁 LED,Then we will from the second Arduino Send the command to the first Arduino 上闪烁 LED.
nRF24L01 模块
nFR24L01 Is a transceiver module,This means that it can send and receive data.These modules are very cheap,体积更小,There are many specifications.Some of these module specifications are as follows:
In the process of transmission power consumption in12mA左右,比LED还要小.
它可以以 250Kbps 到 2 Mbps The baud rate of running.
If used in open space with the antenna,The range of 100 米.
它可以同时发送和接收数据.
Each module maximum and 6 Other modules communication.
它使用 2.4 GHz 频段.
它可以以 1 MB The transmission rate of sending 1 到 25 Bytes of the original data.
它有 125 个不同的频道.
nRF24L01 模块通过 SPI 通信与 Arduino 协同工作.Module pin is as follows:
The working voltage of the module as 1.9 至 3.6V,But other pins can withstand 5V,This means that the other pin can be connected directly to the Arduino.
MOSI、MISO 和 SCK 是 SPI 引脚,The need to connect to the Arduino 的 SPI 引脚.不同的 Arduino 有不同的 SPI 引脚.
CSN 和 CE Model is used to set the module to activities and in command mode and delivery mode switch between.These can be connected to Arduino 的任何数字引脚.
IRQ Pin is interrupt pin,You don't have to connect it.
示例 1 - nRF24L01 Arduino 接口
在 nRF24L01 arduino Interface in the first example,We will simply be data from a Arduino 发送到另一个 Arduino.When we press the connection to the first Arduino 的按钮时,Connected to the second Arduino 的 LED 会亮起.
The first example of the circuit diagram as shown below,连接如下所示.Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001"; //地址
int button_pin = 2;
boolean button_state = 0;
void setup() {
pinMode(button_pin, INPUT);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop()
{
button_state = digitalRead(button_pin);
if(button_state == HIGH)
{
const char text[] = "Your Button State is HIGH";
radio.write(&text, sizeof(text)); //发送数据
}
else
{
const char text[] = "Your Button State is LOW";
radio.write(&text, sizeof(text)); //发送数据
}
radio.write(&button_state, sizeof(button_state));
//发送数据
delay(1000);
}
接收方代码:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";//地址保持一致
boolean button_state = 0;
int led_pin = 3;
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop()
{
if (radio.available()) //等待接收数据
{
char text[32] = ""; //保存数据
radio.read(&text, sizeof(text));
radio.read(&button_state, sizeof(button_state));
if(button_state == HIGH)
{
digitalWrite(6, HIGH);
Serial.println(text);
}
else
{
digitalWrite(6, LOW);
Serial.println(text);}
}
delay(5);
}
示例 2 - nRF24L01 Arduino 接口
在 nRF24L01 Arduino In the second example interface,We'll have a two-way communication.首先,We will be from the first Arduino Send commands to light is connected to the second Arduino 的 LED,Then we will from the second Arduino Send commands to light is connected to the first Arduino 的 LED.第一个 Arduino 的代码:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {
"00001", "00002"};
//发送地址和接收地址
int button_pin = 2;
int led_pin = 3;
boolean button_state = 0;
boolean button_state1 = 0;
void setup() {
pinMode(button_pin, INPUT);
pinMode(led_pin, OUTPUT);
radio.begin();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_MIN);
}
void loop()
{
delay(5);
radio.stopListening(); //Set to the sender
button_state = digitalRead(button_pin);
radio.write(&button_state, sizeof(button_state)); //发送数据
delay(5);
radio.startListening(); //Set to the receiving end
while(!radio.available()); //等待接收数据
radio.read(&button_state1, sizeof(button_state1)); //读取数据
if (button_state1 == HIGH)
{
digitalWrite(led_pin, HIGH);
}
else
{
digitalWrite(led_pin, LOW);
}
}
第二个 Arduino 的代码:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {
"00001", "00002"};
//发送地址和接收地址
int button_pin = 2;
boolean button_state = 0;
boolean button_state1 = 0;
int led_pin = 3;
void setup() {
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.setPALevel(RF24_PA_MIN);
}
void loop()
{
delay(5);
radio.startListening();
//Set to the receiving end
if (radio.available())
//等待接收数据
{
radio.read(&button_state, sizeof(button_state));
if(button_state == HIGH)
{
digitalWrite(led_pin, HIGH);
}
else
{
digitalWrite(led_pin, LOW);
}
delay(5);
radio.stopListening();
//Set to the sender
button_state1 = digitalRead(button_pin);
radio.write(&button_state1, sizeof(button_state1)); //发送数据
}
}
The source code and libraries
Source code and library files have been uploaded to the personal home page
边栏推荐
猜你喜欢
随机推荐
【DS3231 RTC实时时钟模块与Arduino接口构建数字时钟】
【Popular Science Post】UART Interface Communication Protocol
Temporal Segment Networks:Towards Good Practices for Deep TSN论文精读笔记
深度学习实战(1):花的分类任务
写博客的原因。
[Arduino uses a rotary encoder module]
【Arduino 连接 SD 卡模块实现数据读写】
日志分析系统:ELK
【nRF24L01 与 Arduino 连接实现无线通信】
基于阿里云OSS+PicGo的个人图床搭建
Typora use
ffmpeg 有声视频合成背景音乐(合成多声音/合成多音轨)
十大实用的办公工具网站,可以解决你日常100%的问题
C语言上机题(基础)
C# Form按ESC关闭窗体
机器学习相关 概率论重点笔记
zsh: command not found: xxx 解决方法
深度学习理论:model.fit 函数参数详解
GM8775C规格书,MIPI转LVDS,MIPI转双路LVDS分享
目标检测(一):R-CNN系列