Arduino controlling a servo motor

Ask questions about projects relating to: aerodynamics or hydrodynamics, astronomy, chemistry, electricity, electronics, physics, or engineering

Moderators: kgudger, bfinio, MadelineB, Moderators

Post Reply
torinlucca
Posts: 1
Joined: Fri Jun 14, 2024 9:44 am
Occupation: Student

Arduino controlling a servo motor

Post by torinlucca »

I'm working on a pill dispenser project using a servo motor, and have some questions about the difference between continuous and positional servo motors. Would a button be able to be used to control a continuous servo motor? I tried to code one to work where when pushed the servo would move a certain amount but that was not working. I'm not sure if that's because of an in issue with the code, or that I should be using a positional motor. Also would it be possible to code a servo motor to be fully automatic, only turning at a certain time or after an amount of time, yet be able to have a button to over ride that command so that the motor would rotate with the push of the button?
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Arduino controlling a servo motor

Post by bfinio »

Hi - trying to address all your points separately:

- We have a video about the difference between positional and continuous rotation servo motors: https://youtu.be/XrEN1oszq_Y. I highly recommend watching that before you continue. To summarize, the Arduino servo library will control the *speed* of a continuous rotation servo motor, but you cannot directly control its position. You can roughly control how far it advances by controlling how long it is on. For example (I am just making these numbers up), if you know it takes 10 seconds to rotate a full 360 degrees, then you could turn it on for 2.5 seconds to rotate 90 degrees. However, that approach is subject to error that will accumulate over time (it probably won't rotate *exactly* 90 degrees, so if it rotates 91 degrees, then the next time you rotate you are already off by one degree, and so on).
- If you want full positional control of a motor that can rotate continuously, you can use something called a stepper motor instead of a servo motor. We do not have our own tutorial for that, but you should be able to find Arduino stepper motor tutorials online.
- In general, yes you could program any output (LEDs, motors, etc) to turn on after a certain amount of time, but also have an override button. You might need to practice more Arduino programming before you can do this easily, but here are some tips:

1) Learn about IF/ELSE statements and boolean operators. You can write an IF statement to execute if a single condition is true, but you can also write an if statement to execute if some combination of conditions is true. For example, in this case your code could say "if it has been more than ten seconds OR the button is pressed, turn on the motor." More info in the Arduino language reference here:

https://www.arduino.cc/reference/en/#structure

2) You may want to look up the Arduino millis() command. It measures the amount of time in seconds since the program started. You can use it to measure time without using delay().

https://www.arduino.cc/reference/en/lan ... me/millis/

https://docs.arduino.cc/built-in-exampl ... houtDelay/

3) You can also use something called an interrupt to "interrupt" whatever else the code is already doing when a button is pressed. This is a little more advanced so I would not recommend trying this right away:

https://www.arduino.cc/reference/en/lan ... interrupt/

Hope all of that helps! If you have not seen it already, we have a very extensive list of Arduino tutorials here, but they are more about using specific hardware (sensors and motors etc) as opposed to programming concepts like if statements and boolean operators: https://www.sciencebuddies.org/science- ... an-arduino
Post Reply

Return to “Grades 9-12: Physical Science”