In this Project, we will see about how to transfer data through nRF24L01 module with Arduino.
Circuit Diagram:
Arduino Code:
nRF24L01_simple_msg_tx_code.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(9, 10); // CE, CSN const byte address[6] = "pipe1"; void setup() { radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_MIN); radio.stopListening(); } void loop() { const char text[] = "SDev Electronics"; radio.write(&text, sizeof(text)); delay(1000); }
nRF24L01_simple_msg_rx_code.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(9, 10); // CE, CSN const byte address[6] = "pipe1"; void setup() { Serial.begin(9600); radio.begin(); radio.openReadingPipe(0, address); radio.setPALevel(RF24_PA_MIN); radio.startListening(); } void loop() { if (radio.available()) { char text[32] = ""; radio.read(&text, sizeof(text)); Serial.println(text); } }
nRF24L01_control_led_tx_code.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #define btn_onn 3 #define btn_off 4 RF24 radio(9, 10); // CE, CSN const byte address[6] = "pipe1"; void setup() { radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_MIN); radio.stopListening(); pinMode(btn_onn, INPUT_PULLUP); pinMode(btn_off, INPUT_PULLUP); } void loop() { if (digitalRead(btn_onn) == LOW) { const char text[] = "LED01_ONN"; radio.write(&text, sizeof(text)); } if (digitalRead(btn_off) == LOW) { const char text[] = "LED01_OFF"; radio.write(&text, sizeof(text)); } delay(100); }
nRF24L01_control_led_rx_code.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #define mled 2 RF24 radio(9, 10); // CE, CSN const byte address[6] = "pipe1"; void setup() { radio.begin(); radio.openReadingPipe(0, address); radio.setPALevel(RF24_PA_MIN); radio.startListening(); pinMode(mled, OUTPUT); } void loop() { if (radio.available()) { char text[32] = ""; radio.read(&text, sizeof(text)); String str(text); if (str == "LED01_ONN") { digitalWrite(mled, HIGH); } else if (str == "LED01_OFF") { digitalWrite(mled, LOW); } } }
nRF24L01_dynamic_control_tx_code.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #define potpin A0 #define led 2 RF24 radio(9, 10); // CE, CSN const byte addresses[][6] = {"pipe1", "pipe2"}; boolean buttonState = 0; void setup() { pinMode(led, OUTPUT); radio.begin(); radio.openWritingPipe(addresses[1]); // 00002 radio.openReadingPipe(1, addresses[0]); // 00001 radio.setPALevel(RF24_PA_MIN); } void loop() { radio.stopListening(); int potValue = analogRead(potpin); int angleValue = map(potValue, 0, 1023, 0, 255); radio.write(&angleValue, sizeof(angleValue)); delay(5); radio.startListening(); while (!radio.available()); radio.read(&buttonState, sizeof(buttonState)); if (buttonState == HIGH) { digitalWrite(led, HIGH); } else { digitalWrite(led, LOW); } delay(5); }
nRF24L01_dynamic_control_rx_code.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #define btn 2 #define pwm_pin 3 RF24 radio(9, 10); // CE, CSN const byte addresses[][6] = {"pipe1", "pipe2"}; boolean buttonState = 0; void setup() { pinMode(btn, INPUT_PULLUP); radio.begin(); radio.openWritingPipe(addresses[0]); // 00001 radio.openReadingPipe(1, addresses[1]); // 00002 radio.setPALevel(RF24_PA_MIN); } void loop() { delay(5); radio.startListening(); if ( radio.available()) { while (radio.available()) { int pwm_value = 0; radio.read(&pwm_value, sizeof(pwm_value)); analogWrite(pwm_pin, pwm_value); } delay(5); radio.stopListening(); buttonState = digitalRead(btn); radio.write(&buttonState, sizeof(buttonState)); } }
Libraries Used:
1. RF24 - https://github.com/nRF24/RF24/
Youtube Video Tutorial:
Download our official Android App in PlayStore. Click Here
You can get the all the required files (like Circuit Diagram, Arduino.ino file, Libraries Used, and others) for the project in ZIP format and much more...
No comments:
Post a Comment