Debreczeni Tamas
Published © GPL3+

Arduino Radio Communication (nRF24L01)

Make your own RC toys, gadgets, stuff.

AdvancedShowcase (no instructions)6,027
Arduino Radio Communication (nRF24L01)

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

nrf24l01 Remote

Code

nrf24l01 remote

Arduino
//Remote Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//LED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define L_AXIS A0 // Left Y Axis Port
#define R_AXIS A2 // Right Y Axis Port
#define Pot A7   // Poteciometer

//LED Display
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);

float value ;
float voltage ;
int percentage;

struct data{
  int left;
  int right;
  int maxs;
 
};
data send_data;

RF24 radio(7, 8); // CE, CSN
const byte address[6] = "06720"; //NRF Address
void setup() {
  
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
  radio.begin();
  radio.setAutoAck(false);
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();
  
}
void loop() {
  send_data.left=analogRead(L_AXIS);  //Read Left Joystick Y axis value
  send_data.right=analogRead(R_AXIS); //Read Right Joystick Y axis value
  send_data.maxs=map(analogRead(Pot), 0, 1023, 1023, 0);
  
  radio.write(&send_data, sizeof(data));

  value = analogRead(A6);
  voltage = ((5*value)/1023)*4.103354632587859 ;
  percentage = map((voltage*10), 30, 42, 0, 100);
  

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  
  display.setCursor(0,0);
  display.print("L= ");
  display.print(send_data.left);

  display.print("  Power: ");
  if( (500 < send_data.left ) && ( send_data.left < 530 ) )  display.println(0);
  if( send_data.left <= 500 ){
    display.print("-");
    display.println(map(send_data.left, 0, 500, 255, 100));
  }
  if( 530 <= send_data.left )  display.println(map(send_data.left, 530, 1023, 100, 255));

  
  display.print("R= ");
  display.print(send_data.right);

  display.print("  Power: ");
  if( (500 < send_data.right ) && ( send_data.right < 530 ) )  display.print(0);
  if( send_data.right <= 500 ){
    display.print("-");
    display.print(map(send_data.right, 0, 500, 255, 100));
  }
  if( 530 <= send_data.right )  display.print(map(send_data.right, 530, 1023, 100, 255));

  display.print('\n');
  display.print('\n');
  display.print('\n');
  display.print('\n');
  display.print("Max Speed: ");
  display.print(map(send_data.maxs, 0, 1023, 150, 255));
  display.print('\n');
  display.print("Voltage: ");
  display.print(voltage);
  display.print("V   ");
  display.print(percentage);
  display.print("%");
  
  display.display();
  
}

nrf24l01 receiver

Arduino
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);

//Motor Ports
#define R_FRONT 10
#define R_BACK 9  
#define L_FRONT 5 
#define L_BACK 6

int maxSpeed ;

const int buzzer = 3; //buzzer to arduino pin 9

  

RF24 radio(7, 8); // CE, CSN


const byte address[6] = "06720"; //NRF Address

unsigned long lastRecvTime = 0; //Used for latency check

struct data{
  int left;
  int right;
  int maxs;
 
};
data receive_data;

void reset_the_Data() //Set default values
{
  receive_data.left=510;
  receive_data.right=510;
  receive_data.maxs=0;
  
  
}

void receive_the_data() //Read
{
  while ( radio.available() ) {
    radio.read(&receive_data, sizeof(data));
    lastRecvTime = millis();
  }
}

void move(char direction[ ], char motor[ ], int value){
  if(motor == "right"){
    if(direction == "stay"){
      analogWrite(R_FRONT,0);
      analogWrite(R_BACK,0);
    }else if(direction == "backwards"){
      analogWrite(R_BACK,map(value, 0, 500, maxSpeed, 100));
    }else if(direction == "forwards"){
      analogWrite(R_FRONT,map(value, 530, 1023, 100, maxSpeed));
    }
  }else{
    if(direction == "stay"){
      analogWrite(L_FRONT,0);
      analogWrite(L_BACK,0);
    }else if(direction == "backwards"){
      analogWrite(L_BACK,map(value, 0, 500, maxSpeed, 100));
    }else if(direction == "forwards"){
      analogWrite(L_FRONT,map(value, 530, 1023, 100, maxSpeed));
    }
    maxSpeed = map(receive_data.maxs, 0, 1023, 150, 255);
  }

  
  Serial.print(direction);
  Serial.print(" ");
  Serial.print(motor);
  Serial.print(" ");
  Serial.println(value);

}

  



void setup() {
  
  
  Serial.begin(9600);
  radio.begin();
  radio.setAutoAck(false);
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();

   Wire.begin();
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x32)(initializing the display)
  
}
void loop() {
  receive_the_data();
  
  unsigned long now = millis();
  if ( now - lastRecvTime > 1000 ) {  //Check for latency
    reset_the_Data();
  }

  
  if( (500 < receive_data.left ) && ( receive_data.left < 530 ) )  move("stay","left",receive_data.left);
  if( receive_data.left <= 500 )  move("backwards","left",receive_data.left);
  if( 530 <= receive_data.left )  move("forwards","left",receive_data.left);

  if( (500 < receive_data.right ) && ( receive_data.left < 530 ) )  move("stay","right",receive_data.right);
  if( receive_data.right <= 500 )  move("backwards","right",receive_data.right);
  if( 530 <= receive_data.right )  move("forwards","right",receive_data.right);
  
  //int value=map(receive_data.right, 0, 1023, 180, 255);
  //analogWrite(R_FRONT,value);

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  
  display.setCursor(0,0);
  display.print("LY= ");
  display.print(receive_data.left);
  display.print("\t");
  display.print("  RY= ");
  display.print(receive_data.right);
  
        
  display.display();

  
   
  
      
  /*Serial.print(receive_data.left);
  Serial.print("\t");
  Serial.println(receive_data.right);*/
}

Credits

Debreczeni Tamas

Debreczeni Tamas

6 projects • 20 followers

Comments