© Alex Wulff 2019
A. WulffBeginning Radio Communicationshttps://doi.org/10.1007/978-1-4842-5302-1_6

6. Exploring Radio

Alex Wulff1 
(1)
Cambridge, MA, USA
 
While you’re waiting to take your licensing test, you can read this chapter and complete the exercises to learn more about how radio technology can be applied in the world of digital communications. The weather satellite chapter (Chapter 4) may have been your first experience directly receiving a radio transmission, but in this chapter, you will gain a deeper insight into how radio communication also serves as a great opportunity to explore how various obstacles in the path of radio waves can affect signal quality and strength. The setup we’ll use is shown in Figure 6-1. If you have not yet purchased the materials for this chapter, now is a good time to review the list in Chapter 1 and do so.
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig1_HTML.jpg
Figure 6-1

The microcontroller and radio setup used in this chapter

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.

A microcontroller normally has a microprocessor which actually executes the instructions and various supporting peripherals such as devices to regulate power supply, status lights, oscillators, and more. A typical microcontroller is shown in Figure 6-2. This particular variant of a microcontroller is manufactured by a company called Arduino. Arduino produces various hardware devices for hobbyists that are easy to learn how to program. Before Arduino, programming microcontrollers was left to industry professionals with years of experience. Arduino also produces software to allow individuals to program their microcontrollers with ease—this software has been adapted to support the programming of hundreds of different microcontroller designs produced by many different companies. We’ll use Arduino software later to program the microcontrollers used in this chapter.
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig2_HTML.jpg
Figure 6-2

The Arduino Uno

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.

Now that you have the IDE installed, we’ll need to install the library that allows us to interface with the radio module. The steps to do this are listed as follows:
  1. 1.
    Go to Sketch ➤ Include Library ➤ Manage Libraries (as shown in Figure 6-3).
    ../images/483418_1_En_6_Chapter/483418_1_En_6_Fig3_HTML.jpg
    Figure 6-3

    The menu option to open the library manager

     
  2. 2.

    Search “RF24” in the window that appears, and find the library named “RF24” (by user TMRh20). You may need to scroll down.

     
  3. 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.

     
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig4_HTML.jpg
Figure 6-4

The library manager screen with the RF24 library selected. Mine looks slightly different because I already have the library installed

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.

I have created software for this chapter that you can download from www.AlexWulff.com/radiobook/code.zip . Your computer should automatically download the file. After downloading, unzip the file and extract its contents to a directory of your choosing. Inside are two folders, “Client” and “Server.” Inside each of these folders is a file of the same name ending in “.ino”. This is the file extension that Arduino expects its software to have—double-clicking a .ino file opens up the Arduino IDE automatically. You can also open .ino files in Arduino by going to File ➤ Open. Arduino-compatible software, or a “sketch” as it’s called, is written in C++. C++ is a popular programming language; if you’re familiar with it, you’ll have no trouble understanding the sketches used in this chapter. Listings 6-1 and 6-2 provide the sketches for reference.
// Code created by Alex Wulff
// www.AlexWulff.com
#include <SPI.h>
#include <RF24.h>
// CE, CSN
RF24 radio(9, 10);
int packetID = 0;
const byte addresses[][6] = { "server", "client" };
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1,addresses[0]);
  radio.setPALevel(RF24_PA_MIN);
}
void loop() {
  packetID++; char message[50];
  sprintf(message,"Packet ID: %d",packetID);
  unsigned long sendTime = micros();
  radio.stopListening();
  bool stat = radio.write(&message, sizeof(message));
  if (stat == 0) {Serial.println("TX Failed!");}
  else {Serial.print("Sent Packet ID: "); Serial.println(packetID);}
  delay(5);
  radio.startListening();
  bool timeout = false;
  while(!radio.available()) {
    if (micros() - sendTime > 300000) {
      timeout = true;
      Serial.println("Receive Timeout!");
      break;
    }
  }
  if (!timeout) {
    char received[50];
    radio.read(&received, sizeof(received));
    unsigned long roundTripTime = micros() - sendTime;
    Serial.print("ACK Round Trip Time: ");
    Serial.print(roundTripTime);
    Serial.print(" | Message: ");
    Serial.println(received);
  }
  delay(500);
}
Listing 6-1

Server.ino

