You are on page 1of 9

Arduino Wireless Servo Motor Control using

the RF24Network Library

/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Example 01 - Servo Control / Node 00 - Potentiometer ==
by Dejan, www.HowToMechatronics.com
Libraries:
nRF24/RF24, https://github.com/nRF24/RF24
nRF24/RF24Network, https://github.com/nRF24/RF24Network
*/
#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of this node in Octal format (
04,031, etc)
const uint16_t node01 = 01;

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
}

void loop() {
network.update();
unsigned long potValue = analogRead(A0); // Read the potentiometer value
unsigned long angleValue = map(potValue, 0, 1023, 0, 180); // Convert the
value to 0-180
RF24NetworkHeader header(node01); // (Address where the data is going)
bool ok = network.write(header, &angleValue, sizeof(angleValue)); // Send
the data
}
/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Example 01 - Servo Control / Node 01 - Servo motor ==
*/
#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>
#include <Servo.h>

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 01; // Address of our node in Octal format (
04,031, etc)

Servo myservo; // create servo object to control a servo

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
myservo.attach(3); // (servo pin)
}

void loop() {
network.update();
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
unsigned long incomingData;
network.read(header, &incomingData, sizeof(incomingData)); // Read the
incoming data
myservo.write(incomingData); // tell servo to go to a particular angle
}
}

Arduino Wireless Network with Multiple


NRF24L01 Modules
Base 00 source code

/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Base/ Master Node 00==
*/

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

#define button 2
#define led 3

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of this node in Octal format (
04,031, etc)
const uint16_t node01 = 01; // Address of the other node in Octal format
const uint16_t node012 = 012;
const uint16_t node022 = 022;

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
}

void loop() {
network.update();
//===== Receiving =====//
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
unsigned long incomingData;
network.read(header, &incomingData, sizeof(incomingData)); // Read the
incoming data
analogWrite(led, incomingData); // PWM output to LED 01 (dimming)
}
//===== Sending =====//
// Servo control at Node 01
unsigned long potValue = analogRead(A0);
unsigned long angleValue = map(potValue, 0, 1023, 0, 180); // Suitable for
servo control
RF24NetworkHeader header2(node01); // (Address where the data is going)
bool ok = network.write(header2, &angleValue, sizeof(angleValue)); // Send
the data

// LED Control at Node 012


unsigned long buttonState = digitalRead(button);
RF24NetworkHeader header4(node012); // (Address where the data is going)
bool ok3 = network.write(header4, &buttonState, sizeof(buttonState)); // Send
the data

// LEDs control at Node 022


unsigned long pot2Value = analogRead(A1);
RF24NetworkHeader header3(node022); // (Address where the data is going)
bool ok2 = network.write(header3, &pot2Value, sizeof(pot2Value)); // Send the
data
}
di di pangkalan atau node master kita perlu mendefinisikan pustaka dan objek seperti yang dijelaskan
sebelumnya, dan juga mendefinisikan semua node lain yang akan dikirim master untuk mengirim
data. Di bagian loop kita mulai dengan terus-menerus memeriksa apakah ada data yang masuk. Jika
demikian, kita membaca data, menyimpannya ke dalam variabel incomingData dan menggunakannya
untuk mengontrol kecerahan LED. Data ini sebenarnya berasal dari potensiometer dari node 02. Jika
kita melihat kode nya, kita dapat melihat bahwa pengaturannya hampir sama. Yang penting adalah
memberikan alamat yang benar ke tempat kita ingin mengirim data. Dalam kasus ini, itu adalah
master 00. Jadi setelah membaca nilai potensiometer dan mengubahnya menjadi nilai PWM yang
sesuai dari 0 hingga 255, kami mengirim data ini ke master. Kita dapat melihat di sini bahwa saya
menggunakan fungsi millis () mengirim data pada interval 10 milidetik.

Node 02 source code

/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Node 02 (Child of Master node 00) ==
*/

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 02; // Address of our node in Octal format (
04,031, etc)
const uint16_t master00 = 00; // Address of the other node in Octal
format

const unsigned long interval = 10; //ms // How often to send data to the
other unit
unsigned long last_sent; // When did we last send?

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
}

