-->
Notification texts go here Contact Us pelacakan

Arduino NRF24L01 Joystick Controller Remote Transmitter

Arduino NRF24L01 Joystick Controller Remote Transmitter

Hey friends in this video I will show you how to make Wireless Arduino Joystick Controller Transmitter which uses NRF24L01 Transceiver for wireless communication.

Materials:

Arduino NANO

NRF24L01 Transceiver

MPU6050 Gyro sensor

Joystick

Toggle Switch

Tactile Push buttons

Slide Switch

10k Potentiometer

AMS1117 3.3v Regulator

18650 Battery


Circuit:

Transmitter

Receiver

code arduino : 

//Arduino NRF24L01 Transmitter - Joystick Controller Test

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h> //https://github.com/tmrh20/RF24/

#include <Wire.h>

#include <MPU6050.h>

MPU6050 mpu;


#define joybtn1 0  // Joystick button 1

#define joybtn2 1  // Joystick button 2

#define tgl1 7   // Toggle switch 1

#define tgl2 4   // Toggle switch 1

#define btn1 8   // Button 1

#define btn2 9   // Button 2

#define btn3 2   // Button 3

#define btn4 3   // Button 4

#define buzz 10  //Buzzer


RF24 radio(5, 6);   // nRF24L01 (CE, CSN)

const byte address[6] = "00001"; // Address


// Max size of this struct is 32 bytes - NRF24L01 buffer limit

struct Data_Package {

  byte joy1_X;

  byte joy1_Y;

  byte j1Button;

  byte joy2_X;

  byte joy2_Y;

  byte j2Button;

  byte pot1;

  byte pot2;

  byte tSwitch1;

  byte tSwitch2;

  byte button1;

  byte button2;

  byte button3;

  byte button4;

  byte pitch;

  byte roll;


};


Data_Package data; //Create a variable with the above structure


void setup() {

  Serial.begin(9600);


  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

  {

    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");

    delay(500);

  }

  

  

  // radio communication

  radio.begin();

  radio.openWritingPipe(address);

  radio.setAutoAck(false);

  radio.setDataRate(RF24_250KBPS);

  radio.setPALevel(RF24_PA_LOW);

  

  pinMode(joybtn1, INPUT_PULLUP);

  pinMode(joybtn2, INPUT_PULLUP);

  pinMode(tgl1, INPUT_PULLUP);

  pinMode(tgl2, INPUT_PULLUP);

  pinMode(btn1, INPUT_PULLUP);

  pinMode(btn2, INPUT_PULLUP);

  pinMode(btn3, INPUT_PULLUP);

  pinMode(btn4, INPUT_PULLUP);

  pinMode(buzz, OUTPUT);

  digitalWrite(buzz,LOW);

  

  // initial default values

  data.joy1_X = 127;

  data.joy1_Y = 127;

  data.joy2_X = 127;

  data.joy2_Y = 127;

  data.j1Button = 1;

  data.j2Button = 1;

  data.pot1 = 1;

  data.pot2 = 1;

  data.tSwitch1 = 1;

  data.tSwitch2 = 1;

  data.button1 = 1;

  data.button2 = 1;

  data.button3 = 1;

  data.button4 = 1;

  data.pitch = 0;

  data.roll = 0;

  

}



void loop() {

  

  // Read normalized values 

  Vector normAccel = mpu.readNormalizeAccel();


  // Calculate Pitch & Roll

  int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis*normAccel.YAxis + normAccel.ZAxis*normAccel.ZAxis))*180.0)/M_PI;

  int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;

  

  // Read all analog inputs and map them to one Byte value

  data.joy1_X = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255

  data.joy1_Y = map(analogRead(A0), 0, 1023, 0, 255);

  data.joy2_X = map(analogRead(A2), 0, 1023, 0, 255);

  data.joy2_Y = map(analogRead(A3), 0, 1023, 0, 255);

  data.pot1 = map(analogRead(A7), 0, 1023, 0, 255);

  data.pot2 = map(analogRead(A6), 0, 1023, 0, 255);

  // Read all digital inputs

  data.j1Button = digitalRead(joybtn1);

  data.j2Button = digitalRead(joybtn2);

  data.tSwitch2 = digitalRead(tgl2);

  data.tSwitch1 = digitalRead(tgl1);

  data.button1 = digitalRead(btn1);

  data.button2 = digitalRead(btn2);

  data.button3 = digitalRead(btn3);

  data.button4 = digitalRead(btn4);

  data.pitch = (pitch);

  data.roll = (roll);

  

  radio.write(&data, sizeof(Data_Package)); // Send the whole data from the structure to the receiver

  

}

-------------------------------------------------------------------

//Arduino NRF24L01 Receiver - Joystick Controller Test
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9);   // nRF24L01 (CE, CSN)
const byte address[6] = "00001";

unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;

// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
  byte joy1_X;
  byte joy1_Y;
  byte j1Button;
  byte joy2_X;
  byte joy2_Y;
  byte j2Button;
  byte pot1;
  byte pot2;
  byte tSwitch1;
  byte tSwitch2;
  byte button1;
  byte button2;
  byte button3;
  byte button4;
  byte pitch;
  byte roll;
};

Data_Package data; //Create a variable with the above structure

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening(); //  Set the module as receiver
  resetData();
}
void loop() {
  // Check whether there is data to be received
  if (radio.available()) {
    radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
    lastReceiveTime = millis(); // At this moment we have received the data
  }
  // Check whether we keep receving data
  currentTime = millis();
  if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
    resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
  }
  // Print the data in the Serial Monitor
  Serial.print("joy1_X: ");
  Serial.print(data.joy1_X);
  Serial.print("; joy1_Y: ");
  Serial.print(data.joy1_Y);
  Serial.print("; joy2_X: ");
  Serial.print(data.joy2_X); 
  Serial.print("; joy2_Y: ");
  Serial.print(data.joy2_Y);
  Serial.print("; button1: ");
  Serial.print(data.button1);
  Serial.print("; button2: ");
  Serial.print(data.button2);
  Serial.print("; button3: ");
  Serial.print(data.button3);
  Serial.print("; button4: ");
  Serial.print(data.button4);
  Serial.print("; j1Button: ");
  Serial.print(data.j1Button);
  Serial.print("; j2Button: ");
  Serial.print(data.j2Button);
  Serial.print("; tSwitch1: ");
  Serial.print(data.tSwitch1);
  Serial.print("; tSwitch2: ");
  Serial.print(data.tSwitch2);
  Serial.print("; pot1: ");
  Serial.print(data.pot1);
  Serial.print("; pot2: ");
  Serial.print(data.pot2);
  Serial.print("; Pitch: ");
  Serial.print(data.pitch);
  Serial.print("; Roll: ");
  Serial.println(data.roll);

}

void resetData() {
  // Set initial default values
  data.joy1_X = 127;
  data.joy1_Y = 127;
  data.joy2_X = 127;
  data.joy2_Y = 127;
  data.j1Button = 1;
  data.j2Button = 1;
  data.pot1 = 1;
  data.pot2 = 1;
  data.tSwitch1 = 1;
  data.tSwitch2 = 1;
  data.button1 = 1;
  data.button2 = 1;
  data.button3 = 1;
  data.button4 = 1;
  data.pitch = 0;
  data.roll = 0;
}

إرسال تعليق

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.