Difference between revisions of "Nrf24L01-2.4GHz-ExampleSketches"

From ArduinoInfo
Jump to: navigation, search
m
m
Line 90: Line 90:
 
==nRF24L01-SimpleReceive-01==  
 
==nRF24L01-SimpleReceive-01==  
  
[[media type="custom" key="28252771"]]
+
<pre>
 +
<font color="#95A5A6">/* YourDuinoStarter Example: Simple nRF24L01 Receive</font>
 +
<font color="#95A5A6">  - WHAT IT DOES: Receives simple fixed data with nRF24L01 radio</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  Start with radios about 4 feet apart.</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  - CONNECTIONS: nRF24L01 Modules See:</font>
 +
<font color="#95A5A6">  </font><u><font color="#95A5A6">http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo</font></u>
 +
<font color="#95A5A6">  Uses the RF24 Library by TMRH2o here:</font>
 +
<font color="#95A5A6">  </font><u><font color="#95A5A6">https://github.com/TMRh20/RF24</font></u>
 +
<font color="#95A5A6">  1 - GND</font>
 +
<font color="#95A5A6">  2 - VCC 3.3V !!! NOT 5V</font>
 +
<font color="#95A5A6">  3 - CE to Arduino pin 7</font>
 +
<font color="#95A5A6">  4 - CSN to Arduino pin 8</font>
 +
<font color="#95A5A6">  5 - SCK to Arduino pin 13</font>
 +
<font color="#95A5A6">  6 - MOSI to Arduino pin 11</font>
 +
<font color="#95A5A6">  7 - MISO to Arduino pin 12</font>
 +
<font color="#95A5A6">  8 - UNUSED</font>
 +
 
 +
<font color="#95A5A6">  V1.02 02/06/2016</font>
 +
<font color="#95A5A6">  Questions: terry@yourduino.com */</font>
 +
 
 +
<font color="#95A5A6">/*-----( Import needed libraries )-----*/</font>
 +
<font color="#5E6D03">#include</font> <font color="#434F54">&lt;</font><b><font color="#D35400">SPI</font></b><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">&gt;</font>  <font color="#434F54">// Comes with Arduino IDE</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"RF24.h"</font>  <font color="#434F54">// Download and Install (See above)</font>
 +
<font color="#95A5A6">/*-----( Declare Constants and Pin Numbers )-----*/</font>
 +
<font color="#434F54">//None yet</font>
 +
<font color="#95A5A6">/*-----( Declare objects )-----*/</font>
 +
<font color="#434F54">// (Create an instance of a radio, specifying the CE and CS pins. )</font>
 +
