Arduino for Absolute Beginners

Servo Outputs

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video will cover the code and set up needed to use a servo as a output on Arduino.

Keywords

  • Arduino
  • microcontroller
  • electronics
  • servos
  • code
  • setup
  • how to

About this video

Author(s)
Clarissa Kleveno
First online
12 January 2020
DOI
https://doi.org/10.1007/978-1-4842-5671-8_8
Online ISBN
978-1-4842-5671-8
Publisher
Apress
Copyright information
© Clarissa Kleveno 2020

Video Transcript

Servo output. In this segment, we will learn what a servo is, how to connect it to our Arduino and how to program them.

This is a servo. They come in different shapes and sizes. But this is a common hobbyist one. Servos have gear boxes and a small arm that spools back and forth.

Servos are not to be confused with motors. Motors have 360 degrees of motion and spin round and round. Servos only have 180 degrees. The main advantage of a servo is that you can tell the arm what position you want it to turn to.

You might notice that we have three wires coming out of the servo. Brown is ground, red is power, and orange is data. Data is where you send the signal to tell the servo what degree you want your arm to point to. To connect your server to the Arduino, connect your power to the 5-volt pin, ground, the ground pin. and your data to a PWM pin. We’re going to use pin 9.

Let’s move on to the code. This time we’re going to borrow it straight from the Arduino website. So take a look at this URL. The first line of code is comment. It tells us who wrote this code and where you can find more information.

The next line is pound include servo dot h. What we’re doing here is importing an extra code library from the file, servo dot h. Programming a servo is pretty complicated. Just like the built-in functions analog right you used earlier, this allows extra functionality by giving you prebuilt functions to use.

Next we’re going to declare some constants. We’re going to declare an object of the types servo, imported from the servo library, and name it my servo. Next we want to set the initial position of the servo. If you remember, the servo arm can have a position from zero to 180. So we’re going to set it to zero.

With that, we’ll run the setup function. We’re going to attach my servo to pin number 9. This function tells your Arduino to send data back and forth through pin 9. Next, let’s enter the loop. In the first four loop, we’ll start at position zero and move the arm one degree, then wait 15 milliseconds.

Add one to the position, and then keep doing this while position is less than or equal to 180 is still true. My servo all right is a function provided by the servo library that we imported that tells the servo what position to go to. Then delay 15 milliseconds.

After that, we’ll enter a loop that basically does the opposite, and sends the arm back to zero. The loop function will repeat, making the arm move slowly back and forth. And that’s it. Upload a code to your Arduino, and the servo should look something like this.