Components and supplies
SparkFun Transceiver Breakout - nRF24L01+
Enclosure for Arduino Uno
Arduino UNO
Project description
Code
1 way communication (transmitter)
arduino
1//communication 1 way 2//firstly download library https://github.com/nRF24/RF24 3 4#include <SPI.h> 5#include <nRF24L01.h> 6#include <RF24.h> 7RF24 radio(9, 10); // CE, CSN 8const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side. 9int button_pin = 2; 10boolean button_state = 0; 11 12void setup() { 13pinMode(button_pin, INPUT); 14radio.begin(); //Starting the Wireless communication 15radio.openWritingPipe(address); //Setting the address where we will send the data 16radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. 17radio.stopListening(); //This sets the module as transmitter 18} 19 20void loop() 21{ 22button_state = digitalRead(button_pin); 23if(button_state == HIGH) 24{ 25const char text[] = "Your Button State is HIGH"; 26radio.write(&text, sizeof(text)); //Sending the message to receiver 27} 28else 29{ 30const char text[] = "Your Button State is LOW"; 31radio.write(&text, sizeof(text)); //Sending the message to receiver 32} 33radio.write(&button_state, sizeof(button_state)); //Sending the message to receiver 34delay(10); 35}
2 way communication (Arduino B)
arduino
1//nRF24L01 communication 2 ways Arduino B 2 3#include <SPI.h> 4#include <nRF24L01.h> 5#include <RF24.h> 6RF24 radio(9, 10); // CE, CSN 7const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving 8int button_pinB = 6; 9int led_pinB = 5; 10boolean button_stateA = 0; 11boolean button_stateB = 0; 12 13void setup() { 14 pinMode(button_pinB, INPUT_PULLUP); 15 pinMode(led_pinB, OUTPUT); 16 radio.begin(); //Starting the radio communication 17 radio.openWritingPipe(addresses[0]); //Setting the address at which we will send the data 18 radio.openReadingPipe(1, addresses[1]); //Setting the address at which we will receive the data 19 radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. 20} 21 22void loop() 23{ 24 delay(5); 25 radio.startListening(); //This sets the module as receiver 26 if (radio.available()) //Looking for incoming data 27 { 28 radio.read(&button_stateA, sizeof(button_stateA)); 29 if(button_stateA == LOW) 30 { 31 digitalWrite(led_pinB, HIGH); 32 } 33 else 34 { 35 digitalWrite(led_pinB, LOW); 36 } 37 delay(5); 38 39 radio.stopListening(); //This sets the module as transmitter 40 button_stateB = digitalRead(button_pinB); 41 radio.write(&button_stateB, sizeof(button_stateB)); //Sending the data 42 } 43}
2 way communication (Arduino A)
arduino
1//nRF24L01 communication 2 ways Arduino A 2 3#include <SPI.h> 4#include <nRF24L01.h> 5#include <RF24.h> 6RF24 radio(9, 10); // CE, CSN 7const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving 8int button_pinA = 4; 9int led_pinA = 3; 10boolean button_stateA = 0; 11boolean button_stateB = 0; 12 13void setup() { 14 pinMode(button_pinA, INPUT_PULLUP); 15 pinMode(led_pinA, OUTPUT); 16 radio.begin(); //Starting the radio communication 17 radio.openWritingPipe(addresses[1]); //Setting the address at which we will send the data 18 radio.openReadingPipe(1, addresses[0]); //Setting the address at which we will receive the data 19 radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. 20} 21 22void loop() 23{ 24 delay(5); 25 radio.stopListening(); //This sets the module as transmitter 26 button_stateA = digitalRead(button_pinA); 27 radio.write(&button_stateA, sizeof(button_stateA)); //Sending the data 28 delay(5); 29 30 radio.startListening(); //This sets the module as receiver 31 while(!radio.available()); //Looking for incoming data 32 radio.read(&button_stateB, sizeof(button_stateB)); //Reading the data 33 if (button_stateB == LOW) 34 { 35 digitalWrite(led_pinA, HIGH); 36 } 37 else 38 { 39 digitalWrite(led_pinA, LOW); 40 } 41}
1 way communication (receiver)
arduino
1//communication 1 way 2//firstly download library https://github.com/nRF24/RF24 3 4#include 5 <SPI.h> 6#include <nRF24L01.h> 7#include <RF24.h> 8RF24 radio(9, 10); // CE, 9 CSN 10const byte address[6] = "00001"; 11int led_pin = 3; 12boolean button_state 13 = 0; 14 15void setup() { 16pinMode(led_pin, OUTPUT); 17Serial.begin(9600); 18radio.begin(); 19radio.openReadingPipe(0, 20 address); //Setting the address at which we will receive the data 21radio.setPALevel(RF24_PA_MIN); 22 //You can set this as minimum or maximum depending on the distance between 23 the transmitter and receiver. 24radio.startListening(); //This sets 25 the module as receiver 26} 27 28void loop() 29{ 30if (radio.available()) //Looking 31 for the data. 32{ 33char text[32] = ""; //Saving the incoming 34 data 35radio.read(&text, sizeof(text)); //Reading the data 36radio.read(&button_state, 37 sizeof(button_state)); //Reading the data 38if(button_state == HIGH) 39{ 40digitalWrite(led_pin, 41 HIGH); 42Serial.println(text); 43} 44else 45{ 46digitalWrite(led_pin, LOW); 47Serial.println(text);} 48} 49delay(5); 50}
1 way communication (transmitter)
arduino
1//communication 1 way 2//firstly download library https://github.com/nRF24/RF24 3 4#include 5 <SPI.h> 6#include <nRF24L01.h> 7#include <RF24.h> 8RF24 radio(9, 10); // CE, 9 CSN 10const byte address[6] = "00001"; //Byte of array representing 11 the address. This is the address where we will send the data. This should be same 12 on the receiving side. 13int button_pin = 2; 14boolean button_state = 0; 15 16void 17 setup() { 18pinMode(button_pin, INPUT); 19radio.begin(); //Starting 20 the Wireless communication 21radio.openWritingPipe(address); //Setting the address 22 where we will send the data 23radio.setPALevel(RF24_PA_MIN); //You can set it 24 as minimum or maximum depending on the distance between the transmitter and receiver. 25radio.stopListening(); 26 //This sets the module as transmitter 27} 28 29void loop() 30{ 31button_state 32 = digitalRead(button_pin); 33if(button_state == HIGH) 34{ 35const char text[] 36 = "Your Button State is HIGH"; 37radio.write(&text, sizeof(text)); //Sending 38 the message to receiver 39} 40else 41{ 42const char text[] = "Your Button State 43 is LOW"; 44radio.write(&text, sizeof(text)); //Sending the message 45 to receiver 46} 47radio.write(&button_state, sizeof(button_state)); //Sending 48 the message to receiver 49delay(10); 50}
2 way communication (Arduino A)
arduino
1//nRF24L01 communication 2 ways Arduino A 2 3#include <SPI.h> 4#include 5 <nRF24L01.h> 6#include <RF24.h> 7RF24 radio(9, 10); // CE, CSN 8const byte 9 addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for 10 transmitting and one for receiving 11int button_pinA = 4; 12int led_pinA = 3; 13boolean 14 button_stateA = 0; 15boolean button_stateB = 0; 16 17void setup() { 18 pinMode(button_pinA, 19 INPUT_PULLUP); 20 pinMode(led_pinA, OUTPUT); 21 radio.begin(); //Starting 22 the radio communication 23 radio.openWritingPipe(addresses[1]); //Setting 24 the address at which we will send the data 25 radio.openReadingPipe(1, addresses[0]); 26 //Setting the address at which we will receive the data 27 radio.setPALevel(RF24_PA_MIN); 28 //You can set it as minimum or maximum depending on the distance between the transmitter 29 and receiver. 30} 31 32void loop() 33{ 34 delay(5); 35 radio.stopListening(); 36 //This sets the module as transmitter 37 button_stateA 38 = digitalRead(button_pinA); 39 radio.write(&button_stateA, sizeof(button_stateA)); 40 //Sending the data 41 delay(5); 42 43 radio.startListening(); //This 44 sets the module as receiver 45 while(!radio.available()); //Looking 46 for incoming data 47 radio.read(&button_stateB, sizeof(button_stateB)); //Reading 48 the data 49 if (button_stateB == LOW) 50 { 51 digitalWrite(led_pinA, HIGH); 52 53 } 54 else 55 { 56 digitalWrite(led_pinA, LOW); 57 } 58}
2 way communication (Arduino B)
arduino
1//nRF24L01 communication 2 ways Arduino B 2 3#include <SPI.h> 4#include 5 <nRF24L01.h> 6#include <RF24.h> 7RF24 radio(9, 10); // CE, CSN 8const byte 9 addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for 10 transmitting and one for receiving 11int button_pinB = 6; 12int led_pinB = 5; 13boolean 14 button_stateA = 0; 15boolean button_stateB = 0; 16 17void setup() { 18 pinMode(button_pinB, 19 INPUT_PULLUP); 20 pinMode(led_pinB, OUTPUT); 21 radio.begin(); //Starting 22 the radio communication 23 radio.openWritingPipe(addresses[0]); //Setting 24 the address at which we will send the data 25 radio.openReadingPipe(1, addresses[1]); 26 //Setting the address at which we will receive the data 27 radio.setPALevel(RF24_PA_MIN); 28 //You can set it as minimum or maximum depending on the distance between 29 the transmitter and receiver. 30} 31 32void loop() 33{ 34 delay(5); 35 radio.startListening(); 36 //This sets the module as receiver 37 if (radio.available()) 38 //Looking for incoming data 39 { 40 radio.read(&button_stateA, 41 sizeof(button_stateA)); 42 if(button_stateA == LOW) 43 { 44 digitalWrite(led_pinB, 45 HIGH); 46 } 47 else 48 { 49 digitalWrite(led_pinB, LOW); 50 } 51 delay(5); 52 53 54 radio.stopListening(); //This sets the module 55 as transmitter 56 button_stateB = digitalRead(button_pinB); 57 radio.write(&button_stateB, 58 sizeof(button_stateB)); //Sending the data 59 } 60}
1 way communication (receiver)
arduino
1//communication 1 way 2//firstly download library https://github.com/nRF24/RF24 3 4#include <SPI.h> 5#include <nRF24L01.h> 6#include <RF24.h> 7RF24 radio(9, 10); // CE, CSN 8const byte address[6] = "00001"; 9int led_pin = 3; 10boolean button_state = 0; 11 12void setup() { 13pinMode(led_pin, OUTPUT); 14Serial.begin(9600); 15radio.begin(); 16radio.openReadingPipe(0, address); //Setting the address at which we will receive the data 17radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver. 18radio.startListening(); //This sets the module as receiver 19} 20 21void loop() 22{ 23if (radio.available()) //Looking for the data. 24{ 25char text[32] = ""; //Saving the incoming data 26radio.read(&text, sizeof(text)); //Reading the data 27radio.read(&button_state, sizeof(button_state)); //Reading the data 28if(button_state == HIGH) 29{ 30digitalWrite(led_pin, HIGH); 31Serial.println(text); 32} 33else 34{ 35digitalWrite(led_pin, LOW); 36Serial.println(text);} 37} 38delay(5); 39}
Downloadable files
2 way communication (Arduino B)
2 way communication (Arduino B)
1 way communication (receiver)
1 way communication (receiver)
1 way communication (transmitter)
1 way communication (transmitter)
1 way communication (transmitter)
1 way communication (transmitter)
2 way communication (Arduino B)
2 way communication (Arduino B)
1 way communication (receiver)
1 way communication (receiver)
2 way communication (Arduino A)
2 way communication (Arduino A)
Comments
Only logged in users can leave comments
lightthedreams
15 Followers
•9 Projects
11
1
nRF24L01 for communication 1 way and 2 way | Arduino Project Hub
Anonymous user
3 years ago
Saya pakai coding yang satu arah sudah bisa running di awal awal jam ketika sudah lama kenapa stuck yaa gan gak bisa control receiver, harus di restart dulu receivernya baru normal lagi..