void loop() {
network.update();
//===== Sending =====//
unsigned long now = millis();
if (now - last_sent >= interval) { // If it's time to send a data, send
it!
last_sent = now;
unsigned long potValue = analogRead(A0);
unsigned long ledBrightness = map(potValue, 0, 1023, 0, 255);
RF24NetworkHeader header(master00); // (Address where the data is
going)
bool ok = network.write(header, &ledBrightness, sizeof(ledBrightness));
// Send the data
}
}
/*
Arduino Wireless
Selanjutnya, Network
dari master, - Multiple
kami mengirim NRF24L01 Tutorial
data potensiometer ke node 01 untuk
== Node 02 (Child
mengendalikan motor servo of Master node 00) ==
*/

Node 01<RF24Network.h>
#include source code
#include <RF24.h>
#include <SPI.h>
#include <Servo.h>

#define led 2

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 01; // Address of our node in Octal format (
04,031, etc)
const uint16_t master00 = 00; // Address of the other node in Octal
format

Servo myservo; // create servo object to control a servo

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
myservo.attach(3); // (servo pin)
pinMode(led, OUTPUT);
}

void loop() {
network.update();
//===== Receiving =====//
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
unsigned long incomingData;
network.read(header, &incomingData, sizeof(incomingData)); // Read the
incoming data
if (header.from_node == 0) { // If data comes from Node 02
myservo.write(incomingData); // tell servo to go to a particular
angle
}
if (header.from_node == 10) { // If data comes from Node 012
digitalWrite(led, !incomingData); // Turn on or off the LED 02
}
}
}

Simpul 01 sebenarnya menerima data dari dua node yang berbeda, satu untuk kontrol servo
dan yang lainnya untuk kontrol LED yang berasal dari sensor inframerah dari node 012.
Node 012 source code

/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Node 012 (child of Node 02)==
*/

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

#define led 2
#define IR 3

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 012; // Address of our node in Octal format (
04,031, etc)
const uint16_t node01 = 01; // Address of the other node in Octal
format

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
pinMode(led, OUTPUT);
pinMode(IR, INPUT);
}

void loop() {
network.update();
//===== Receiving =====//
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
unsigned long buttonState;
network.read(header, &buttonState, sizeof(buttonState)); // Read the
incoming data
digitalWrite(led, !buttonState); // Turn on or off the LED
}
//===== Sending =====//
unsigned long irV = digitalRead(IR); // Read IR sensor
RF24NetworkHeader header8(node01);
bool ok = network.write(header8, &irV, sizeof(irV)); // Send the data
}

Dalam kasus seperti itu, kami menggunakan atribut header.from_node untuk mendapatkan
informasi dari node mana data berasal. Jika data yang masuk berasal dari master, kami
menggunakannya untuk mengontrol servo, dan jika data yang masuk dari node 012, kami
menggunakannya untuk mengontrol LED. Di node 012 kita memiliki transmisi dan
penerimaan. Sensor inframerah mengontrol LED yang disebutkan sebelumnya di node 01 dan
LED di sini adalah kontrol dari tombol di master.

Akhirnya, LED di node 022 dikendalikan menggunakan data yang berasal dari potensiometer
lain di master
Node 022 source code
/*
Arduino Wireless Network - Multiple NRF24L01 Tutorial
== Node 022 (child of Node 02)==
*/

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

#define led1 2
#define led2 3
#define led3 4
#define led4 5

RF24 radio(10, 9); // nRF24L01 (CE,CSN)


RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 022; // Address of our node in Octal format (
04,031, etc)
const uint16_t master00 = 00; // Address of the other node in Octal format

void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}

void loop() {
network.update();
//===== Receiving =====//
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
unsigned long potValue;
network.read(header, &potValue, sizeof(potValue)); // Read the incoming
data
// Turn on the LEDs as depending on the incoming value from the
potentiometer
if (potValue > 240) {
digitalWrite(led1, HIGH);
} else {
digitalWrite(led1, LOW);
}
if (potValue > 480) {
digitalWrite(led2, HIGH);
} else {
digitalWrite(led2, LOW);
}
if (potValue > 720) {
digitalWrite(led3, HIGH);
} else {
digitalWrite(led3, LOW);
}
if (potValue > 960) {
digitalWrite(led4, HIGH);
} else {
digitalWrite(led4, LOW);
}
}
}
Arduino Nano Pinout
NodeMCU Pinout

You might also like