How to control servo motor using arduino

in utopian-io •  7 years ago  (edited)

Good day everyone,
Today we will be learning how to control a servo motor using an arduino uno. But first, let me have a brief overview on what an arduino uno is, so that we can have an idea on what this technology is all about.

image.png

Understanding Arduino

Arduino is an open-source electronics platform that is based on easy to use hardware and software, they are designed to make electronics more accessible to designers, artists, hobbyist and anyone that is interested in creating interactive objects/environments.
Arduino boards like Arduino uno can be bought pre-assembled ,because it is an open-source hardware. The pre-assembled board includes a microcontroller, which is programmed using the Arduino language. The arduino programming language is a simplified C/C++ and it is called a ''sketch'', this uses basic programming structures, variables and functions.
image.png

Arduino Uno

Arduino Uno is a type of Arduino board that has a microcontroller based on the ATmega328. It has 14 input/output pins for digital signals, 6 analog input pins, a 16 Mhz quartz crystal, a Usb connection, a power jack, an ICSP header and a reset button. All this component is integrated together on this board to support the microcontroller, now all we need to do is just plug it to the computer and start playing with it. This is the best board to get when just starting to dive into the world of electronics and coding because of how robust this board is and it is affordable.

Servo motor

image.png
Servo motors are a rotary actuator or linear actuators that are made for precision, velocity and acceleration. It can precisely control it angular or linear position. it is made up of motors that are coupled to a sensor for postition feedback. Servo motors are controlled by sending electrical impulse of variable width, or pulse width modulation(pwm), through the control wire. A servo motor can only turn up to 180°, but I would advise you to avoid using the full 180 degrees because it can damage the servo motor.

Controlling the servo motor

Now that we have learned about the components that we are using today, let us get started in how to control our servo motor using Arduino Uno.

First Step(Connecting the wires)

We connect the black wire from the servo to the GND pin on the Arduino Uno, then next is we connect the red wire from the servo to the +5V pin on the Arduino Uno and lastly we connect the yellow wire or sometimes white wire from the servo to the digital pin on the Arduino Uno, it can be in any of the 14 digital pins as long as you remember what number is that pin.
image.png
The Schematic
image.png

Second Step(Coding)

Now that we are done with connecting the wires from the servo motor to the Arduino Uno, let us now start with the coding. The codes that I used is right down below.
image.png

#include<Servo.h> - This declaration tells the Arduino editor that you want access to the Servo library functions.
Servo myservo; -This will create a servo object to control a servo, there can be 12 servo objects that can be created on most boards.
int pos = 0; -This is the variable to store the servo position.
void setup()- This is where we put the initialization process.
myservo.attach(9); -This attaches the servo that is on pin 9 to the servo object.
void loop() - This is where we put the program that we want to run forever.
for (pos = 0; pos <= 180; pos += 1) -The position of the servo will go from 0 degrees to 180 degrees in steps of 1 degree.
myservo.write(pos); - This will tell the servo to go to position in variable 'pos'
delay(15); -This will make the servo wait 15ms(milliseconds) for the servo to reach the position.
for (pos = 180; pos >= 0; pos -= 1) -This will tell the servo to go from 180 degrees to 0 degrees.
myservo.write(pos);-This will tell the servo to go to position in variable 'pos'
delay(15); - This will make the servo wait 15ms(milliseconds) for the servo to reach the position.

That is it for the explanation for the codes. Now, after you have typed the codes, click the Verify button in the upper left of the Arduino IDE and then press Upload to upload the sketch to your board (make sure that you have your board connected to your computer and you selected the your Arduino Uno board in the Arduino IDE) .
Now, you have accomplished in controlling the servo motor. It is all up to you now, play with it to your heart's content.

That's all for now

ted7

signing out,



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by ted7 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.

Your contribution cannot be approved yet. please enclose all the code in code box.

See the Utopian Rules. Please edit your contribution to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Oh! Okay, I'll change it right now.

I have finished putting the code in the code box. I hope I did it correctly.😖

Thanks for the effort. I'll approve. But by code block, I meant a markdown code block. Please check any markdown tutorial online for inserting code blocks.

I red the markdown tutorials on how to make the code block but whenever I try the < code > < /code > command and ``` `` it will put all the text below the codes in the code block. I surely did something wrong and just can't seem to figure it out just yet :(

This:

```
int pos = 0;
```

or this:

<code>int pos = 0;</code>

will make this:
int pos = 0;

Thank you for the contribution. It has been approved.

[utopian-moderator]

Thank you very much for approving it. I will do my best to figure out how to make the code block.

Hey @ted7 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x