Arduino Wireless Network with Multiple NRF24L01 Modules.

In this tutorial we will learn how to build an Arduino wireless network, composed of multiple NR24L01 transceiver modules. You can watch the following video or read the written tutorial below.
Overview
As an example I made a network of 5 nodes and each of them can communicate with any node in the network and at the same time they can work as both transmitters and receivers. This example is actually set up in a way that explains how to make a much larger network, or to be precise, we can have a total of 3125 modules communicating to each other on a single RF channel. So let’s take a look how it works.

In my previous tutorials we have already learned how to make a wireless communication between two Arduino boards using the NRF24L01 modules and the RF24 library. Now in addition to this library, we will use the RF24Network library, which enables in an easy way to build an Arduino wireless network with many boards communicating to each other. Here’s how the network topology works.

A single NRF24L01 module can actively listen up to 6 other modules at the same time.

This ability is utilized by the RF24Network library to generate a network arranged in a tree topology, where one node is the base, and all other nodes are children of either that node or of another. Each node can have up to 5 children, and this can go 5 levels deep, which means we can create a network of total 3125 nodes. Each node must be defined with a 15-bit address, which precisely describes the position of the node within the tree.

We can actually define the addresses of the nodes in octal format. So, the address of the master or the base is 00, the base children addresses are 01 to 05, the 01 node children addresses are 011 to 051 and so on.

Note that if node 011 wants to talk to the node 02, the communication will have to go through the node 01 and the base node 00, so these two nodes must be active all the time in order the communication to be successful.

Arduino Wireless Servo Motor Control using the RF24Network Library

Before we explain the main example of this tutorial, for better understanding how the library works let’s make a simpler example of two Arduino boards communicating to each other. Here’s the circuit diagram for this example.

You can get the components needed for this Arduino Tutorial from the links below:

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

So using the potentiometer at the first Arduino we will control the servo motor at the second Arduino. Let’s take a look at the source codes now.

Here’s the code at the potentiometer side:


/*

  Arduino Wireless Network - Multiple NRF24L01 Tutorial

 == Example 01 - Servo Control / Node 00 - Potentiometer ==

  by Dejan, www.HowToMechatronics.com

  Libraries:

  nRF24/RF24, https://github.com/nRF24/RF24

  nRF24/RF24Network, https://github.com/nRF24/RF24Network

*/

#include <RF24.h>

#include <RF24Network.h>

#include <SPI.h>

RF24 radio(10, 9); // nRF24L01 (CE,CSN)

RF24Network network(radio); // Include the radio in the network

const uint16_t this_node = 00; // Address of this node in Octal format ( 04,031, etc)

const uint16_t node01 = 01;      

void setup() {

  SPI.begin();

  radio.begin();

  network.begin(90, this_node); //(channel, node address)

}

void loop() {

  network.update();

  unsigned long potValue = analogRead(A0); // Read the potentiometer value

  unsigned long angleValue = map(potValue, 0, 1023, 0, 180); // Convert the value to 0-180

  RF24NetworkHeader header(node01); // (Address where the data is going)

  bool ok = network.write(header, &angleValue, sizeof(angleValue)); // Send the data

First we need to include both libraries RF24 and RF24Network, as well as the SPI library. Then we need to create the RF24 object, and include it into the RF24Network object. Here we need define the addresses of the nodes in octal format, or 00 for this node, and 01 for the other node at the servo side.


In the setup section we need to initialize the network, by setting the channel and the address of this node.


In the loop section we constantly need to call the update() function through which all action in the network happens. Then we read the value of the potentiometer and convert it into a value from 0 to 180 which is suitable for the servo control. Then we create a network header where we assign the address of the node where the data is going. At the end, using the write() function we send the data to the other node. So here the first parameter contains the addresses information, the second parameter points which data will be send, and the third parameter is the size of the data.

Here’s the code at the servo side:


/*

  Arduino Wireless Network - Multiple NRF24L01 Tutorial

  == Example 01 - Servo Control / Node 01 - Servo motor ==

*/

#include <RF24.h>

#include <RF24Network.h>

#include <SPI.h>

#include <Servo.h>

RF24 radio(10, 9); // nRF24L01 (CE,CSN)

RF24Network network(radio); // Include the radio in the network

const uint16_t this_node = 01; // Address of our node in Octal format ( 04,031, etc)

Servo myservo; // create servo object to control a servo

void setup() {

  SPI.begin();

  radio.begin();

  network.begin(90, this_node); //(channel, node address)

  myservo.attach(3); // (servo pin)

}

void loop() {

  network.update();

  while ( network.available() ) { // Is there any incoming data?

    RF24NetworkHeader header;

    unsigned long incomingData;

    network.read(header, &incomingData, sizeof(incomingData)); // Read the incoming data

    myservo.write(incomingData); // tell servo to go to a particular angle

  }

}

On the other side, at the servo motor, we need to define the libraries and the objects in the same way as explained earlier. Here the address of this node in octal format is 01. After defining the servo motor, in the loop section, using the while() loop and the available() function we constantly check whether there is any incoming data. If true, we create a network header through which the data will be accepted and also a variable where the data will be stored. Then using the read() function we read the data, and store it into the incomingData variable. At the end we use this data to move the servo motor according to the potentiometer from the other node.



Comments

Popular posts from this blog

Smartphone Controlled Arduino Rover.

How to Build a Robot Line Follower without a Controller