The larger blue and black boards are two versions of the same microcontroller, and the two smaller boards at the bottom of the photo are the radio modules connected to a microcontroller with jumper wires. Two separate microcontroller/radio pairs are used in this project.
Microcontrollers
A microcontroller is a small device designed to execute instructions. Microcontrollers are similar to computers in that they interpret computer code to accomplish tasks, but microcontrollers are generally constrained to complete very specific tasks. They execute instructions many orders of magnitude slower than a typical computer processor, and oftentimes only have one processing core, rendering them inefficient for doing multiple things at once. Despite these limitations, microcontrollers are easy to program and operate, and it’s simple to use them with various radio modules.
The radio modules we’ll use are capable and inexpensive devices commonly used by hobbyists. They offer a transmit range of a few hundred feet and operate in the unlicensed 2.4 GHz radio band (the same band shared by Wi-Fi routers, microwave ovens, and many other devices). These particular devices utilize the NRF24L01 chipset.
The Arduino IDE
First, we’ll download the Arduino software that we’ll use to program the microcontroller. This software is an IDE, or integrated development environment. IDEs generally contain all the pieces necessary to help a programmer write software, then compile it and deploy it on a device. The Arduino IDE supports external libraries, or collections of software that enable support for a specific function. In this case, we’ll use a library to interface with the radio module. Download and install the Arduino IDE from www.arduino.cc/en/Main/Software . The Arduino IDE is available on macOS, Windows, and Linux.
- 1.Go to Sketch ➤ Include Library ➤ Manage Libraries (as shown in Figure 6-3).
- 2.
Search “RF24” in the window that appears, and find the library named “RF24” (by user TMRh20). You may need to scroll down.
- 3.
Click the library, and then click the install button. Select the most recent version from the dropdown. This step is shown in Figure 6-4.
RF24 is a library that makes it easy to interface with the NRF24L01 radio modules. This library contains collections of software that provide a simple code-based interface to initialize the radio modules, change various settings, and send/receive packets of data.
The Code
The microcontrollers themselves do nothing without instruction; microcontrollers need code to execute in order to be useful. We’ll use separate code for each microcontroller. One will act as a “server” to serve messages, and another will act as a “client” that receives the messages and sends a response. This network model is common across many communications systems, such as with the servers that power much of the Internet.
Server.ino
Client.ino
The client sketch will wait until it receives a message and then send a response back to the server. We’ll now wire the circuit, then come back to these sketches.
The Radio Module
As mentioned before, the radio module utilizes the NRF24L01 2.4 GHz radio transceiver. This integrated circuit is inexpensive and well supported with many different software libraries. The NRF24L01 chip does not occupy much space on the module—the rest of the hardware is supporting equipment necessary for the transmission and reception of data. The radio module also includes a 2.4 GHz antenna in the form of a trace antenna. Trace antennas occupy an extremely small amount of space and can be manufactured directly on a printed circuit board with minimal to no changes to manufacturing hardware. Trace antennas have the highest directional gain to electromagnetic waves hitting the circuit board perpendicularly. Other components on the board include a 16 MHz oscillator (a crystal that produces a signal with a frequency of 16 MHz) as well as various resistors and capacitors to regulate power.
Microcontrollers interface with this module using a digital communications standard called Serial Peripheral Interface (SPI). In SPI, a “master” microcontroller controls “slave” peripheral devices. SPI communication requires a clock line to synchronize communications, two data lines to actually transmit messages, and a select line to select which slave device is active. This module also has lines for power and ground and a special line to enable transmitting/receiving. In total, you will make seven connections between the Arduino and the radio module.
The Circuit
We’ll make electrical connections between the radio module and your Arduino device using jumper wires, which are wires with connectors that interface well with various modules and microcontrollers. The jumper wires used in this chapter are listed in the “Materials” section of Chapter 1.
Jumper wires have a male and a female connector. The female connector connects to the exposed metal pins on the radio module, and the male connector gets plugged into the receptacles on the body of the microcontroller.
“GND” is the ground pin for the radio module.
“VCC” is the positive voltage supply pin for the radio module (expected to be around 3.3 V).
“CE” is the chip-enable pin.
“CSN” is the chip-select line that activates the radio module.
“SCK” is the SPI clock line.
“MOSI” is the master-out-slave-in line, which the microcontroller uses to send data to the radio module.
“MISO” is the master-in-slave-out line, which the radio module uses to send data to the microcontroller.
“IRQ” is an interrupt pin that we will not use in this example.
Now that you know what each of the pins on the radio module is, you can begin to connect them to the microcontroller with the jumper wires. The connections you’ll need to make are listed in Figure 6-6. For example, the figure indicates that you need to connect the VCC pin to the 3V3 pin, as the VCC label is next to the 3V3 pin.
If you did everything correctly, those should be the last connections you need to make. All the tinkering from here on out will be entirely software based! You can now go ahead and connect one of the boards to your computer via the included USB cable. Various lights should come on on the microcontroller as it powers up.
All the necessary settings should now be configured. Ensure that the Client.ino sketch is open, and click the “Upload” button at the top-left corner of the screen (this looks like an arrow pointing to the right).
During uploading, the lights on your microcontroller will flash, and the sketch will finish uploading after a few seconds. If you unplug and plug the microcontroller back in, this sketch will remain in the microcontroller’s memory, and it will begin executing the sketch from the beginning. There’s no need to ever reprogram the microcontroller unless you want to change something about the sketch.
- 1.
Unplug the first microcontroller from the computer.
- 2.
Place AA batteries in the battery pack, and then plug the battery pack into the connector on the microcontroller that you just unplugged from the computer.
- 3.
Some lights should flash as the batteries are connected, then the green power light should remain on. If nothing happens, check that you inserted the batteries into the battery holder correctly.
- 4.
Plug the second microcontroller into the computer, and follow the same steps for selecting the board and port as above.
- 5.
Load the Server.ino sketch.
- 6.
Upload Server.ino to the second microcontroller.
You should now have two programmed microcontrollers with radio modules attached. The microcontroller with the Client.ino sketch should be powered by batteries and disconnected from the computer. The microcontroller with the Server.ino sketch should be connected to the computer.
The “Sent Packet ID …” line indicates that the server radio module successfully sent data. The number increases with each packet. The “ACK Round Trip …” line indicates that the server received a response from the client. The round trip time, given in microseconds, is the time elapsed from when the server sent a message to when it received a response. The distance between radio modules is not directly measurable from this value. The maximum range of the radios is small, and it only takes radio waves around 1 nanosecond to travel one foot. Thus, in order to increase the round trip time by even a microsecond, the modules would need to be 1000 ft. apart. The time fluctuates much more than this even with stationary radios.
The line “TX Failed!” indicates that the radio module was unsuccessful in sending data to the other microcontroller. If the connections are incorrect between the microcontroller and the radio, you may receive this message. Or, if the connections are incorrect, you might not get any message at all. If something isn’t working, double- and triple-check all of the connections on both devices.
You can also get “TX Failed!” if the two radios are out of range. The RF24 library automatically resends each message a certain number of times if the other radio module doesn’t respond to indicate that the transmission was successful. This response is not handled in the client or server sketches; instead, the library does this behind the scenes. If the number of automatic resends elapses without a response, the RF24 library will indicate that a transmission failed.
“TX Failed!” is also accompanied by “Receive Timeout!” in the serial monitor output. The server sketch waits a certain period of time to receive a response before attempting to send a message to the client again. If this period of time elapses, the microcontroller will print the timeout message and attempt to send a message again.
The client sketch also has serial output. If you’re curious, you can connect the server to the battery pack and connect the client to the computer and observe its serial output. In order for the serial monitor to appear correctly, the microcontroller must be selected in the Arduino IDE’s port selection setting.
Experimentation
Now that the setup is out of the way, you can begin to experiment! Unfortunately, the RF24 library has no means of indicating the signal strength of received messages. Signal strength is a great way to observe how changes you make affect the communications system. A measurable quantity that allows you to indirectly observe signal strength is the round trip time. If the two radios are far enough apart, it will take many attempts to send the message and wait for a response before a message and acknowledgement gets through. This entire process takes time, so you will see an increase in round trip time due to this acknowledgement procedure as the radios move apart.
You can expect round trip times of less than 10,000 microseconds when the modules are close to one another and times between 10,000 and 40,000 microseconds when the modules are far apart.
The first thing I recommend trying is exploring the range of the pair of radios. You can do this by taking the client and placing it at increasing distances from the server until the connection drops completely. There’s no indication on the client of whether or not it’s connected to the server, so you’ll need to go back to your computer to see the status of the link. If you’re using a laptop, you can leave the client and carry the laptop around to see how the signal changes.
This is a good opportunity to explore antenna radiation patterns as well. Once you’re at the point where the signal drops out, try twisting the radio module in different ways to see if you can regain the connection. As mentioned, the antenna on the radio module has the highest gain to signals perpendicular the board. You will likely not observe any effects of polarization—rotating the board on some axes will have no effect. The signals from this board will appear cross-polarized, meaning the radio waves hitting the receiver are polarized both vertically and horizontally. The dominant effect here is the radiation pattern of the antenna.
Try placing obstacles in the path of the signal, and see how it changes signal reception. You can go so far as to almost completely wrap a radio module in tinfoil and still receive a signal. The small amount of energy that leaks out of the gaps in the tinfoil can still carry a signal to the other radio, as you will observe. If you try wrapping a radio in tinfoil, make sure that you surround it with paper first. The tinfoil can cause problems by shorting the metal contacts on the radio.
Software Modifications
RF24_PA_MIN
RF24_PA_LOW
RF24_PA_HIGH
RF24_PA_MAX
The power difference between MIN and MAX is obviously the greatest, so I recommend only switching between those two options. If you modify a sketch, you will need to save the sketch and re-upload it to the microcontroller. If you change the power level, you’ll need to do it on both microcontrollers to observe any changes.
If you’re comfortable with software, you can configure many more options on the radio such as data rate and auto-acknowledgement and see how these affect range. A full list of options is available on the RF24 library page: https://tmrh20.github.io/RF24/classRF24.html .
Summary
If all went well, you just implemented your first communications network! This small network is comprised of microcontrollers—processors that execute simple instructions—and radio modules. We programmed these microcontrollers through the Arduino IDE, using a library designed to facilitate communications with the radio modules.
This network is more of a proof of concept rather than a useful communications system. Data is sent for the purpose of exploring radio propagation and its effects. One would use these radio modules for any application that requires a short-range data link. They’re even capable of supporting data rates in excess of 1 million bits per second (Mbps), which is fast enough to transfer images in a reasonable amount of time.
It’s worth your time to try and expand these radios to fit a different application. The Arduino platform is a powerful way to explore radio communications, IoT, sensing, and so much more. There are numerous resources available that make it easy to learn how to create interesting projects with Arduino-based devices. In the next chapter, we’ll explore the world of amateur radio and how you can go about obtaining an amateur radio license.