Today, we are going to look at something simple yet useful. I am going to show you how you
can send data wireless from one Arduino board to another using some very common wireless
modules. I will be using these for the home automation project but I decided to make a
separate video for future reference.
These modules are by far the simplest ones you could use to add wireless capability to
your projects. The modules themselves are extremely cost effective and are available
for much less than a dollar. The older version of this was called the nRF24L01 and it only
supported data rates of 1 or 2 megabits per second. The newer version is called the nRF24L01+
and it can support an additional lower data rate of 250kbps. The lower data rate does
consume slightly less current and I also think that it will be less susceptible to interference
compared to higher data rates as it will not use as much bandwidth.
The module I will be using has a PCB antenna which makes it compact but you also do get
a variant that supports an external antenna. This allows for much higher range of about
1000 meters in line of sight. These are best case values so take them with a pinch of salt.
The power down current consumption is 900 nano amps which makes this suitable for battery
operated nodes.
In order to get this working, we need two such RF modules, we will also need two Arduino
boards and I will be using two Arduino Nanos – the Nano every and the old one which is
simply called Nano. The RF module needs to be powered with 3.3V and it uses SPI for communication.
This means that we need 4 additional pins for communication. Finally, we have the CE
pin which is used to activate the transmitting or receiving mode bringing the total to 7
pins. On the transmitter side, I am going to add a switch to pin 2, such that it will
be pulled LOW when the switch is pressed. On the receiver, I will connect an LED to
pin 2. You could select any other pin that you like. This is the reference wiring diagram
that you can use.
You cannot use the modules with a breadboard because the pin spacing is not enough to place
it in the middle and if you place it anywhere else, then you will end up shorting the pins.
This means that you either have to solder wires directly to it or use some jumper cables.
I decided to go with the latter. So here’s an example of how I’ve connected the Arduino
Nano Every board to the radio module.
Now that we have a goal in mind, it is time to work on the sketch so that we can test
it out.
For this, we will need to install a library, so use the tools menu to open up the library
manager and then type in NRF24. Select this library by TMRh20 and install it. Give it
a minute to complete and close the window. Now, you can use the file menu to explore
the example sketches but these are a little complicated to download the sketches from
the GitHub repository and open them up in the Arduino IDE.
Let’s start with the transmitter. All we are doing here is assigning an address to
the module and we will have to use the same address for the receiver. This is how the
RF modules know who they have to communicate with. We then initialize the module and get
it ready for transmission. If the switch is pressed, then the pin will go LOW and we then
send the message 111, or else, we send the message 000. You can obviously change the
value that is being sent to suit your requirements. I am going to select the correct board and
COM port here and then upload this sketch to the transmitter.
Once that is done, open up the sketch for the receiver. Make sure you use the exact
same address that you used in the transmitter sketch. All we are doing here is getting the
module ready to receive data and we then start listening to the data that is being sent to
it. If we receive the number 111 then we know that the switch was being pressed and so we
switch ON the LED. In all other cases, we switch the LED OFF. I am going to select the
correct board, COM port and then upload this to it.
It's now time to test it out. I have the transmitter on one side with the receiver on the other.
When I press the switch, you will notice that the LED will light up instantly.
That is the quickest way to get started using these modules. You can extend this principle
to do just about anything that you want. Read through the pingpair_sleepy example to
get an idea of how you can use the sleep mode to conserve battery. A single node can act
as both a transmitter and receiver, though you cannot send and receive data at the same
time. You have the stopListening and startListening functions which allow you to take control.
For example, if you want to send data, then you will first have to call the stopListening
function. One you are done sending data, you can then start listening again.
What we have here is a one to one communication between two nodes. We have given the nodes
the same address which allows them to communicate with each other. Theoretically, we could add
another node with the same address and communicate between all three and this can then be extended
as needed. However, only one node should be sending data at any given time or else there
will be collisions. If you want to send data between multiple nodes then it would be advisable
to create a mesh network which will get rid of the collisions as communication between
the nodes will be automatically handled in the network. The second benefit is that you
can extend the range between the nodes. As an example, let’s say that we have two nodes
in the one to one communication mode and that the maximum distance between any given nodes
is D. This means that all the nodes will have to be located within this distance or else
they will not be able to communicate. In a mesh network, each node can act as a repeater,
which means that as long as the distance between any two nodes is within D, we can still communicate
between all of them. This means that nodes can be located much further away. There is
a R24Mesh library from the same developer that can be used for this.
And that is it for this video. Hopefully this will make it easier for you to add wireless
communication to your projects. I will be integrating this with the home automation
project next so stay tuned for that. Thank you for watching and I will see you in the
next one!