Hello to the community. This is my first post in this forum. I’m a newbie with electronics,too. If my post is in the wrong category(i was between this and ProjectGuidance), please move it to the appropriate one.
I like rc things and i m trying to revive an old really cheap rc plane i bought. I am using an arduino nano clone(banggood) for the project to do 4 things : a)monitor lipo's voltage b)turn on a couple of status leds c)read 2 channels of a 2.4GHZ rc receiver d)output a pwm value related to the throttle input of the receiver to control the plane’s dc motor(using mosfet). So to my problem.
When the program runs and i m trying to control the dc motor, the pwm value is not “linear”. I don’t have an oscilloscope. I connected a multimeter on the output pin and the voltage(pwm value) is not stable. It goes up and down 0.3v - 0.4v. Like it’s trying to output the value and it doesn’t have the strength to do so. So the motor works with strong oscillations. Same with the led. The board cannot turn it on fully. Only to dim it. The board can detect the voltage of the lipo just fine and it’s reading the pulse from the rc receiver, so those parts are ok.
The program runs fine in an original Uno i have. The board is powered with ~7volts from a LM317 regulator which i think is fine. My simple code is below. Do you have any suggestions or ideas ? Why these clones have a so bad performance and all the time issues?
// Based on the 'YMFC-3D_receiver' code, written by Joop Brokking
//Declaring Variables
byte last_channel_1, last_channel_2;
int THROTTLE, GEAR;
unsigned long timer_1, timer_2;
int mosfetGate = 5;
int pwmValue = 0;
float Vs = 0.0;
int statusLed1 = 7;
bool armed = 0;
int counter = 0;
float vsum = 0.0;
unsigned long REFRESH = 250000; // It's in micros() -> 250 millis
unsigned long lastRefreshTime = 0;
//////////////////////// Setup routine ///////////////////
void setup() {
Serial.begin(19200);
analogReference(EXTERNAL);
pinMode(statusLed1, OUTPUT);
pinMode(mosfetGate, OUTPUT);
PORTD = B00000000; //set all the portD pins low to prevent floating values
PORTB = B00000000; //set all the portB pins low to prevent floating values
//Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as inputs
PCICR |= (1 << PCIE2); // set PCIE2 to enable PCMSK2 scan
PCMSK2 |= (1 << PCINT18); // set PCINT18 (digital input 2) to trigger an interrupt on state change
PCMSK2 |= (1 << PCINT19); // set PCINT19 (digital input 3)to trigger an interrupt on state change
while (GEAR < 1500){
counter++;
if(counter == 125){
digitalWrite(statusLed1, !digitalRead(statusLed1));
counter = 0;
}
delay(2); // Delay 2 milliseconds before the next loop in order for the "blinking" to work
}
armed = 1; // Arm the receiver and it's ready to fly
Vs = analogRead(A4) * 0.01354 ;
digitalWrite(statusLed1, LOW);
counter = 0;
}
//////////////////////////Main program loop///////////////////////////////
void loop() {
if(GEAR < 1500){
armed = 0;
pwmValue = 0;
analogWrite(mosfetGate, 0);
}
else armed = 1;
if(armed == 1){
pwmValue = map(THROTTLE, 1000, 2020, 1, 255);
if(pwmValue < 0) pwmValue = 0;
if(pwmValue > 255) pwmValue = 255;
analogWrite(mosfetGate, pwmValue);
}
// Complementary filter used to reduse noise.
Vs = Vs * 0.6 + (analogRead(A4) * 0.01354) * 0.4;
vsum = vsum + Vs;
counter++;
if(micros() - lastRefreshTime >= REFRESH){
lastRefreshTime = lastRefreshTime + REFRESH;
Serial.print(vsum / counter);
Serial.print("\t");
Serial.print(counter);
Serial.print("\t");
Serial.println(vsum);
counter = 0;
vsum = 0.0;
}
}
//This routine is called every time input 2 or 3 changed state
ISR(PCINT2_vect) {
//Channel 1===========PIN2=========Throttle==============================
if (PIND & B00000100) { // Is input 2 high ?
if (last_channel_1 == 0) { //Input 2 changed from 0 to 1
last_channel_1 = 1; //Remember current input state
timer_1 = micros(); //Set timer_1 to micros()
}
}
else if (last_channel_1 == 1) { //Input 2 changed from 1 to 0
last_channel_1 = 0; //Remember current input state
THROTTLE = micros() - timer_1; //Channel 1 is micros() - timer_1
}
//Channel 2===========PIN3=========Gear switch (arm)=====================
if (PIND & B00001000) { // Is input 3 high ?
if (last_channel_2 == 0) { //Input 3 changed from 0 to 1
last_channel_2 = 1; //Remember current input state
timer_2 = micros(); //Set timer_2 to micros()
}
}
else if (last_channel_2 == 1) { //Input 3 changed from 1 to 0
last_channel_2 = 0; //Remember current input state
GEAR = micros() - timer_2; //Channel 2 is micros() - timer_2
}
}
// Function for displaying the receiver signals
// If needed for debugging
void print_signals() {
Serial.print("Throttle:");
Serial.print(THROTTLE);
Serial.print("\t");
Serial.print(pwmValue);
Serial.print("\t");
Serial.print(" Gear sw:");
Serial.println(GEAR);
}