nrf24l01 - R2D2-2019/R2D2-2019 GitHub Wiki
The nRF24L01+ is an inexpensive 2.4 GHz wireless transceiver that interfaces with many microcontrollers.
The nRF24L01+ is a half-duplex transceiver that you can connect to your Arduino, Raspberry Pi, or other microcontroller to send bi-directional information. It operates in the 2.4 GHz ISM (Industrial, Scientific, and Medical) band.
The pros are that it works well, has low power consumption, is easy to use, and is an extremely inexpensive way to send and receive information. You can power it directly off of the Arduino's 3.3V regulated output, or the 5V regulated output if you use the base.
The cons are that it uses several IO pins to function as intended, it cannot send/receive simultaneously, the 2x4 8 position header is minorly inconvenient to use, and VDD cannot exceed 3.6 V, with 3.0 recommended, and the range is somewhat limited.
however the pros far outweigh the cons.
Below is an example how to quickly send a message with the nRF24L01+ using the RF24 library.
//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//create an RF24 object
RF24 radio(9, 8); // CE, CSN
//address through which two modules communicate.
const byte address[6] = "00001";
void setup()
{
radio.begin();
//set the address
radio.openWritingPipe(address);
//Set module as transmitter
radio.stopListening();
}
void loop()
{
//Send message to receiver
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);
}
- download https://github.com/nRF24/RF24 library
- Set up two Arduinos with nRF24L01+ wired to them.
- Copy the code below into a sketch.
- Change line 15 to "RF24 radio(9,10);"
- Upload the Code to the first Arduino with line 12 reading "bool radioNumber = 0;"
- Upload the Code to the second Arduino with line 12 reading "bool radioNumber = 1;"
- Then open two terminals: one to connect to the first Arduino and one to connect to the second. Type "T" as instructed in one terminal. Numbers will start bouncing back and forth between the two transceivers. This lets you know that you've hooked up everything correctly and that everything is working. Make sure everything is connected properly.
#include <SPI.h>
#include <RF24.h>
/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 0;
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(9,10);
/**********************************************************/
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();`
}
void loop() {
`/****************** Ping Out Role ***************************/`
`if (role == 1) {`
`radio.stopListening(); // First, stop listening so we can talk.`
`Serial.println(F("Now sending"));`
`unsigned long start_time = micros(); // Take the time, and send it. This will block until complete`
`if (!radio.write( &start_time, sizeof(unsigned long) )){`
`Serial.println(F("failed"));`
`}`
`radio.startListening(); // Now, continue listening`
`unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds`
`boolean timeout = false; // Set up a variable to indicate if a response was received or not`
`while ( ! radio.available() ){ // While nothing is received`
`if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop`
`timeout = true;`
`break;`
`}`
`}`
`if ( timeout ){ // Describe the results`
`Serial.println(F("Failed, response timed out."));`
`}else{`
`unsigned long got_time; // Grab the response, compare, and send to debugging spew`
`radio.read( &got_time, sizeof(unsigned long) );`
`unsigned long end_time = micros();`
`// Spew it`
`Serial.print(F("Sent "));`
`Serial.print(start_time);`
`Serial.print(F(", Got response "));`
`Serial.print(got_time);`
`Serial.print(F(", Round-trip delay "));`
`Serial.print(end_time-start_time);`
`Serial.println(F(" microseconds"));`
`}`
`// Try again 1s later`
`delay(1000);`
`}`
`/****************** Pong Back Role ***************************/`
`if ( role == 0 )`
`{`
`unsigned long got_time;`
`if( radio.available()){`
`// Variable for the received timestamp`
`while (radio.available()) { // While there is data ready`
`radio.read( &got_time, sizeof(unsigned long) ); // Get the payload`
`}`
`radio.stopListening(); // First, stop listening so we can talk`
`radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.`
`radio.startListening(); // Now, resume listening so we catch the next packets.`
`Serial.print(F("Sent response "));`
`Serial.println(got_time);`
`}`
`}`
`/****************** Change Roles via Serial Commands ***************************/`
`if ( Serial.available() )`
`{`
`char c = toupper(Serial.read());`
`if ( c == 'T' && role == 0 ){`
`Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));`
`role = 1; // Become the primary transmitter (ping out)`
`}else`
`if ( c == 'R' && role == 1 ){`
`Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));`
`role = 0; // Become the primary receiver (pong back)`
`radio.startListening();`
`}`
`}`
}
in deze module laten we een joystick praten met Servos doormidden van de nrf24io1, hieronder de fritzing voor de joystick arduino. Fritzing model
Hier de fritzing voor de servo arduino.Fritzing Servos
module code, don't forget to download the RF24 library!