Connect with us

Arduino and ESC problem

Discussion in 'Microcontrollers, Programming and IoT' started by Martynas, Jun 10, 2017.

Scroll to continue with content
  1. Martynas

    Martynas

    4
    0
    Jun 10, 2017
    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);
    }
    
    
    [​IMG]
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,025
    2,138
    Nov 17, 2011
    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.
     
  3. Martynas

    Martynas

    4
    0
    Jun 10, 2017
    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.
     
  4. Bluejets

    Bluejets

    4,062
    848
    Oct 5, 2014
    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.
     
  5. Martynas

    Martynas

    4
    0
    Jun 10, 2017
    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 :)
     
  6. Bluejets

    Bluejets

    4,062
    848
    Oct 5, 2014
    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.
     

    Attached Files:

  7. Harald Kapp

    Harald Kapp Moderator Moderator

    10,025
    2,138
    Nov 17, 2011
    O.K., sorry, an oversight on my side.
     
  8. Martynas

    Martynas

    4
    0
    Jun 10, 2017
    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: Jun 11, 2017
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-