Encoder and servo 6

code background

Encoder and servo

Let's move a servomotor

What are we going to do?

In this activity you'll learn how to control a servomotor with an encoder. A rotary encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to an analog or digital signal.

UNO board

You can use any Arduino UNO original or compatible board

USB cable

Depending on your board, you'll need a microUSB cable or a miniUSB cable


You need a computer with Arduino IDE software. If you don't have it, follow this link with instructions to download and install it.

Download Arduino IDE

Rotary encoder

A simple rotary encoder with double SVG connections


A servomotor (AKA servo). In this case we'll use a micro sized servo.

Let's get started

Follow these instructions carefully. You can click on some of the images to zoom in.

Always connect the components BEFORE powering the UNO board or YOU MAY DAMAGE YOUR BOARD OR COMPONENTS.


1. Connect your components

Zoom in

2. Open Arduino IDE

Once you've opened the editor, download and open the sample code from HERE

3. Connect your board

Connect your board with USB cable and wait a bit until you computer recognises it. Now select the correct serial port from Tools->Serial port->(choose correct port) COM1, 2, 3...

Click to zoom

4. Change code
If you succesfully downloaded and opened the code from previous step, you won't need to do any changes. However, feel free to modify anything you like as long as you know what you're doing.   IMPORTANT: If you have a potentiometer instead of a rotary encoder, don't use the program above. Go to File->Exemples->Servo->Knob and connect the potentiometer to A0 port of Arduino like in this image.
5. Upload to the board

Use the upload button to transfer the program to your board. Once done, you should read Upload completed. That means the transfer was succesful. If it's not succesful, try disconnecting your board and connecting it again and repeating the upload process.

6. Final result

After uploading, everything should be working as planned. Otherwise, double check the code and try again. Congratulations, you've done it! You're on the right path to be a maker! 🙂

Now, what else?

Now is your turn:

  • What would you change on the code to make it quicker or more accurate?
  • And, if you want to limit the angle rotation to 90 degrees?


 Continue your journey with the next activity from below


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 thoughts on “Encoder and servo