Escolar Documentos
Profissional Documentos
Cultura Documentos
As nRF24L01+ transceiver module require a lot of data transfer, they will give the best
performance when connected up to the hardware SPI pins on a microcontroller. The
hardware SPI pins are much faster than ‘bit-banging’ the interface code using another set
of pins.
Note that each Arduino Board has different SPI pins which should be connected accordingly.
For Arduino boards such as the UNO/Nano V3.0 those pins are digital 13 (SCK),
12 (MISO) and 11 (MOSI).
If you have a Mega, the pins are different! You’ll want to use digital 50 (MISO), 51 (MOSI),
52 (SCK), and 53 (SS). Refer below table for quick understanding.
UNO 11 12 13
NANO 11 12 13
MEGA 51 50 52
PINOUT CONNECTIONS
Arduino
nRF24L01P
UNO
GND GND 1
VCC 3.3V 2
CE 9 3
CSN 8 4
SCK 13 5
MOSI 11 6
MISO 12 7
GND is the Ground Pin. It is usually marked by encasing the pin in a square so it can be used
as a reference for identifying the other pins.
VCC supplies power for the module. This can be anywhere from 1.9 to 3.9 volts. You can
connect it to 3.3V output from your Arduino. Remember connecting it to 5V pin will likely
destroy your nRF24L01+ module!
CE (Chip Enable) is an active-HIGH pin. When selected the nRF24L01 will either transmit
or receive, depending upon which mode it is currently in.
CSN (Chip Select Not) is an active-LOW pin and is normally kept HIGH. When this pin goes
low, the nRF24L01 begins listening on its SPI port for data and processes it accordingly.
SCK (Serial Clock) accepts clock pulses provided by the SPI bus Master.
MOSI (Master Out Slave In) is SPI input to the nRF24L01.
MISO (Master In Slave Out) is SPI output from the nRF24L01.
IRQ is an interrupt pin that can alert the master when new data is available to process.
il modulo nRF24L01P usa SPI per comunicare, la libreria TMRH20 nRF24 semplifica la
gestione del modulo. Tale libreria dipende dalla libreria SPI, quindi occorre includere anche
la libreria SPI nello sketch.
#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
radio.setDataRate(RF24_1MBPS);
nonchè i Retries nel caso di mancato ACK, 5 retries distanziati di (3+1)*250 usec
radio.setRetries(3,5);
Codice del trasmettitore TX con AutoACK abilitato
#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
int msg[1] = {1};
int rec[1] = {5};
bool stat = true;
RF24 radio(9,8); // CE, CSN pins
const uint64_t pipe[1] = {0xF0F0F0F0E1LL};
void setup()
{
Serial.begin(9600);
radio.begin();
delay(100);
radio.setAutoAck(true);
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.setDataRate(RF24_1MBPS);
radio.setChannel(100); //sets frequency to channel 100
radio.stopListening();
radio.openWritingPipe(pipe[0]);
radio.setRetries(3,5); // 5 retries distanced by 4*250usec
}
void loop()
{
if(stat)
{
if(radio.write(msg,sizeof(msg)))
{
Serial.print( msg[0] );
Serial.println("...tx success");
if(radio.isAckPayloadAvailable())
{
radio.read(rec,sizeof(int));
Serial.print("received ACK payload is : ");
Serial.println(rec[0]);
}
else
{
stat = false; //doing this completely shuts down the
transmitter if an ACK payload is not received !!
Serial.println("status has become false so stop
here....");
}
msg[0]+=3;;
if(msg[0]>=100)
{msg[0]=1;}
delay(1000);
}
}
}