Optimized high speed nRF24L01+ driver class documentation v1.4.10
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
examples/GettingStarted/GettingStarted.ino

Written by 2bndy5 in 2020

A simple example of sending data from 1 nRF24L01 transceiver to another.

This example was written to be used on 2 devices acting as "nodes". Use the Serial Monitor to change each node's behavior.

1/*
2 * See documentation at https://nRF24.github.io/RF24
3 * See License information at root directory of this library
4 * Author: Brendan Doherty (2bndy5)
5 */
6
13#include <SPI.h>
14#include "printf.h"
15#include "RF24.h"
16
17#define CE_PIN 7
18#define CSN_PIN 8
19// instantiate an object for the nRF24L01 transceiver
20RF24 radio(CE_PIN, CSN_PIN);
21
22// Let these addresses be used for the pair
23uint8_t address[][6] = { "1Node", "2Node" };
24// It is very helpful to think of an address as a path instead of as
25// an identifying device destination
26
27// to use different addresses on a pair of radios, we need a variable to
28// uniquely identify which address this radio will use to transmit
29bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
30
31// Used to control whether this node is sending or receiving
32bool role = false; // true = TX role, false = RX role
33
34// For this example, we'll be using a payload containing
35// a single float number that will be incremented
36// on every successful transmission
37float payload = 0.0;
38
39void setup() {
40
41 Serial.begin(115200);
42 while (!Serial) {
43 // some boards need to wait to ensure access to serial over USB
44 }
45
46 // initialize the transceiver on the SPI bus
47 if (!radio.begin()) {
48 Serial.println(F("radio hardware is not responding!!"));
49 while (1) {} // hold in infinite loop
50 }
51
52 // print example's introductory prompt
53 Serial.println(F("RF24/examples/GettingStarted"));
54
55 // To set the radioNumber via the Serial monitor on startup
56 Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
57 while (!Serial.available()) {
58 // wait for user input
59 }
60 char input = Serial.parseInt();
61 radioNumber = input == 1;
62 Serial.print(F("radioNumber = "));
63 Serial.println((int)radioNumber);
64
65 // role variable is hardcoded to RX behavior, inform the user of this
66 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
67
68 // Set the PA Level low to try preventing power supply related problems
69 // because these examples are likely run with nodes in close proximity to
70 // each other.
71 radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
72
73 // save on transmission time by setting the radio to only transmit the
74 // number of bytes we need to transmit a float
75 radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
76
77 // set the TX address of the RX node into the TX pipe
78 radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
79
80 // set the RX address of the TX node into a RX pipe
81 radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
82
83 // additional setup specific to the node's role
84 if (role) {
85 radio.stopListening(); // put radio in TX mode
86 } else {
87 radio.startListening(); // put radio in RX mode
88 }
89
90 // For debugging info
91 // printf_begin(); // needed only once for printing details
92 // radio.printDetails(); // (smaller) function that prints raw register values
93 // radio.printPrettyDetails(); // (larger) function that prints human readable data
94
95} // setup
96
97void loop() {
98
99 if (role) {
100 // This device is a TX node
101
102 unsigned long start_timer = micros(); // start the timer
103 bool report = radio.write(&payload, sizeof(float)); // transmit & save the report
104 unsigned long end_timer = micros(); // end the timer
105
106 if (report) {
107 Serial.print(F("Transmission successful! ")); // payload was delivered
108 Serial.print(F("Time to transmit = "));
109 Serial.print(end_timer - start_timer); // print the timer result
110 Serial.print(F(" us. Sent: "));
111 Serial.println(payload); // print payload sent
112 payload += 0.01; // increment float payload
113 } else {
114 Serial.println(F("Transmission failed or timed out")); // payload was not delivered
115 }
116
117 // to make this example readable in the serial monitor
118 delay(1000); // slow transmissions down by 1 second
119
120 } else {
121 // This device is a RX node
122
123 uint8_t pipe;
124 if (radio.available(&pipe)) { // is there a payload? get the pipe number that received it
125 uint8_t bytes = radio.getPayloadSize(); // get the size of the payload
126 radio.read(&payload, bytes); // fetch payload from FIFO
127 Serial.print(F("Received "));
128 Serial.print(bytes); // print the size of the payload
129 Serial.print(F(" bytes on pipe "));
130 Serial.print(pipe); // print the pipe number
131 Serial.print(F(": "));
132 Serial.println(payload); // print the payload's value
133 }
134 } // role
135
136 if (Serial.available()) {
137 // change the role via the serial monitor
138
139 char c = toupper(Serial.read());
140 if (c == 'T' && !role) {
141 // Become the TX node
142
143 role = true;
144 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
145 radio.stopListening();
146
147 } else if (c == 'R' && role) {
148 // Become the RX node
149
150 role = false;
151 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
152 radio.startListening();
153 }
154 }
155
156} // loop
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:116
@ RF24_PA_LOW
Definition RF24.h:50
#define delay(millisec)