One part of the project that’s bothered me is the use of the 433Mhz equipment I originally bought for it. Don’t get me wrong, it’s pretty good – with 12V to the transmitter module, you can achieve respectable range – however, to do that you need a boost convertor to get the 12V from your 5V power supply, plus you’ll also need tuned lengths of cable for the transmitter and receiver. It’s all a bit……….tedious.
So after watching some videos on Arduino communications on Julian Ilett’s excellent Youtube channel, I decided I’d see how I got on with the Nordic Semiconductor’s nRF24L01 communications equipment. I’ve used the information in this excellent guide at the Arduino-Info site (link to be added).
Anyway, borrowing from a couple of sources (the code in the guide as well as the example code in the TMRh20 library mentioned in the guide), I hacked together this working code to test communications between a transmitter and receiver. FYI, I’m using the high-power tranceivers (with PA+LNA) at both TX & RX complete with the base modules mentioned in the guide.
Transmitter:
#include <SPI.h> // Comes with Arduino IDE
#include <RF24.h> // Download and Install (See above)
#include <nRF24L01.h>
RF24 myRadio (7, 8);
byte addresses[6] = {“1Node”};
int tx_data;
int hilo[1];
void setup(){
hilo[0] = 0;
myRadio.begin();
myRadio.setChannel(108);
myRadio.openWritingPipe(addresses[0]);
delay(50);
}
void loop(){
if (hilo[0] == 0){
myRadio.write(&hilo, sizeof(hilo));
hilo[0] = 1;
}
else {
myRadio.write(&hilo, sizeof(hilo));
hilo[0] = 0;
}
delay(1000);
}
The TX code just continually switches the state of the variable ‘hilo’ between 0 and 1 and back with a 1 second delay between transmissions. Any other nRF24L01 receiver using the same RF24 library, channel and ‘pipe’ address will pick this data up.
My receiver code:
#include <SPI.h> // Comes with Arduino IDE
#include <RF24.h> // Download and Install (See above)
#include <nRF24L01.h>
RF24 myRadio (7, 8);
byte addresses[6] = {“1Node”};
int rx_data;
int hilo[1];
void setup(){
pinMode(3, OUTPUT);
hilo[0] = 0;
myRadio.begin();
myRadio.setChannel(108);
myRadio.openReadingPipe(1, addresses[0]);
myRadio.startListening();
delay(50);
}
void loop(){
recvlstate();
if (hilo[0] == 0){
digitalWrite(3, LOW);
}
if (hilo[0] == 1){
digitalWrite(3, HIGH);
}
}
void recvlstate(){
if (myRadio.available()) // While there is data ready
{
myRadio.read( &hilo, sizeof(hilo) ); // Get the data payload (You must have defined that already!)
}
}
The receiver code just takes in the transmitted variable and sends an output pin (Arduino pin 3 in this case) high or low depending on the 0 or 1 state of the ‘hilo’ variable. I’ve had this code working between a transmitter & 2 receivers, so the next step is to integrate the use of these devices into the main project.