Hi. I'm learning Arduino and want to control the servo motor using a nrf24l01+ module. The radio works fine without a motor, but with works only for several seconds.
What I have:
SETUP
Receiver:
- Arduino micro
- Servo motor 9g
- nrf24l01+ module with a power adaptor 5V to 3.3V
Transmitter:
- Arduino mega
- Joystick
- nrf24l01+ module with a power adaptor 5V to 3.3V
PROBLEM DESCRIPTION
When I use these 2 boards without any kind of a motor, the signal goes fine. I can transmit some hardcoded values as well as values received from a joystick. No problems. So I assume my pins connections are fine. Also, I can rotate servo motor just programmatically without radio, so here pins are fine too.
The problem appears when I start to move the motor. It can rotate for several seconds and then the radio stops receiving the signal. After the board restart problem is the same: the motor works for a couple of seconds and then the radio stops working (radio.available() returns false).
CODE RECEIVER
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <Servo.h>
#define PIN_SERVO 9
// Radio pins
#define SCK_PIN 15
#define MI_PIN 14
#define MO_PIN 16
#define CE_PIN 3
#define CSN_PIN 4
Servo myservo;
int angle = 5;
int step = 5;
int dt = 500;
// Radio
RF24 radio(CE_PIN, CSN_PIN);
struct JPayload {
int x = 0;
int y = 0;
};
JPayload payload;
const byte address[6] = "00001";
void setup() {
myservo.attach(PIN_SERVO);
// myservo.write(0);
Serial.begin(115200);
while (!Serial)
{
/* code */
}
if (!radio.begin()) {
Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Serial.println("Setup done");
}
void loop() {
uint8_t pipe;
if (radio.available(&pipe)) {
Serial.print("Recieved ");
uint8_t bytes = radio.getPayloadSize(); // get the size of the payload
radio.read(&payload, sizeof(JPayload));
Serial.print(payload.x); // print the payload's value
Serial.print(":"); // print the payload's value
Serial.println(payload.y); // print the payload's value
// rotate servo
int angle = map(payload.x, 0, 1021, 0, 180);
Serial.print("Rotate to ");
Serial.print(angle);
Serial.println(" angle");
myservo.write(angle);
} else {
Serial.println("Not available");
}
delay(dt);
}
CODE TRANSMITTER
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
#include "joystick.h"
/*
Master/Slave (OLD) Controller/Peripheral (NEW)
Master In Slave Out (MISO) Controller In, Peripheral Out (CIPO)
Master Out Slave In (MOSI) Controller Out Peripheral In (COPI)
Slave Select pin (SS) Chip Select Pin (CS)
*/
#define SCK_PIN 52
#define MO_PIN 51
#define MI_PIN 12
#define CE_PIN 7
#define CSN_PIN 8
#define jX_PIN A6
#define jY_PIN A7
RF24 radio(CE_PIN, CSN_PIN);
Joystick joystic(jX_PIN, jY_PIN);
// Let these addresses be used for the pair
const byte address[6] = "00001";
const int dt = 500;
struct JPayload {
int x = 0;
int y = 0;
};
JPayload payload;
void setup() {
Serial.begin(115200);
if (!radio.begin()) {
Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
joystic.initialize();
radio.setPALevel(RF24_PA_MIN); // RF24_PA_MAX is default.
radio.openWritingPipe(address);
radio.stopListening();
} // setup
void loop() {
int currX = joystic.getX();
int currY = joystic.getY();
if (currX >= 495 && currX <= 500){
currX = -1;
}
if (currY >= 516 && currY <= 532){
currY = -1;
}
payload.x = currX;
payload.y = currY;
Serial.print("Transmitting...");
Serial.print(payload.x);
Serial.print(":");
Serial.print(payload.y);
bool report = radio.write(&payload, sizeof(JPayload)); // transmit & save the report
if (report) {
Serial.println(" -> OK");
} else {
Serial.println(" -> FAILED");
}
delay(dt);
} // loop
LOGS EXAMPLES
On the receiver side I see that data is coming, the angle is calculated, and the motor is working actually:
Rotate to 0 angle
Recieved 493:-1
Rotate to 86 angle
Recieved -1:-1
Rotate to 0 angle
On a transmitter side:
Transmitting...493:-1 -> OK
Transmitting...-1:-1 -> OK
After I use the joystick to turn the servo for a several seconds I see:
"Not available" AND "Transmitting...494:-1 -> FAILED" accordingly.
What might be the problem? Could somebody help, please?