For this week we have to design and build a wired and/or wireless network connecting at least two processors.

Given the need for my final project to communicate 2 devices located in different places, I decided to start testing some type wireless communication.

Looking on the internet I found a possible option: the nRF24L01 module. These modules are radio frequency transceiver to 2.4Ghz (the same as WiFi) really very cheap: they do not offer the same advanced features of the wifi, but are suitable for my needs.

nRF24L01

Responsive image

Responsive image

The advantages of this module are:

Between all the libraries I've tested (Mirf - MySensors NRF24L01 Arduino Library - RadioHead Packet Radio Library) the most complete and easy to use is the RF24.

This library, as well as being well documented, there are a number of examples to better understand the operation of the module nRF24L01.

First of all, we have to connect the nRF24 to our microcontroller boar, following this schematic:

Responsive image

nRF24L01 pin ----> Microcontroller pin

*CE and CSN pin are defined by software.
** Pin 8 IRQ is unused by most software, but the RF24 library has an example that utilizes it.

to use this module of course we will need a current regulator to be able to properly power to 3.3 V.

Responsive image

The first test we can do is upload the sketch "GettingStarted" library RF24 on both microcontrollers that we want to communicate, defining the roles of each in the same sketch. This is a very basic example of how to send data from one node to another.

Responsive image

Set role = 0 in one microcontroller

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving
bool role = 0;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/examples/GettingStarted"));
  Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  
  radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
 // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);
  
  // Open a writing and reading pipe on each radio, with opposite addresses
  if(radioNumber){
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  }else{
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
  }
  
  // Start the radio listening for data
  radio.startListening();
}


and set role = 1 in the other

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving
bool role = 1;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/examples/GettingStarted"));
  Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  
  radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
 // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);
  
  // Open a writing and reading pipe on each radio, with opposite addresses
  if(radioNumber){
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  }else{
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
  }
  
  // Start the radio listening for data
  radio.startListening();
}
            


Once loaded the sketch in two microcontrollers, we start the serial monitor and we should see, depending on which microcontroller is connected to the PC, the bits that are transmitted or received.

here the two screens (transmitter and receiver) compared

Responsive image

Another example in the library, it can give you a visual idea of wireless communication, is "LedRemote".

This is an example of how to use the RF24 class to control a remote bank of LED's using buttons on a remote control. On the 'remote', connect any number of buttons or switches from a microcontroller pin to ground. Update 'button_pins' to reflect the pins used.

// Pins on the remote for buttons
const uint8_t button_pins[] = { 2,3,4};
const uint8_t num_button_pins = sizeof(button_pins);


On the 'led' board, connect the same number of LED's from an microcontroller pin to a resistor to ground. Update 'led_pins' to reflect the pins used.

// Pins on the LED board for LED's
const uint8_t led_pins[] = { 2,3,4};
const uint8_t num_led_pins = sizeof(led_pins);


Also connect a separate pin to ground and change the 'role_pin'. This tells the sketch it's running on the LED board.

// sets the role of this unit in hardware.  Connect to GND to be the 'led' board receiver
// Leave open to be the 'remote' transmitter
const int role_pin = A4;


Every time the buttons change on the remote, the entire state of buttons is send to the led board, which displays the state.

Responsive image

Once loaded the scketch, always open the serial monitor and control the transmission of data. This is the start-up message

Responsive image

and these ones when sending data (ie the pressure of a button)

Responsive image

This is a video while the 2 micro are working: