Interfacing NRF24L01 With Arduino: Controlling Servo Motor

by hIOTron IoT in Circuits > Arduino

7 Views, 0 Favorites, 0 Comments

Interfacing NRF24L01 With Arduino: Controlling Servo Motor

Integrating arduino with nRF.PNG

We will learn more about nRF24l01 modules and how to communicate them with a microcontroller platform like Arduino.

Supplies

Hardware Components

nRF24L01

Arduino UNO

SG90 Micro-servo motor

Jumper wires (generic)

About Project

nrf24l01 module.jpg

The nRF24L01 modules are transceiver modules, meaning each module can both send as well as receive data but since they are half-duplex they can either transfer or receive data at a time. The module has the common nRF24L01 IC from Nordic semi-conductors which is accountable for the transmission as well as the reception of data. The IC interacts with the help of the SPI protocol and hence can be simply communicated with any microcontrollers. One of the benefits of using these modules is that every module has 6 Pipelines.

That means each module can interact with 6 other modules to transmit or receive data. This makes the module appropriate for generating star or mesh networks in IoT applications. Arduino Uno nRF24L01 module connections at the Receiver side As we know that nRF24L01 interacts with the help of SPI protocol. Here we connect the MOSI, MISO, and SCK pins from nRF to pins 11, 12 and 13 respectively. The nRF module is electrically charged by the 3.3V pin on Arduino. Also, connect a servo motor to pin 7 and power it via the 5V pin on Arduino. Arduino Nano nRF24L01 module Connections at Transmitter side The connections for this are also the same. The output analog voltage which will deviate from 0-5V is attached to the A7 pin of the Nano. And both the boards are charged via the USB port.

Run a Program

Code for Transmitter Part: /*Transmit POT value through NRF24L01 using Arduino * * Pin Conections * CE - 7 MISO - 12 MOSI - 11 SCK - 13 CS - 8 POT-A7 */ #include #include "RF24.h" RF24 myRadio (7, 8); struct package { int msg = 0; }; byte addresses[][6] = {"0"}; typedef struct package Package; Package data; void setup() { Serial.begin(9600); myRadio.begin(); myRadio.setChannel(115); //115 band above WIFI signals myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage myRadio.setDataRate( RF24_250KBPS ) ; //Minimum speed delay(500); Serial.print("Setup Initialized"); } void loop() { int Read_ADC = analogRead(A7); char servo_value = map (Read_ADC, 0, 1024, 0,180); if (servo_value>1) data.msg = servo_value; WriteData(); delay(50); // ReadData(); //delay(200); } void WriteData() { myRadio.stopListening(); //Stop Receiving and start transminitng myRadio.openWritingPipe( 0xF0F0F0F0AA); //Sends data on this 40-bit address myRadio.write(&data, sizeof(data)); Serial.print("\nSent:"); Serial.println(data.msg); delay(200); } void ReadData() { myRadio.openReadingPipe(1, 0xF0F0F0F066); // Which pipe to read, 40 bit Address myRadio.startListening(); //Stop Transminting and start Reveicing if ( myRadio.available()) { while (myRadio.available()) { myRadio.read( &data, sizeof(data) ); } Serial.print("\nReceived:"); Serial.println(data.msg); } } Code for Receiver Part: /*CE - 7 MISO - 12 MOSI - 11 SCK - 13 CS - 8 */ #include #include "RF24.h" #include Servo myservo; RF24 myRadio (7, 8); struct package { int msg; }; typedef struct package Package; Package data; byte addresses[][6] = {"0"}; void setup() { Serial.begin(9600); myRadio.begin(); myRadio.setChannel(115); //115 band above WIFI signals myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage myRadio.setDataRate( RF24_250KBPS ) ; //Minimum speed myservo.attach(6); Serial.print("Setup Initialized"); delay(500); } int Servo_value; int Pev_servo_value; void loop() { ReadData(); delay(50); Pev_servo_value = Servo_value; Servo_value = data.msg; while (Pev_servo_value< Servo_value) { myservo.write(Pev_servo_value); Pev_servo_value++; delay(2); } while (Pev_servo_value> Servo_value) { myservo.write(Pev_servo_value); Pev_servo_value--; delay(2); } //data.msg = "nothing to send"; //WriteData(); // delay(50); } void ReadData() { myRadio.openReadingPipe(1, 0xF0F0F0F0AA); //Which pipe to read, 40 bit Address myRadio.startListening(); //Stop Transminting and start Reveicing if ( myRadio.available()) { while (myRadio.available()) { myRadio.read( &data, sizeof(data) ); } Serial.print("\nReceived:"); Serial.println(data.msg); } } void WriteData() { myRadio.stopListening(); //Stop Receiving and start transminitng myRadio.openWritingPipe(0xF0F0F0F066);//Sends data on this 40-bit address myRadio.write(&data, sizeof(data)); Serial.print("\nSent:"); Serial.println(data.msg); delay(300); }