tags: Arduino RF24

nRF24L01

nRF24L01 is a wireless transmitter and receiver in 2.4G frequencey. The master or slave mode can be control with code. The lenth of data it can transfer is 32 byte. We can send char, integer, boolean and others through matrix or string at once as long as it doesn't over the limit.

Pin out

Look from above and the antenna at front. CE and CSN can be changed to any digital pin you want.

Vcc CSN MOSI IRQ
Gnd CE SCK MISO
3.3V 8 11
Gnd 7 13 12

Send

#include <SPI.h> #include "RF24.h" RF24 rf24(7,8); // Pin CE, CSN(follow the pin you attach above) const byte addr[] = "00001"; //Address char msg[] = "Hahaha"; //Message void setup() { rf24.begin(); rf24.setChannel(83); // Channel num rf24.openWritingPipe(addr); // Channel address rf24.setPALevel(RF24_PA_MIN); // Channel level rf24.setDataRate(RF24_2MBPS); // Transfer rate rf24.stopListening(); // Stop listening (->master mode) } void loop() { rf24.write(&msg, sizeof(msg)); // Send data delay(1000); }

Receive

#include <SPI.h> #include "RF24.h" RF24 rf24(7,8); // Pin CE, CSN const byte addr[] = "00001"; const byte pipe = 1; //Select listening pipe (1~6) void setup() { Serial.begin(9600); rf24.begin(); rf24.setChannel(83); rf24.setPALevel(RF24_PA_MIN); rf24.setDataRate(RF24_2MBPS); rf24.openReadingPipe(pipe, addr); // Open reading channel & reading pipe rf24.startListening(); // Slave mode Serial.println("nRF24L01 ready!"); } void loop() { if (rf24.available(&pipe)) { char msg[32] = ""; rf24.read(&msg, sizeof(msg)); for(auto &i:msg){ Serial.print(i); Serial.print(' '); }Serial.print('\n'); } }

Reference

  1. nRF24L01 Wireless Module & Arduino communication test (two)
  2. How nRF24L01+ Wireless Module Works & Interface with Arduino
  3. RF24 libraries