The server sketch attempts to send a message and then listens for a reply from the client. If a certain period of time elapses before the server receives a message from the client, the server will time out and attempt to send a message again.
#include <SPI.h>
#include <RF24.h>
 // CE, CSN
RF24 radio(9, 10);
int packetID = 0;
const byte addresses[][6] ={ "server", "client" };
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, addresses[1]);
  radio.openWritingPipe(addresses[0]);
  radio.setPALevel(RF24_PA_MIN);
}
void loop() {
  packetID++; delay(10);
  radio.startListening();
  if (radio.available()){
    char received[32] = "";
    radio.read(&received, sizeof(received));
    Serial.println(received);
  }
  delay(5);
  radio.stopListening();
  char toSend[32];
  sprintf(toSend,"Receiver ACK: %d",packetID) ;
  radio.write(&toSend, sizeof(toSend));
  delay(500);
}
Listing 6-2

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.

Figure 6-5 lists each of the pins on the radio module. We’ll decode these below, although you don’t really need to know what each does for the purposes of this chapter:
  • “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.

../images/483418_1_En_6_Chapter/483418_1_En_6_Fig5_HTML.jpg
Figure 6-5

A listing of the pins on the radio module

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.

A radio communications system is useless with just one radio—make the same connections to another radio module with the second microcontroller as you do the first, so you have a pair of transceivers.
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig6_HTML.jpg
Figure 6-6

What connections to make with the microcontroller

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.

The Arduino IDE needs to know what board you’re using. With the Client.ino sketch open, go to Tools ➤ Board ➤ Arduino/Genuino Uno. This is shown in Figure 6-7. Your board window will likely look different than mine.
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig7_HTML.jpg
Figure 6-7

Arduino IDE board selection menu

Next, the Arduino IDE needs to know what port on your computer the board is attached to. Go to Tools ➤ Port, and select your Uno. On macOS, it should appear similar to the port shown in Figure 6-8. On Windows, the board will be listed as something similar to “COM3 (Arduino/Genuino Uno).” The number after “COM” will likely be different.
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig8_HTML.jpg
Figure 6-8

Arduino IDE port selection menu

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.

If you encounter any issues with programming, Arduino has a support article online to help: www.arduino.cc/en/guide/troubleshooting#toc1 . Common issues include not having the correct port selected and not having the correct board selected. We’ll now upload the server sketch to the other microcontroller.
  1. 1.

    Unplug the first microcontroller from the computer.

     
  2. 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. 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. 4.

    Plug the second microcontroller into the computer, and follow the same steps for selecting the board and port as above.

     
  5. 5.

    Load the Server.ino sketch.

     
  6. 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.

We’ll now verify that your setup is working. The Arduino IDE has something called a serial monitor that can receive information from the microcontroller. Every time “Serial.print” gets called in the code, information is sent to the serial monitor. This serial monitor is an easy way to verify that everything is working correctly. To open the serial monitor, click the icon at the upper-right corner of the IDE. This is shown in Figure 6-9.
../images/483418_1_En_6_Chapter/483418_1_En_6_Fig9_HTML.jpg
Figure 6-9

How to open the Arduino IDE’s serial monitor

When the microcontrollers are correctly sending data to one another, an output such as the one in the following should appear in the serial monitor:
Sent Packet ID: 1551
ACK Round Trip Time: 8776 | Message: Receiver ACK: 1542
Sent Packet ID: 1552
ACK Round Trip Time: 8772 | Message: Receiver ACK: 1543
Sent Packet ID: 1553
ACK Round Trip Time: 8776 | Message: Receiver ACK: 1544
Sent Packet ID: 1554
ACK Round Trip Time: 8772 | Message: Receiver ACK: 1545
Sent Packet ID: 1555
ACK Round Trip Time: 10724 | Message: Receiver ACK: 1546

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.

It’s also entirely likely that your setup does not work the first time you try it. An unsuccessful output will look similar to the one as follows:
TX Failed!
Receive Timeout!
TX Failed!
Receive Timeout!
TX Failed!
Receive Timeout!
TX Failed!

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

These radios contain hardware called a power amplifier that can boost the power of the signal emitted from the radio. The level of amplification is software configurable. The client and server sketches set this amplification to the minimum possible level in the line shown as follows:
radio.setPALevel(RF24_PA_MIN);
You can modify the RF24_PA_MIN value to one of the following options:
  • 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.141.44