# Control a servo motor with the frequency?

Hi,

sorry for my bad English. But I need some help. I want to control my servo motor 'SG90' with the frequency. I'm using the 'delay' function to control the speed of the servo motor, but it would be better to use the frequency. My idea was to transform the frequency (for example 200 Hz) to the period time (t = 1/200s = 5 ms; dividing by 180) and to use the 'delay' function (like delay(5/180)). But the servo motor does not react as I want. How is it possible to use the frequency and that the motor should move smoothly?

Thanks!

Hi,

sorry for my bad English. But I need some help. I want to control my servo motor 'SG90' with the frequency. I'm using the 'delay' function to control the speed of the servo motor, but it would be better to use the frequency. My idea was to transform the frequency (for example 200 Hz) to the period time (t = 1/200s = 5 ms; dividing by 180) and to use the 'delay' function (like delay(5/180)). But the servo motor does not react as I want. How is it possible to use the frequency and that the motor should move smoothly?

Thanks!

You say you want to use "the frequency" but what frequency and where are you getting this frequency from?

As always please post the code you have tried and explain what it does and what you need it to do that's different.

Steve

What Arduino are you using? You should be able to use PWM.

You should be using the Servo library to drive a hobby servo. Measuring frequency can be done several
ways, for instance using pulseIn() to time the waveform high and low parts, adding them to get the period.

ToddL1962:
What Arduino are you using? You should be able to use PWM.

The PWM produced by analogWrite() is not for servos.

...R

Duplicate topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

sk96kap:
I want to control my servo motor 'SG90' with the frequency. I'm using the 'delay' function to control the speed of the servo motor, but it would be better to use the frequency.

I don't understand. It would be a big help if you post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

I presume you are using delay() to cause the servo to move a small distance at a time so that you can control the speed - as in the servo sweep example that comes with the Arduino IDE.

Where will this "frequency" come from?

If you have a value of 200Hz how do you want the servo to move? It would not be possible to get a servo to do 200 sweeps in a second.

...R

Why not just use the servo library?

As several people have already asked, please say exactly what the code does now. Then say what you want it to do that is different.

Steve

I’m using the library ‘SimpleCLI’. The library has the intention to read out the serial monitor. So I define the word ‘freq1’ and the argument ‘hz’. If the user types ‘freq1 -hz 10’ then the program will take the value 10 Hz. I need to transform the value in ms; 1/10 = 0,1 s * 1000 = 100 ms and it must be divided with 180 degrees. With each step, I need the delay ((1/10 * 1000) / 180). Then the motor will rotate with the delay and affects the speed. A while loop controls the expression and the process should be repeated 10 times. But the motor does not react as I want. If I use 20 Hz or 150 Hz, it does not seem to work and I do not see 10 repetitions. How is it possible to process the manual entry of a frequency?

``````#include <Servo.h>
#include <SimpleCLI.h>
Servo servo;
SimpleCLI commandLine;
Command frequenz;

void setup() {
servo.attach(9);
Serial.begin(38400);
frequenz = commandLine.addCommand("freq1"); //the keyword is freq1
frequenz.addArgument("hz"); //the keyword gets the argument hz; so if i say freq1 -hz 200 it should take 200hz
}

void loop() {
for (int i = 0; i <= 180; i += 1) {
servo.write(i);
delay(7);
}

if (Serial.available()) {
Serial.println(input);
commandLine.parse(input);
}

if (commandLine.available()) {
Command cmd = commandLine.getCommand();
if (cmd.getName() == "freq1") {
Argument hzFreq = cmd.getArgument("hz"); //Get the value of hz
String str = hzFreq.getValue();
int n = str.toInt();

float tau_hz = ((1 / n) * 1000) / 180; //transform it in ms and dividing by 180
int zaehler_4 = 0;

while (zaehler_4 < 10) {
for (int i = 180; i >= 0; i -= 1) {
servo.write(i);
delay(tau_hz);
}
for (int i = 0; i <= 180; i += 1) {
servo.write(i);
delay(tau_hz);
}
zaehler_4++;
}
}
}

for (int i = 180; i >= 0; i -= 1) {
servo.write(i);
delay(7);
}
}
``````

`(1 / n)`n is an int

sk96kap:
I'm using the library 'SimpleCLI'.

For this application it seems to be anything but simple.

...R

Start over fresh, no silly CLI library or blather about frequency to confuse the issue. JUST work on getting your servo to do what you want.. set up a SIMPLE piece of code you can drop in a number and the servo does the right thing. Then you can add all the bells and whistles. If you find you even want to by then.

-jim lee

The usual thing for debugging is to add some Serial.prints to see if the code is getting to the parts you want. In this case you also need to print the variables in your calculations i.e. str, n and tau_hz also zaehler_4. See if they are what you expect.

Steve