<font color="#000000">RF24</font> <font color="#000000">myRadio</font> <font color="#000000">(</font><font color="#000000">7</font><font color="#434F54">,</font> <font color="#000000">8</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// "myRadio" is the identifier you will use in following methods</font>
 +
<font color="#95A5A6">/*-----( Declare Variables )-----*/</font>
 +
<font color="#00979C">byte</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">]</font><font color="#000000">[</font><font color="#000000">6</font><font color="#000000">]</font> <font color="#434F54">=</font> <font color="#000000">{</font><font color="#00979C">"1Node"</font><font color="#000000">}</font><font color="#000000">;</font> <font color="#434F54">// Create address for 1 pipe.</font>
 +
<font color="#00979C">int</font> <font color="#000000">dataReceived</font><font color="#000000">;</font>  <font color="#434F54">// Data that will be received from the transmitter</font>
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">setup</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** SETUP: RUNS ONCE ******/</font>
 +
<font color="#000000">{</font>
 +
  <font color="#434F54">// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"RF24/Simple Receive data Test"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Questions: terry@yourduino.com"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Start up the physical nRF24L01 Radio</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setChannel</font><font color="#000000">(</font><font color="#000000">108</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Above most Wifi Channels</font>
 +
  <font color="#434F54">// Set the PA Level low to prevent power supply related issues since this is a</font>
 +
  <font color="#434F54">// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setPALevel</font><font color="#000000">(</font><font color="#000000">RF24_PA_MIN</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#434F54">//  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power</font>
 +
 
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">openReadingPipe</font><font color="#000000">(</font><font color="#000000">1</font><font color="#434F54">,</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Use the first entry in array 'addresses' (Only 1 right now)</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#000000">}</font><font color="#434F54">//--(end setup )---</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">loop</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** LOOP: RUNS CONSTANTLY ******/</font>
 +
<font color="#000000">{</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font> <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font> <font color="#434F54">// Check for incoming data from transmitter</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#5E6D03">while</font> <font color="#000000">(</font><font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font>  <font color="#434F54">// While there is data ready</font>
 +
    <font color="#000000">{</font>
 +
      <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">read</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">dataReceived</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">dataReceived</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Get the data payload (You must have defined that already!)</font>
 +
    <font color="#000000">}</font>
 +
    <font color="#434F54">// DO something with the data, like print it</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#00979C">"Data received = "</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#000000">dataReceived</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font> <font color="#434F54">//END Radio available</font>
 +
 
 +
<font color="#000000">}</font><font color="#434F54">//--(end main loop )---</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare User-written Functions )-----*/</font>
 +
 
 +
<font color="#434F54">//None yet</font>
 +
<font color="#434F54">//*********( THE END )***********</font>
 +
 
 +
 
 +
</pre>
 +
 
 
----
 
----
 
<span id="js1"> </span>
 
<span id="js1"> </span>

Revision as of 10:58, 25 April 2018

Nrf24L01-2.4GHz-ExampleSketches

These are example Arduino software sketches that show how to use the nRF24L01 radio modules. Start with our overall nRF24L01 Information and How-To HERE to make sure you connect the nRF24L01 modules correctly. Consider using one of the ["base modules"] to get stable 3.3V DC power to your modules. Otherwise consider soldering capacitors across Vcc and Gnd on your modules. See the How-To..

Below are the different examples as they are written and tested here. Click on the names below. There are also many examples that come with the RF24 library and you can [them HERE]


  1. BARE MINIMUM EXAMPLE</span>
  2. TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS

Bare Minimum example:

This is not a very good example for practical use, but it details the few minimum things you need to do to create radio sketches that work for nRF24L01 Transmit and Receive. Copy and paste these examples into a BLANK window on your Arduino IDE. VERIFY to check them out. You NEED the RF24 library downloaded and installed. See the How-To.

nRF24L01-SimpleTransmit-01

/* YourDuinoStarter Example: Simple nRF24L01 Transmit
  - WHAT IT DOES: Transmits simple fixed data with nRF24L01 radio
  - SEE the comments after "//" on each line below
   Start with radios about 4 feet apart.
  - SEE the comments after "//" on each line below
  - CONNECTIONS: nRF24L01 Modules See:
  http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
  Uses the RF24 Library by TMRH2o here:
  https://github.com/TMRh20/RF24
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 7
   4 - CSN to Arduino pin 8
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED

   V1.02 02/06/2016
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"  // Download and Install (See above)
/*-----( Declare Constants and Pin Numbers )-----*/
//None yet
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataTransmitted;  // Data that will be Transmitted from the transmitter

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
  Serial.begin(115200);
  delay(1000);
  Serial.println(F("RF24/Simple Transmit data Test"));
  Serial.println(F("Questions: terry@yourduino.com"));
  dataTransmitted = 100; // Arbitrary known data to transmit. Change it to test...
  myRadio.begin();  // Start up the physical nRF24L01 Radio
  myRadio.setChannel(108);  // Above most Wifi Channels
  // 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.
  myRadio.setPALevel(RF24_PA_MIN);
  //  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power

  myRadio.openWritingPipe( addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  delay(1000);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  myRadio.write( &dataTransmitted, sizeof(dataTransmitted) ); //  Transmit the data

  Serial.print(F("Data Transmitted = "));
  Serial.print(dataTransmitted);
  Serial.println(F(" No Acknowledge expected"));
  dataTransmitted = dataTransmitted + 1;  // Send different data next time
  delay(500);

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/


//*********( THE END )***********


nRF24L01-SimpleReceive-01

<font color="#95A5A6">/* YourDuinoStarter Example: Simple nRF24L01 Receive</font>
<font color="#95A5A6">  - WHAT IT DOES: Receives simple fixed data with nRF24L01 radio</font>
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
<font color="#95A5A6">   Start with radios about 4 feet apart.</font>
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
<font color="#95A5A6">  - CONNECTIONS: nRF24L01 Modules See:</font>
<font color="#95A5A6">  </font><u><font color="#95A5A6">http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo</font></u>
<font color="#95A5A6">  Uses the RF24 Library by TMRH2o here:</font>
<font color="#95A5A6">  </font><u><font color="#95A5A6">https://github.com/TMRh20/RF24</font></u>
<font color="#95A5A6">   1 - GND</font>
<font color="#95A5A6">   2 - VCC 3.3V !!! NOT 5V</font>
<font color="#95A5A6">   3 - CE to Arduino pin 7</font>
<font color="#95A5A6">   4 - CSN to Arduino pin 8</font>
<font color="#95A5A6">   5 - SCK to Arduino pin 13</font>
<font color="#95A5A6">   6 - MOSI to Arduino pin 11</font>
<font color="#95A5A6">   7 - MISO to Arduino pin 12</font>
<font color="#95A5A6">   8 - UNUSED</font>

<font color="#95A5A6">   V1.02 02/06/2016</font>
<font color="#95A5A6">   Questions: terry@yourduino.com */</font>

<font color="#95A5A6">/*-----( Import needed libraries )-----*/</font>
<font color="#5E6D03">#include</font> <font color="#434F54"><</font><b><font color="#D35400">SPI</font></b><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">></font>   <font color="#434F54">// Comes with Arduino IDE</font>
<font color="#5E6D03">#include</font> <font color="#00979C">"RF24.h"</font>  <font color="#434F54">// Download and Install (See above)</font>
<font color="#95A5A6">/*-----( Declare Constants and Pin Numbers )-----*/</font>
<font color="#434F54">//None yet</font>
<font color="#95A5A6">/*-----( Declare objects )-----*/</font>
<font color="#434F54">// (Create an instance of a radio, specifying the CE and CS pins. )</font>
<font color="#000000">RF24</font> <font color="#000000">myRadio</font> <font color="#000000">(</font><font color="#000000">7</font><font color="#434F54">,</font> <font color="#000000">8</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// "myRadio" is the identifier you will use in following methods</font>
<font color="#95A5A6">/*-----( Declare Variables )-----*/</font>
<font color="#00979C">byte</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">]</font><font color="#000000">[</font><font color="#000000">6</font><font color="#000000">]</font> <font color="#434F54">=</font> <font color="#000000">{</font><font color="#00979C">"1Node"</font><font color="#000000">}</font><font color="#000000">;</font> <font color="#434F54">// Create address for 1 pipe.</font>
<font color="#00979C">int</font> <font color="#000000">dataReceived</font><font color="#000000">;</font>  <font color="#434F54">// Data that will be received from the transmitter</font>

<font color="#00979C">void</font> <font color="#5E6D03">setup</font><font color="#000000">(</font><font color="#000000">)</font>   <font color="#95A5A6">/****** SETUP: RUNS ONCE ******/</font>
<font color="#000000">{</font>
  <font color="#434F54">// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)</font>
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font>
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"RF24/Simple Receive data Test"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Questions: terry@yourduino.com"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>

  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Start up the physical nRF24L01 Radio</font>
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setChannel</font><font color="#000000">(</font><font color="#000000">108</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Above most Wifi Channels</font>
  <font color="#434F54">// Set the PA Level low to prevent power supply related issues since this is a</font>
  <font color="#434F54">// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.</font>
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setPALevel</font><font color="#000000">(</font><font color="#000000">RF24_PA_MIN</font><font color="#000000">)</font><font color="#000000">;</font>
  <font color="#434F54">//  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power</font>

  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">openReadingPipe</font><font color="#000000">(</font><font color="#000000">1</font><font color="#434F54">,</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Use the first entry in array 'addresses' (Only 1 right now)</font>
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>

<font color="#000000">}</font><font color="#434F54">//--(end setup )---</font>


<font color="#00979C">void</font> <font color="#5E6D03">loop</font><font color="#000000">(</font><font color="#000000">)</font>   <font color="#95A5A6">/****** LOOP: RUNS CONSTANTLY ******/</font>
<font color="#000000">{</font>

  <font color="#5E6D03">if</font> <font color="#000000">(</font> <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font> <font color="#434F54">// Check for incoming data from transmitter</font>
  <font color="#000000">{</font>
    <font color="#5E6D03">while</font> <font color="#000000">(</font><font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font>  <font color="#434F54">// While there is data ready</font>
    <font color="#000000">{</font>
      <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">read</font><font color="#000000">(</font> <font color="#434F54">&</font><font color="#000000">dataReceived</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">dataReceived</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Get the data payload (You must have defined that already!)</font>
    <font color="#000000">}</font>
    <font color="#434F54">// DO something with the data, like print it</font>
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#00979C">"Data received = "</font><font color="#000000">)</font><font color="#000000">;</font>
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#000000">dataReceived</font><font color="#000000">)</font><font color="#000000">;</font>
  <font color="#000000">}</font> <font color="#434F54">//END Radio available</font>

<font color="#000000">}</font><font color="#434F54">//--(end main loop )---</font>

<font color="#95A5A6">/*-----( Declare User-written Functions )-----*/</font>

<font color="#434F54">//None yet</font>
<font color="#434F54">//*********( THE END )***********</font>



TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS

These examples include a full round-trip error checking and acknowledge system. They use many of the features of the nRF24L01 Radios and the RF24 Library by TMRH2O such as setting the channel frequency and the power output level. They print the activity and data received on the Serial Monitor at 115200 speed.

    • OPTIONS:** //**Hardware**// - This version defaults to using fixed data with no required Joystick or Servos, for test purposes. There is a variable in both sketches called 'hasHardware' which is set to 'false'. When you have joystick and/or servo hardware connected then set hasHardware = true;
    • OPTIONS: //Radio RF Power Level//** - If you get your radios communicating OK and you think you have good 3.3V power, you can set the RF Power Amplifier to "RF24_PA_MAX". It is initially set to "RF24_PA_LOW" to be less sensitive to poor 3.3V power. See This Page about nRY24L01 problems, power solutions. We strongly recommend the 'base modules' to get reliable information.
    • SERVOS:** Servos draw a lot of current and will **not** work when the Arduino is powered from USB. You must use some external 5V power supply for the servos. The YourDuino RoboRED pictured on the How-to page has a built-in 5V 2A power supply that can run 2 servos when the External power connector is supplied with 9 to 12V.

SKETCH: JOYSTICK DATA TRANSMITTED TO ANOTHER ARDUINO

(Cut and paste into a blank Arduino IDE window) media type="custom" key="28256273"


SKETCH: JOYSTICK DATA RECEIVED FROM ANOTHER ARDUINO

(Cut and paste into a blank Arduino IDE window) media type="custom" key="28256275"


zz