This Arduino sketch serves as the receiver code for a wirelessly controlled robot using an NRF24L01 radio module. It reads joystick data (X and Y axis values) transmitted from a corresponding sender unit and translates these values into motor commands to control a robotic platform.
- Wireless Communication: Utilizes the NRF24L01 module for robust 2.4 GHz wireless data transmission.
- Joystick Control: Interprets X and Y axis data from a remote joystick to dictate robot movement.
- Differential Drive Control: Controls two DC motors (likely via an H-bridge motor driver) to achieve forward, backward, left, right, and stop movements.
- Adjustable Speed: Motor speed is set to a constant value (150 PWM) but can be easily modified.
- Low Power Transmission: Configured for minimum power amplification (
RF24_PA_MIN) for basic range and power efficiency.
- Arduino Board: (e.g., Arduino Uno, Nano, Mega)
- NRF24L01 Wireless Transceiver Module: (with breakout board if necessary)
- L298N Motor Driver Module: Or any equivalent H-bridge motor driver
- DC Motors: 2 motors for differential drive robot
- Power Supply: Suitable for your motors and Arduino
Connect the components as follows. Please verify the pin numbers on your specific Arduino board and motor driver, as they might vary.
| NRF24L01 Pin | Arduino Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| CE | 8 |
| CSN | 10 |
| SCK | 13 |
| MOSI | 11 |
| MISO | 12 |
Note: The SCK, MOSI, and MISO pins are the SPI pins, which are fixed on most Arduino boards (e.g., Uno: 13, 11, 12 respectively). CE and CSN pins are user-definable; in this code, they are 8 and 10.
| L298N Pin | Arduino Pin | Description |
|---|---|---|
| ENA | 3 (PWM) | Motor A Enable/Speed |
| IN1 | 4 | Motor A Direction Control |
| IN2 | 5 | Motor A Direction Control |
| IN3 | 6 | Motor B Direction Control |
| IN4 | 7 | Motor B Direction Control |
| ENB | 9 (PWM) | Motor B Enable/Speed |
Connect your two DC motors to the Motor A and Motor B output terminals on the L298N. Ensure the L298N's power input (12V or 5V depending on your motors) and ground are connected to a suitable power source.
- Arduino IDE: Download from Arduino.cc
- RF24 Library: Install via Arduino IDE's Library Manager (Sketch > Include Library > Manage Libraries...). Search for "RF24" by TMRh20.
- Install Arduino IDE: If you haven't already, download and install the Arduino IDE.
- Install Libraries: Open the Arduino IDE. Go to
Sketch > Include Library > Manage Libraries.... Search forRF24and install the library byTMRh20. - Upload the Code:
- Open the provided
.inofile in the Arduino IDE. - Select your Arduino board from
Tools > Board. - Select the correct COM Port for your Arduino from
Tools > Port. - Click the "Upload" button to compile and upload the code to your Arduino board.
- Open the provided
- Power Up: Once uploaded, ensure your robot is powered correctly.
The code continuously listens for incoming data from the NRF24L01 module. The data struct expects xAxis and yAxis integers, typically ranging from 0-1023 (for a 10-bit ADC).
- Forward:
yAxis > 400 - Backward:
yAxis < 320 - Right Turn:
xAxis > 400 - Left Turn:
xAxis < 320 - Stop: If no directional input is received within the specified thresholds (i.e.,
xAxisandyAxisare within the dead zone, typically around 320-400), the motors are stopped.
The analogWrite() function is used to set the motor speed (PWM value). The current speed is set to 150 for both motors, which can be adjusted from 0 (off) to 255 (full speed).
- NRF24L01 Address: The
addressbyte array ("00001") must match the address used by your sender NRF24L01 module for communication. - Motor Control Pins: Adjust
ENA,ENB,MotorA1,MotorA2,MotorB1,MotorB2to match your motor driver's connections. - Joystick Thresholds: The values
400and320forxAxisandyAxisdefine the "dead zone" of your joystick. You might need to adjust these based on your joystick's output range and desired sensitivity. - Motor Speed: Change the
150value inanalogWrite(ENA, 150)andanalogWrite(ENB, 150)to increase or decrease the robot's speed. - NRF24L01 Power Level:
radio.setPALevel(RF24_PA_MIN)sets the minimum transmitting power. You can change this toRF24_PA_LOW,RF24_PA_HIGH, orRF24_PA_MAXto adjust the range, keeping in mind higher power consumes more energy. - Data Rate:
radio.setDataRate(RF24_250KBPS)sets the data transfer rate. Other options includeRF24_1MBPSandRF24_2MBPS. A lower data rate generally provides better range and reliability.
- "RF24 library not found": Ensure you've installed the RF24 library correctly through the Arduino IDE's Library Manager.
- No communication:
- Double-check all wiring, especially the NRF24L01 module's VCC (3.3V) and GND.
- Ensure the
addressbyte array is identical on both the sender and receiver NRF24L01 modules. - Verify both modules are powered on.
- Check the baud rate in
Serial.begin()matches any serial monitor you are using for debugging.
- Motors not moving/moving incorrectly:
- Ensure your motor driver is correctly wired and receiving sufficient power.
- Verify the
MotorAandMotorBpins are connected to the correctINpins on your motor driver. - Check if
ENAandENBare connected to PWM-capable pins on your Arduino (pins 3 and 9 are PWM on Uno). - Adjust
receive_data.yAxisandreceive_data.xAxisthresholds if your joystick's neutral position or range is different.