当前位置:网站首页>【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
边栏推荐
- sacalatest AnyFunSuite:no implicits found for parameter pos
- 单火线开关设计详解
- [Arduino connected to GPS module (NEO-6M) to read positioning data]
- 78XX 79XX多路输出电源
- Flutter入门之网络请求篇
- NE5532运放加法器
- openmv学习 2022.5.9
- ffmpeg 有声视频合成背景音乐(合成多声音/合成多音轨)
- 【Arduino 连接 SD 卡模块实现数据读写】
- 连接本地MySql时出现2003-Can‘t connect to MySql server on ‘localhost‘(10061)
猜你喜欢
随机推荐
[Arduino connected to GP2Y1014AU0F dust sensor]
Spark特征工程-归一化 和 分桶
zsh: command not found: xxx 解决方法
USB2.0一致性测试方法_高速示波器
【使用树莓派时碰到的一些问题】
基于树莓派的智能箱包开发环境搭建
【心率传感器与Arduino连接读取心率数据】
USB HUB USB集线器电路设计
【opencv】error: (-215:Assertion failed) ssize.empty() in function ‘cv::resize‘报错原因
将ORCAD原理图导入allegro中进行PCB设计
MPU6050 加速度计和陀螺仪传感器与 Arduino 连接
Arduino点亮数码管
3D建模作品
TQP3M9009电路设计
[Arduino uses a rotary encoder module]
redo log与binlog间的破事
OneNET Studio与IoT Studio对比分析
AD8361检波器
中国大陆开源镜像站汇总
十大实用的办公工具网站,可以解决你日常100%的问题









![[Arduino uses a rotary encoder module]](/img/2a/43ffe782d6d5aa9f38f875bb5d98b1.png)