servo library +esc

Hi, my name is Valentin.
I`m glad to join this forum and to make my first steps in world of Arduino.
I found code (tested it), that measure actual RPM of motor (D3 pin)through optical sensor
and output PWM signal (D11 pin)to achieve target RPM.
That make PID control, and closed loop system.

But this code control brushed DC motor, and that is my problem.
I want to control ESC hooked on RC brushless motor. I already seen some videos
(and code) that prove that is achievable with Arduino servo library, but at this level,
i can not do it myself.
Help me edit code so i can send signal to ESC instead.

All credits go to mr.jmhrvy1947.
Here is the video DIY Arduino/Leonardo CNC Spindle Speed Control - YouTube
Here is the complete his work on github GitHub - jmharvey1/Arduino-CNC-Speed-Control: Arduino/Leonardo Speed Control Code; As shown on YouTube Video http://youtu.be/kYFNUQgE26E

I also atached this code as text.

code.txt (36.9 KB)

Ok, so when you use ESCs, all you need is the standard servo library. Forget all that other PID loop/dc motor/etc stuff. If you just want to test/calibrate your ESC, you can use the following code (note that you will also need a potentiometer to act as a throttle):

#include <Servo.h>

Servo ESC_Servo;

const byte pot_Pin = 0;
const byte servo_Pin = 3;

void setup()
{
  Serial.begin(115200);

  ESC_Servo.attach(servo_Pin);
}

void loop()
{
  potentiometerStuff();
}

void potentiometerStuff()
{
  int data = 0;

  data = map(analogRead(pot_Pin),0,1023,20,180);
  Serial.println(data);

  ESC_Servo.write(data);
  
  return;
}

No, man i can`t forget. The code i posted above is 100% what i need. Just need pwm output on dp 11 to be
proper for an ESC input.
Thanks, anyway.

valentinv1:
No, man i can`t forget. The code i posted above is 100% what i need. Just need pwm output on dp 11 to be
proper for an ESC input.
Thanks, anyway.

Oh, I thought you were trying to control the ESC via PID loops and all sorts of stuff. Just use the servo library on D11 and you'll be fine.

Power_Broker:
... Just use the servo library on D11 and you'll be fine.

But as I mentioned before, Im not capable to do it myself at the moment. I need edited code to see how it worls. I understand some of it and know that I must include servo library, but thats my limits for now.

valentinv1:
But as I mentioned before, Im not capable to do it myself at the moment. I need edited code to see how it worls. I understand some of it and know that I must include servo library, but thats my limits for now.

I literally gave you the code.

reference guide

Power_Broker:
I literally gave you the code.

reference guide

??? I honestly dont get it. If its a 2-3 lines of code , be so kind to write it here.

...add/edit lines of attached code.
Thanks.

Just need pwm output on dp 11 to be proper for an ESC input.

You might as well wish to win the lottery without buying a ticket. PWM output is NOT how to control a servo (which is what an ESC is, in effect). The Servo library IS.

You have some value that you think will make a variable speed electric motor achieve the desired results. You need to map that value to an angle that the ESC (servo) can be told to go to.

If you can’t do that, buy the same hardware used in the project you are trying to replicate. Do NOT go substituting other hardware until you KNOW how to map the output for one device to the output needed for the substitute device.

Rather than coming here and begging others to write code for you, try it yourself.
Take one of the servo tutorials, wire up your ESC and see if you can control your motor.
After you do that, try incorporating it into your original program.

If you run into problems, ask for help here. We enjoy helping out people who are trying to do it themselves.

If, instead, you do want someone to write code for you, this is the wrong forum.
Gigs and Collaborations is where you have others write code for you. Bring your wallet.

vinceherman:
Rather than coming here and begging others to write code for you, try it yourself.
Take one of the servo tutorials, wire up your ESC and see if you can control your motor.
After you do that, try incorporating it into your original program.

If you run into problems, ask for help here. We enjoy helping out people who are trying to do it themselves.

If, instead, you do want someone to write code for you, this is the wrong forum.
Gigs and Collaborations is where you have others write code for you. Bring your wallet.

I am certainly not begging anyone. I do not beg for far, far more important things to me, than this.
It is your free will to help me or not, but i am sick of getting general answers to the straight question.
I read "sticky" rules before posting my first question, and followed that rules, so i provided everything
i have straight and clear as possible.
I think that the best and fastest way of learning is when someone show to you things straight, so you see - you learn.
If this is of some commercial value, than i would go to that "other" places already. But its here free for everyone. So if it is a few lines of code, then i think it is not so hard and time consumed to help, but then again its your choice and will.

Good points.
In your favor is knowing that the sticky posts even exist. :slight_smile:

But do realize that there is a fairly constant stream of new forum members who are not interested in doing anything themselves. They have a homework assignment that is due tomorrow (but they never phrase it that way). They want the codes to make it work.

I do still think it would be a good idea to wire up the ESC and test it before you try it in your program.

Yes, it may be just a few lines of code to change from an analogWrite to a servo.write
But if it does not work, you will have a big complex program to debug.

Do you have a potentiometer? Wire it up. Wire up the esc. Write some code to read the pot value, map it to the servo value and write it to the servo. (This also makes it easy to work through any arming sequence that the ESC requires). Mount the motor firmly for this test. Having it kick in at full throttle can be scary if not mounted properly. Ask me how I know!

Here is a tutorial for a servo and a pot. You will not power the ESC from the arduino 5v pin. The main battery usually powers the ESC.

Give it a try. Ask questions. Remember, we really are here because we like helping others.

vinceherman:
Good points....

Thanks for understanding, it wasnt directly addressed to you, but this is my experience on most forums on net in last few years. There are so few posters anywhere, that gave novice ppl direct answers and not speak in general. I, myself, sit quiet if i cant\ dont know (or im lazy enough at that moment to do it :slight_smile: ).
So again, i`m glad you get my point, as i got yours about constant steam of new lazy members. You must be sick of it :slight_smile: .

