Maker Pro
Maker Pro

Arduino and ESC problem

Martynas

Jun 10, 2017
4
Joined
Jun 10, 2017
Messages
4
So I have these:
https://hobbyking.com/en_us/zippy-flightmax-5000mah-3s1p-20c.html 2x wired in parallel for 6S
https://hobbyking.com/en_us/turnigy-plush-100a-w-ubec-speed-controller.html
https://hobbyking.com/en_us/turnigy-aerodrive-sk3-5055-280kv-brushless-outrunner-motor.html

And I want to control them with an arduino UNO ( Later on with 433mhz transmitter/receiver https://www.banggood.com/433Mhz-RF-...o-ARM-MCU-Wireless-p-74102.html?rmmds=myorder although might switch to NRF24L01+ 2.4GHz)

After wiring everything up like in the image and using this code, I press 0 in the serial monitor several times till value gets to 60-70 the motor starts jittering back and forth rather than running constantly in one direction.

What is causing this issue and what can I do about it? (I'm proficient in several programming languages though I havent used the arduino api at all)

Code:
#include <Servo.h>
Servo myservo;
int value = 60;
int val = 0;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
  delay(1);
  myservo.write(10);
  delay(5000);
}

void loop()
{
  while(Serial.available()>0)
  {
    if (Serial.read() == '0')
    {
      value +=2;
    }
    else
    {
      value-=2;
    }
    Serial.println(value);
  }
  myservo.write(value);
}

tlFmGlR.png
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,635
Joined
Nov 17, 2011
Messages
13,635
It might be this bit of code.
Code:
if (Serial.read() == '0')
    {
      value +=2;
    }
    else
    {
      value-=2;
    }
The serial.read() tries to read from the serial port, but returns -1 if no data is available. If you don't send data or send data not fast enough, the else branch will decrement 'value' with every pass of the loop.
I suggest you use two different codes (e.g.0 and 1) to increment or decrement the value, but let value untouched else:
Code:
   switch (Serial.read() ) {
      case '0': 
         value +=2;
         break;
      case '1':
         value -=2;
         break;
      default:
         break;
}

I also suggest you incorporate some limiting function such that 'value' can never be less than min and never be more than max.
 

Martynas

Jun 10, 2017
4
Joined
Jun 10, 2017
Messages
4
It might be this bit of code.
Code:
if (Serial.read() == '0')
    {
      value +=2;
    }
    else
    {
      value-=2;
    }
The serial.read() tries to read from the serial port, but returns -1 if no data is available. If you don't send data or send data not fast enough, the else branch will decrement 'value' with every pass of the loop.
I suggest you use two different codes (e.g.0 and 1) to increment or decrement the value, but let value untouched else:
Code:
   switch (Serial.read() ) {
      case '0':
         value +=2;
         break;
      case '1':
         value -=2;
         break;
      default:
         break;
}

I also suggest you incorporate some limiting function such that 'value' can never be less than min and never be more than max.
Thats why my while loop has a check if serial is available and if it returns more than zero.
Also when logging, the value doesnt change like you say it will.
What I am unsure about is why the motor jitters rather than spins normally, because comparing my code to others who had success it seems that mine should spin also.
So the issue might be that the motor has a different angle/sec than an endless spinning servo?
Im really stumped by this.
 

Bluejets

Oct 5, 2014
6,859
Joined
Oct 5, 2014
Messages
6,859
ESC will require a signal from 70 to around 220 for min and max speeds as this is what the tx delivers normally. You may also require an " initialise" signal as most rc esc will not start until throttle is first taken to max then returned to minimum level ( safety aspect so prop dont chop) Plenty of code out there that will do it.
 

Martynas

Jun 10, 2017
4
Joined
Jun 10, 2017
Messages
4
ESC will require a signal from 70 to around 220 for min and max speeds as this is what the tx delivers normally. You may also require an " initialise" signal as most rc esc will not start until throttle is first taken to max then returned to minimum level ( safety aspect so prop dont chop) Plenty of code out there that will do it.
It might be it, I am not arming the escand the esc might not know what I am sending is throttle signals and it makes the motor jitter back and forth.
Will test it out tomorrow morning, though more input is welcome :)
 

Bluejets

Oct 5, 2014
6,859
Joined
Oct 5, 2014
Messages
6,859
Found one I built a few months ago for a friends loco engine.
I'll try to put the files here.
mmm...seems to work...wasn't sure if the uno file would load.

You will of course have to change any reference to the pro-mini and replace with uno if applicable.
As will you have to arrange code for the speed control as this was full on with a push button.
 

Attachments

  • 3_CylinderStarter.jpg
    3_CylinderStarter.jpg
    48.2 KB · Views: 70
  • Starter.jpg
    Starter.jpg
    33.9 KB · Views: 75
  • GeorgeStarterFeb_2017_ver2.ino
    1.6 KB · Views: 57

Martynas

Jun 10, 2017
4
Joined
Jun 10, 2017
Messages
4
Found one I built a few months ago for a friends loco engine.
I'll try to put the files here.
mmm...seems to work...wasn't sure if the uno file would load.

You will of course have to change any reference to the pro-mini and replace with uno if applicable.
As will you have to arrange code for the speed control as this was full on with a push button.
Thanks, I used some of your code and apparently the bullet connectors on my motor to the esc were soldered poorly, hence the jittering, after fixing it and running the code again everything works fine. :)
Also for the A0 pin I used a potentiometer :)
Code:
    #include <Servo.h>//Using servo library to control ESC // using bounce2 library for push button switch
    Servo esc; //Creating a servo class with name as esc //LED on when ESC is armed ...perhaps make it a flashing type LED
   
    void setup()
    {
      esc.attach(9); //Specify the esc signal pin,Here as D8
      esc.writeMicroseconds(1000); //initialize the signal to 1000
      Serial.begin(9600);
    }
    void loop()
    {
      int val; //Creating a variable val
      val= analogRead(A0); //Read input from analog pin a0 and store in val
      val= map(val, 0, 1023,1000,2000); //mapping val to minimum and maximum(Change if needed)
      esc.writeMicroseconds(val); //using val as the signal to esc
    }
 
Last edited:
Top