0805-761-5108 || hello@nerdshed.com.ng

How about Two Arduino Talk to Each Other.. Wirelessly?

In this article, we are going to control an Arduino’s built in LED on pin 13 with another Arduino using a 2.4 Ghz NRF2401 wireless transceiver

What you’ll need to carry out this project;

Hardware

  • 2 x Arduino UNO microcontroller board, buy it here
  • 2 x 2.4 Ghz NRF2401 wireless transceiver, get them here

Software

  • The latest Arduino IDE (download here)

Introducing the 2.4 Ghz NRF2401 transceiver module.

The 2.4 Ghz NRF2401  is used where wireless communication or control between systems is needed. The 2.4 Ghz NRF2401 is a transceiver meaning it can transmit and receive data. The module is very cheap and can be interfaced with any micro-controller.

Technical specifications:

  • Antenna range > 1km
  • Frequency  = 2.4 Ghz
  • Range of operating input voltage = 1.9v -3.6v

Note: Special arrangements must be made when working with 5v circuits,

esp8266 board
esp8266 board
The 2.4 Ghz NRF2401 transciever

IMPLEMENTATION

Let us proceed to interfacing the Arduino UNO with the 2.4 Ghz NRF2401 transceiver.

STEP 1: CIRCUIT CONNECTION

Make sure the two Arduino/NRF2401 are connected like this.

Arduino to NRF2401 connection
Arduino to NRF2401 connection

NOTE: OPERATING VOLTAGE RANGE IS 1.9V TO 3.6V. DO NOT EXCEED THE MAXIMUM VOLTAGE.

STEP 2: INSTALL REQUIRED LIBRARIES

The NRF2401 transceiver requires a library to be installed and used for the project to work.

  • Get the library RF24-master
  • Make sure your Arduino IDE is closed.
  • Once the zip is downloaded, extract the folders to the libraries folder of your Arduino
  • You’ve installed the RF24 library successfully.

STEP 3: CODE

Since there will be a master Arduino which will send messages to the slave Arduino. Upload this code to your selected master Arduino

Master Arduino Code

#include <SPI.h>
#include <RH_NRF24.h>

// Create an instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Arduino Mini

void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}

void loop()
{
  if (nrf24.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))
    {
//      NRF24::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
      
      // Send a reply
      uint8_t data[] = "And hello back to you";
      nrf24.send(data, sizeof(data));
      nrf24.waitPacketSent();
      Serial.println("Sent a reply");
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

Slave Arduino Code

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}

void loop()
{
  Serial.println("Sending to nrf24_server");
  // Send a message to nrf24_server
  uint8_t data[] = "Hello World!";
  nrf24.send(data, sizeof(data));
  
  nrf24.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (nrf24.waitAvailableTimeout(500))
  { 
    // Should be a reply message for us now   
    if (nrf24.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is nrf24_server running?");
  }
  delay(400);
}

STEP 4: RESULTS

  • Open up the Serial monitor of master Arduino code
Arduino serial monitor result
Arduino serial monitor result
  • Using any terminal of your choice, i am using Tera Term. Download it here
    Arduino serial monitor result
    Arduino serial monitor result

I hope you enjoyed building this project

Wanna do more?

Build a wireless temperature monitoring system using the following parts

  • 1 x DHT22 Temperature/Humidity sensor, get it here
  • 2 x Arduino Uno, get them here
  • 2 x NRF2401, get them here

Share This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

No products in the cart.

X