Say Goodbye to Zigbee, and Say Hello to Nrf24l01. (Tutorial 1)

Posted by: admin Category: Network Tags: , , , Comments: 25

Say Goodbye to Zigbee, and Say Hello to Nrf24l01. (Tutorial 1)

Zigbee is a good wireless solution for arduino long time, however, it is expensive. NRf24l01 only $1.5 per piece at Electrodragon, you can find it here. Not only cheap, this module is also high speed (2Mbps), Low power consumption (1uA at standby mode)  and quite far communication distance (20-30 meters), you will like it! The tutorial and code is available here!

How to Say hello to another Nrf24l01?

We use library mirf which is easy for new user to start.

Step 1. you will always need to include the libary as follow

[c]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
[/c]

Step 2.  Setup up for each Nrf24l01, this is also the same for almost any Nrf24l01. The payload is just set to the maximum length 32 bytes. SPI also set up.

[c]
void setup(){
Serial.begin(9600);
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setTADDR((byte *)"serv1"); //TADDR should change into RADDR if it is a receive
Mirf.payload = 32;
Mirf.config();
Serial.println("Beginning … "); // or "Listening …" on sever
}
[/c]

Step 3. Send Hello in bytes from transmitter to other one in every 0.5 second, this is a simple loop part in transmitter. The char “hello” is sent.
[c]
void loop(){ 
Mirf.send((byte *) "Hello");
delay(500);
}
[/c]
The code of the same meaning, remember array is zero indexed, so the array size should always plus one
[c]
void loop(){
char a[6] ="abced";
Mirf.send((byte *) a);
delay(500);
[/c]
And if you want send int format information, the code is
[c]
void loop(){ 
int a[6] = {2, 4, -8, 3, 2};
Mirf.send((byte *) a);
delay(500);
}
[/c]
Step 4. Now, let receive our data on the receiver module, simply set a byte array of 32 bytes length, when data is not sending and data is ready,

we receive it and print it in byte format.
[c]
void loop(){
byte data[32];
if(!Mirf.isSending(); Mirf.dataReady()){
Serial.println("Got packet");
Mirf.getData((byte *); data);
Serial.print(data[0], BYTE); //h
Serial.print(data[1], BYTE); //e
Serial.print(data[2], BYTE); //l
Serial.print(data[3], BYTE); //l
Serial.print(data[4], BYTE); //o
Serial.println("");
}
}
[/c]
If you send int format data, the receive code should be
[c]
void loop(){
int data[32]; // here is int format
if(!Mirf.isSending(); Mirf.dataReady()){
Serial.println("Got packet");
Mirf.getData((byte *); data);
Serial.print(data[0]); //2
Serial.print(data[1]); //4
Serial.print(data[2]); //-8
Serial.print(data[3]); //3
Serial.print(data[4]); //2
Serial.println("");
}
}
[/c]
Now you know the data is transferring between Nrf24l01 as binary, and you can make a data array to get it and print it in byte format.

This is just the most simple communication function for this module, more functions and tutorials will be available soon.

Share this post

Comments (25)

Leave a Reply

Your email address will not be published. Required fields are marked *