hIOTron
Created November 9, 2021 © GPL3+

Interfacing nRF24L01 with Arduino: Controlling Servo Motor

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

Interfacing nRF24L01 with Arduino: Controlling Servo Motor

Things used in this project

Story

Read more

Code

Run a Program

Arduino
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 <SPI.h>  
#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 <SPI.h>  
#include "RF24.h" 
#include <Servo.h>

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);
}

Credits

hIOTron
78 projects • 2 followers
hIOTron is an internet of things based company that offers an IoT Platform, products, IoT Solutions, and IoT Training.
Contact

Comments

Please log in or sign up to comment.