i have a working circuit and code for an esc and an arduino nano, and for some reason when i connect the arduino with power via usb, everything is fine, but if i use a 9V battery, the motor starts going on maximum speed, as if it was stuck in the calibration process. (I also use an nrf24l01 module to communicate with a transmitter, and the connection seems to work)
the code for the part that causes problems:
/* Receiver code for the Arduino Radio control with PWM output
* Install the NRF24 library to your IDE
* Upload this code to the Arduino UNO, NANO, Pro mini (5V,16MHz)
* Connect a NRF24 module to it:
Module // Arduino UNO,NANO
GND -> GND
Vcc -> 3.3V
CE -> D9
CSN -> D10
CLK -> D13
MOSI -> D11
MISO -> D12
This code receive 7 channels and create a PWM output for each one on D2, D3, D4, D5, D6, D7and D8
Please, like share and subscribe : https://www.youtube.com/c/ELECTRONOOBS
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h> //To create PWM signals we need this lybrary
const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitter
RF24 radio(9, 10); //CSN and CE pins
// The sizeof this struct should not exceed 32 bytes
struct Received_data {
byte ch1;
byte ch2;
byte ch3;
byte ch4;
};
Received_data received_data;
Servo channel_1;
Servo channel_2;
Servo channel_3;
Servo channel_4;
Servo channel_5;
int ch1_value = 0;
int ch2_value = 0;
int ch3_value = 0;
int ch4_value = 0;
int ch5_value = 0;
void reset_the_Data()
{
// 'safe' values to use when NO radio input is detected
received_data.ch1 = 0; //Throttle (channel 1) to 0
received_data.ch2 = 127;
received_data.ch3 = 127;
received_data.ch4 = 127;
}
/**************************************************/
void setup()
{
//Attach the servo signal on pins from D2 to D8
channel_1.attach(2);
channel_2.attach(3);
channel_3.attach(4);
channel_4.attach(5);
channel_5.attach(6);
//We reset the received values
reset_the_Data();
//Once again, begin and radio configuration
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1,pipeIn);
//We start the radio comunication
radio.startListening();
channel_1.writeMicroseconds(2000);
delay(5000);
channel_1.writeMicroseconds(1000);
delay(2000);
channel_1.write(10);
}
/**************************************************/
unsigned long lastRecvTime = 0;
//We create the function that will read the data each certain time
void receive_the_data()
{
while ( radio.available() ) {
radio.read(&received_data, sizeof(Received_data));
lastRecvTime = millis(); //Here we receive the data
}
}
/**************************************************/
void loop()
{
//Receive the radio data
receive_the_data();
//////////This small if will reset the data if signal is lost for 1 sec.
/////////////////////////////////////////////////////////////////////////
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
// signal lost?
reset_the_Data();
//Go up and change the initial values if you want depending on
//your aplications. Put 0 for throttle in case of drones so it won't
//fly away
}
ch1_value = map(received_data.ch1,123,255,1000,2000);
ch2_value = map(received_data.ch2,0,255,1000,2000);
ch3_value = map(received_data.ch3,0,255,1000,2000);
ch4_value = map(received_data.ch4,0,255,1000,2000);
ch5_value = map(received_data.ch4,0,255,1000,2000);
//Create the PWM signals
channel_1.writeMicroseconds(ch1_value);
channel_2.writeMicroseconds(ch2_value);
channel_3.writeMicroseconds(ch3_value);
channel_4.writeMicroseconds(ch4_value);
channel_5.writeMicroseconds(ch5_value);
}//Loop end