# Driving Servo Sinusoidally

**URL:** <https://forum.arduino.cc/t/driving-servo-sinusoidally/6361>\
**Category:** Frequently-Asked Questions\
**Created:** [December 8, 2010, 5:26pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361 "2010-12-08T17:26:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 8, 2010, 5:26pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/1 "2010-12-08T17:26:36Z")

</div>

I'm hoping to be able to drive a motor sinusoidally, or more generally, I hope to be able to put some input mathematical function (sine, sawtooth wave, square wave) and have a motor reproduce that motion. I'm wondering if this is possible with a servo motor and Arduino. I need this to be quite accurate and was just curious if Arduino had the capabilities to produce these motions accurately with a servo motor.

Sorry if this has previously been asked I couldn't find a suitable answer searching.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 8, 2010, 5:29pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/2 "2010-12-08T17:29:34Z")

</div>

R/C servos can only rotate - can you explain what you mean by "sinusoidally"?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 8, 2010, 5:55pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/3 "2010-12-08T17:55:30Z")

</div>

hmm....

Well, I want the rotation to be sinusoidal. Similar to the sweep example given with the Arduino but I want to accurately define the position/velocity of the rotation by varying the signal sent to the servo.

Basically I want to make a rigid panel rotate back and forth (think of a fish fin swimming, it oscillates back and forth to propel the fish forward), and want the servo to provide this motion. I want the motion of this oscillation to be accurately defined by mathematical functions to mimic things like sine, sawtooth, and square waves.

That help?

---

<div class="post-metadata">

**Author:** ![BroHogan](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@BroHogan](https://forum.arduino.cc/u/BroHogan)\
**Post date:** [December 8, 2010, 6:02pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/4 "2010-12-08T18:02:59Z")

</div>

FWIW  
If the servo was providing the up and down motion of the "fin" - say the Y axis, wouldn't the wave shape depend on speed that the "fish" was moving? (say the X axis).  
I'd guess it's quite possible to control a servo to generate the Y axis for an expected, or measured X axis.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 8, 2010, 6:58pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/5 "2010-12-08T18:58:32Z")

</div>

Yes, I'm effectively trying to dictate the y-axis in your example BroHogan, I'm just not sure how accurate Arduino would be in being able to do this with a servo motor. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 10, 2010, 5:06am UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/6 "2010-12-10T05:06:50Z")

</div>

From what I know about servos, they have varying increments that they are rated for, the more increments the more accurate the servo. The arduino should be able to provide very accurate signals with the right hardware/ software but the accuracy will only be as good as your servo. Out of curiosity, what exactly is this for? You mentioned fish, are you making a robot fish or was that just an example?

---

<div class="post-metadata">

**Author:** ![retrolefty](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@retrolefty](https://forum.arduino.cc/u/retrolefty)\
**Post date:** [December 10, 2010, 5:37am UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/7 "2010-12-10T05:37:49Z")

</div>

In principle any linear servo could be used to generate any waveform one desired for a X vs time chart type application. Limits to resolution and speed would have to be researched if they would meet the applications requirements.

Lefty

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 10, 2010, 6:12am UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/8 "2010-12-10T06:12:48Z")

</div>

Here's a small function I use to generate a sine wave with a digital to analog converter (DAC).

```cpp
uint16_t sin_wave[128];

void setup()
{
  for (int16_t i = 0; i < 128; i++)
  {
    float angle = TWO_PI * i / 128;
    int16_t val = sin(angle) * (2048 - 1);
    val += 2048;
    sin_wave[i] = val;
  }
}

```

128 is the number of steps (the granularity of your sine wave). 2048 is the waveform zero point offset.

The next step is to pass these values in a sweep from 0 to 127 to the output with an appropriate delay between each step. You could use a similar technique for just about any waveform. Either pre compute the steps or just compute and feed the values directly to your output as you go.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 6:37pm UTC](https://forum.arduino.cc/t/driving-servo-sinusoidally/6361/9 "2021-05-06T18:37:39Z")

</div>