Anyway, back to the topic...

vinceherman:
I do still think it would be a good idea to wire up the ESC and test it...

Try that already(another tutorial)...everything works ok! (i also have $2 srevo tester, and contactless tachometer, so i was able to do some comparative tests, so that part of "homework" is done too)
I also, recreate jim`s code with some salvaged parts ( different sensor,fan with white mark, pot...)
That works too. I was able to measure RPM of the fan, with arduino quite well.

[quote author=vinceherman ]
Yes, it may be just a few lines of code to change from an analogWrite to a servo.write
But if it does not work, you will have a big complex program to debug.[/quote]
Do it , if you can/want, and then ill scratch my head with it. No worry, its a way to learn.

P.S I already have final solution for all of this externaly, with some cheap PIC (16F629... or so).
Its a simple converter that looks like this: ![](https://s24.postimg.org/eda2thbhh/001.jpg) but while waiting for my PIC programmer to arrive i want to do it with arduino for god sake :) . And i think it would be more elegant to do all this, just with arduino. So get your hands dirty, and help me with code :) Its not begging. It`s good and polite to say please :slight_smile: !

valentinv1:
So get your hands dirty, and help me with code :slight_smile:
Its not begging. Its good and polite to say please :slight_smile: !

I already gave you everything you need to figure it out!!! It’s not difficult at all - many beginners pick it up easily.

Well, here’s my attempt to spoon feed the info you already have:

The Servo library is already included in the IDE and can be called using the following statement at the beginning of the code:

#include <Servo.h>

Having included the library, you can now use the functions, variables, and classes associated with the Servo library.

The first thing to do after including the Servo library, you must first construct a Servo class instance for each servo you will be using. This can be done with the following code:

Servo NameOfServo;

After constructing a Servo class instance, you can use the class’s functions such as defining what pin the Arduino will use to control the servo. This can be done with the following code:

NameOfServo.attach(ServoSignalPin);

After defining what pin the Arduino will use to control the servo, you can use the Arduino to send positional data to the servo - causing the servo to move. This is conventionally done by using the write() function where the argument is the desired position of the servo in degrees. This can be done with the following code:

NameOfServo.write(90); //causes servo to move to the 90 degree position

And that is how simple using servos is. You owe me for spoon feeding this topic.

Power_Broker:
You owe me for spoon feeding this topic.

:slight_smile: Ok, i owe you! :slight_smile:
Now let me be serious...
My dear sir, i figured out simple use of servo.h already, and there is no need to explain that again.
In "my" (jim`s) code pwm output on dp11, is not constant value and it changes accordingly
due to chages @ dp3 input. So the pulse go lower or higher . But output signal is for the "generic" DC
motor pwm controller (as shown in video).
How to change code for output to suit ESC is foggy to me?
I observe the code and most of it is "cosmetic" for different modes + simple scope mode on a1.
There is an pwmPin setup that could be use as servoPin... but still foggy to me.

valentinv1:
How to change code for output to suit ESC is foggy to me?

An ESC is controlled exactly as a servo. servo.write(0) means no throttle and servo.write(180) means full throttle. Do you understand that? Now you have the tools to fix your code yourself. I will not do it for you unless you are willing to pay me.

valentinv1:
There is an pwmPin setup that could be use as servoPin… but still foggy to me.

A servo/ESC can be controlled by any I/O pin on the Arduino. Again, this is handled by the servo.attach() function.

Thanks for suggestions on how to do it. I still owe you :slight_smile: .

Power_Broker:
...I will not do it for you unless you are willing to pay me.

I am not willing to pay you a dime, for few lines of code. I am not in that kind of need or a hurry.
...ccc, please don`t offer that kind of help anymore. There are other places for that. :slight_smile:

I will try to figure this if i can, with ppl willing to help (and knowing how). If i manage this, it will
be here for everyone (for free, off course).
Besides i already have a "way around" solution with external pic12f629, as i mentioned before.
( All credit for this external converter goes to mr. Ilya Kretov. If someone is intrested i`ll post links
to schematics and hex file)

P.S. Any of your free advices are still welcomed here, and please no hard feelings. I have no intention
to offend you in any way. :wink:

I am crying :slight_smile: :slight_smile: :slight_smile: