NRF24L01-Mirf

From ElectroDragon Wiki

Progarmming Use Mirf

  • Mirf
  • Download libraryMirf for arduino and install it.
  • Connect nrf24l01 module to arduino as follow, add logic shifter for 5V-3V3, Power by 3V3:

DI 12 - MISO, DO 11 - MOSI, SCK 13 - SCK, CS 9 - CE, 3V3 - VCC, GND - GND, NC - IRQ Demo codes

  • Setup, same for sender and receiver)
//first included your library
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
void setup(){
   Serial.begin(9600);
    Mirf.spi = &MirfHardwareSpi;
    //Mirf.csnPin = 10; (This is optional to change the chip select pin)
    //Mirf.cePin = 9; (This is optional to change the enable pin)
    Mirf.init(); 
    Mirf.setTADDR((byte *)"serv1");
    Mirf.payload = 32;
    Mirf.config();
    Serial.println("Beginning ... "); // "Beginning ..." on sender, or "Listening ..." on sever (Receiver)
  }

The completed Demo Code

Sender Receiver
  • Send string in char directly
 
void loop(){ 
    Mirf.send((byte *) "Hello");
    delay(500);
}


  • Send string in char (Optional writing)
 
void loop(){ 
    char a[6] ="abced"; // char
    Mirf.send((byte *) a);
    delay(500);
}
  • Or:
 
void loop(){ 
    int Vals[6] = {2, 4, -8, 3, 2};
    Mirf.send((byte *) a);
    delay(500);
}
Receive standard 32 Bytes
 
void loop(){ 
     byte data[32]; // or int data[32];
     if(!Mirf.isSending() && Mirf.dataReady()){
          Serial.println("Got packet");
          Mirf.getData((byte *) &data);
          Serial.write(byte(data[0])); //h
          Serial.write(byte(data[1])); //e
          Serial.write(byte(data[2])); //l
          Serial.write(byte(data[3])); //l
          Serial.write(byte(data[4])); //o
          Serial.println("");
     }
}


  • Receive more than 32 bytes
 
Mirf.getData(data);
int i;
for (i = 0; i < sizeof(data); i++)
    {
    Serial.println(data[i]); //Serial.print(data[i], DEC);
    }
 //first included your library
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
void setup(){
   Serial.begin(9600);
    Mirf.spi = &MirfHardwareSpi;
    Mirf.csnPin = 10; //(This is optional to change the chip select pin)
    Mirf.cePin = 9; //(This is optional to change the enable pin)
    Mirf.init(); 
    Mirf.setTADDR((byte *)"serv1");
    Mirf.payload = 32;
    Mirf.config();
    Serial.println("Beginning ... "); // "Beginning ..." on sender, or "Listening ..." on sever (Receiver)
  }
  
  void loop(){ 
    Mirf.send((byte *) "Hello");
    delay(500);
}
 //first included your library
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

void setup(){
   Serial.begin(9600);
    Mirf.spi = &MirfHardwareSpi;
    Mirf.csnPin = 10; //(This is optional to change the chip select pin)
    Mirf.cePin = 9; //(This is optional to change the enable pin)
    Mirf.init(); 
    Mirf.setTADDR((byte *)"serv1");
    Mirf.payload = 32;
    Mirf.config();
    Serial.println("Beginning ... "); // "Beginning ..." on sender, or "Listening ..." on sever (Receiver)
  }
  
void loop(){ 
     byte data[32]; // or int data[32];
     if(!Mirf.isSending() && Mirf.dataReady()){
          Serial.println("Got packet");
          Mirf.getData((byte *) &data);
          Serial.write(byte(data[0])); //h
          Serial.write(byte(data[1])); //e
          Serial.write(byte(data[2])); //l
          Serial.write(byte(data[3])); //l
          Serial.write(byte(data[4])); //o
          Serial.println("");
     }
}

Result Turn on the serial port on receiver arduino, it will show up:

Beginning ... 
Got packet
Hello
Got packet
Hello
Got packet
Hello
Got packet
Hello
  • During the "beginning ..." line, press the restart button on the sender arduino, then reset the board and let it start to send data.

Properties:

byte Function Default Note
cePin CE Pin controls RX / TX 8 -
csnPin CSN Pin (Chip select not) 7 -
channel RF Channel 0 - 127 or 0 - 84 in the US 0 -
payload Size in bytes Default 16, max 32 Channel and payload must be the same for all nodes.

Functions

Function Decsription example
void init(void) Initialize the module, set the pin modes for the configurable pins and initialize the SPI module. Mirf.init();
void setRADDR(byte *addr) Set the receiving address. Addresses are 5 bytes long. Mirf.setRADDR((byte *)"addr1");
void setTADDR(byte *addr) Set the sending address. Mirf.setTADDR((byte *)"addr1");
void config(void) Set channel and payload width. Power up in RX mode and flush RX fifo. Mirf.payload = 32;
Mirf.channel = 2;
Mirf.config();
bool dataReady(void) Is there data ready to be received? if(Mirf.dataReady()){
//Get the data to play with.
}
void getData(byte *data) Get the received data. 'data' should be an array of bytes Mirf.payload long. byte data[Mirf.payload]
Mirf.getData(data);
void send(byte *data) Send data. 'data' should be Mirf.payload bytes long. -
bool isSending(void) Return true if still trying to send. If the chip is still in transmit mode then this method will return the chip to receive mode. Mirf.send(data);
while(Mirf.isSending()){
//Wait.
} //Chip is now in receive mode.
NB: Lots more information is available from the status registers regarding acknolagement or failure status. See Mirf.cpp:218.
bool rxFifoEmpty(void) Is the RX Fifo Empty. -
bool txFifoEmpty(void) Is the TX Fifo Empty. -
byte getStatus(void) Return the status register. -
void powerUpRx(void) Power up chip and set to receive mode. Also clear sending interrupts. -
void powerUpTx(void) Power up tx mode. -