0
0
Arduinoprogramming~7 mins

RF communication with nRF24L01 in Arduino

Choose your learning style9 modes available
Introduction

RF communication lets two devices talk wirelessly using radio waves. The nRF24L01 is a small, cheap module that helps Arduino boards send and receive data without wires.

You want to control a robot remotely without cables.
You need to send sensor data from one Arduino to another across a room.
You want to build a wireless weather station.
You want to create a simple walkie-talkie system between two devices.
Syntax
Arduino
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(CE_pin, CSN_pin); // Create radio object
const byte address[6] = "00001";

void setup() {
  radio.begin();
  radio.openWritingPipe(address); // Set address for sending
  radio.openReadingPipe(0, address); // Set address for receiving
  radio.setPALevel(RF24_PA_LOW); // Set power level
  radio.startListening(); // Start listening for data
}

void loop() {
  // Your code to send or receive data
}

CE and CSN pins connect the Arduino to the nRF24L01 module.

openWritingPipe() sets the address to send data to.

Examples
This example sends the word "Hello" every second.
Arduino
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10); // CE=9, CSN=10
const byte address[6] = "00001";

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.stopListening(); // Set to transmit mode
}

void loop() {
  const char text[] = "Hello";
  radio.write(&text, sizeof(text));
  delay(1000);
}
This example listens for messages and prints them to the Serial Monitor.
Arduino
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);
const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    char text[32] = {0};
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}
Sample Program

This program sends the message "Hi Arduino!" every 2 seconds and prints if it was sent successfully.

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

// Pins for CE and CSN
RF24 radio(9, 10);
const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_LOW);
  radio.stopListening(); // Transmit mode
}

void loop() {
  const char message[] = "Hi Arduino!";
  bool success = radio.write(&message, sizeof(message));
  if (success) {
    Serial.println("Message sent successfully");
  } else {
    Serial.println("Failed to send message");
  }
  delay(2000);
}
OutputSuccess
Important Notes

Make sure the CE and CSN pins match your wiring.

Both sender and receiver must use the same address to communicate.

Use the Serial Monitor to see messages or debug.

Summary

nRF24L01 modules let Arduinos talk wirelessly using radio signals.

You set addresses to tell modules where to send or listen for data.

Use the RF24 library to easily send and receive messages.