Arduino Programming for Servo motor

In summary, the author is working on a project that requires controlling the motion of a servo motor with an Arduino microcontroller. He does not want the motion to behave linearly (constant increments of the pos variable), and he is looking for suggestions on how to approach this. He is new to programming, and if you want to keep it taking 40 increments from 0 to 180 and 40 increments from 180 back to 0 then you should use for(pos = 0; pos < 80; pos ++) { float pos_sin = sin(2*PI*pos/80); myservo.write(pos_sin); }. If you want to increase / decrease that frequency then you either need to change the increments per cycle
  • #1
pyroknife
613
4
Hi all. I am working on a project that requires using an arduino microcontroller to control the motion of a servo motor.

The original code is of the form:
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}


void loop()
{
for(pos = 0; pos <= 40; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 40; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}


but the problem is I don't want the motor's motion to behave linearly (constant increments of the pos variable). My desired motion is a sinusoidal like function.
Does anyone have any suggestions on how to approach this?
I am quite new to programming, especially with microcontrollers.
 
Engineering news on Phys.org
  • #2
If you want to keep it taking 40 increments from 0 to 180 and 40 increments from 180 back to 0 then

for(pos = 0; pos < 80; pos ++)
{
float pos_sin = abs(sin(2*PI*pos/80))
myservo.write(pos_sin)
}

Might want to double check what scaling myservo.write wants, as this is it will get a floating point 0 .. 1 where before you were giving it an integer 0..40
 
  • #3
Floid said:
If you want to keep it taking 40 increments from 0 to 180 and 40 increments from 180 back to 0 then

for(pos = 0; pos < 80; pos ++)
{
float pos_sin = abs(sin(2*PI*pos/80))
myservo.write(pos_sin)
}

Thanks for the reply!
I actually want the servo motor to go from 0deg to 40deg and back to 0 deg and then repeat. That is what I am calling one full cycle.
 
  • #4
Well in that case basically just multiply pos_sin * 40 once you calculate it. Also just use PI so that you go from sin(0) -> sin(PI/2) -> sin(PI) which takes you from 0*40 -> 1*40 -> 0*40 (full cycle from 0 to 40 to back again.

for(pos = 0; pos < 80; pos ++)
{
float pos_sin = sin(PI*pos/80)
myservo.write(40*pos_sin)
}

Depending on if you want to end on 0 or right before 0 make pos <= 80 instead of pos < 80.

Also the 80 really has no significance now, you could make it any number depending on the resolution you want (be sure to change it in both places if you did change it).
 
  • #5
Floid said:
Well in that case basically just multiply pos_sin * 40 once you calculate it. Also just use PI so that you go from sin(0) -> sin(PI/2) -> sin(PI) which takes you from 0*40 -> 1*40 -> 0*40 (full cycle from 0 to 40 to back again.

for(pos = 0; pos < 80; pos ++)
{
float pos_sin = sin(PI*pos/80)
myservo.write(40*pos_sin)
}
By using this, it is difficult to specify the frequency of operation?
 
  • #6
Well, the frequency is going to be dependent on how fast the for loop gets called. I left out the delay you had in your original program but if you need it for servo settling time then if you assume each iteration of the loop has a 15ms delay:

80 increments per cycle (from my code) * 15ms delay = 1.2 seconds to make a fully cycle so you are at a little less than 1Hz operation. If you want to increase / decrease that frequency then you either need to change the increments per cycle or the delay per cycle. So for example if you want to double the frequency cut the increments per cycle in half.

Like most systems you end up trading resolution (a bunch of increments) for speed. You have to find the happy medium you need.
 
  • #7
Floid said:
Well, the frequency is going to be dependent on how fast the for loop gets called. I left out the delay you had in your original program but if you need it for servo settling time then if you assume each iteration of the loop has a 15ms delay:

80 increments per cycle (from my code) * 15ms delay = 1.2 seconds to make a fully cycle so you are at a little less than 1Hz operation. If you want to increase / decrease that frequency then you either need to change the increments per cycle or the delay per cycle. So for example if you want to double the frequency cut the increments per cycle in half.

Like most systems you end up trading resolution (a bunch of increments) for speed. You have to find the happy medium you need.

Okay, thanks a bunch. I'll give this a try.
 
  • #8
The abs is not necessary right, for my case?
 
  • #9
Out of curiosity, instead of using a for loop, could you just write a sin function and have that drive the motion instead of using a loop?
 

Related to Arduino Programming for Servo motor

1. What is an Arduino?

An Arduino is an open-source microcontroller platform that is used for building electronic projects. It consists of a small programmable circuit board and software that allows users to write and upload code to control various electronic components, such as servo motors.

2. What is servo motor?

A servo motor is a type of motor that uses feedback control to precisely rotate to a specific angle. It is commonly used in robotics and other applications that require precise control of movement.

3. How do I connect a servo motor to an Arduino?

To connect a servo motor to an Arduino, you will need to use three wires: power (red), ground (black), and control (usually yellow or white). The power and ground wires should be connected to the appropriate pins on the Arduino, and the control wire should be connected to a PWM (Pulse Width Modulation) pin.

4. How do I program a servo motor with Arduino?

To program a servo motor with Arduino, you will need to use the servo library and the "attach()" and "write()" functions. The "attach()" function assigns a servo motor to a specific pin on the Arduino, and the "write()" function sets the angle of rotation for the motor. You can also use the "map()" function to map a range of values to the angle of rotation.

5. What are some common applications of Arduino programming for servo motors?

Arduino programming for servo motors is commonly used in robotics, home automation, and other projects that require precise control of movement. Examples include robotic arms, automated doors and windows, and RC cars.

Similar threads

  • Electrical Engineering
Replies
5
Views
3K
Replies
1
Views
4K
  • Mechanical Engineering
Replies
4
Views
3K
  • Mechanical Engineering
Replies
2
Views
3K
  • General Engineering
Replies
8
Views
6K
Replies
3
Views
6K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Electrical Engineering
3
Replies
85
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top