Example Code for Arduino-Control Servo Angle
Last revision 2026/01/15
The article offers a comprehensive guide and example code for controlling servo motor angles using Arduino, focusing on setup with a rotary encoder module, necessary libraries, and step-by-step instructions for achieving precise servo movements in DIY projects.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- Rotary Encoder Module (I2C) x 1
- Servo Motor x 1
- Jumper wires
Software Preparation
- Arduino IDE
- Download and install the Rotary Encoder Module Library. (About how to install the library?)
- Use the built-in
Servolibrary (included with Arduino IDE).
Other Preparation Work
Ensure the rotary encoder's I2C address is set to 0x54 via the DIP switches on the back of the module.I2C Setting Method
Sample Code
/*!
* @file setSensor.ino
* @brief This demo shows tha thow to control servo angle by the encoder
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [qsjhyy]([email protected])
* @version V0.1
* @date 2021-09-15
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_VisualRotaryEncoder
*/
#include <DFRobot_VisualRotaryEncoder.h>
/*
Instantiate an object to drive our sensor;
Set address according to the encoder DIP:
| 1 | 2 | ADDR |
|---|---|------|
| 0 | 0 | 0x54 |
| 0 | 1 | 0x55 |
| 1 | 0 | 0x56 |
| 1 | 1 | 0x57 |
*/
DFRobot_VisualRotaryEncoder_I2C sensor(/*iicAddr = */0x54, /*iicBus = */&Wire);
#include <Servo.h>
Servo myservo;
int pos = 0; // Init servo angle
void setup()
{
myservo.attach(9); // Set servo connection pin
Serial.begin(115200);
// Init sensor
while( NO_ERR != sensor.begin() ){
Serial.println("Communication with device failed, please check connection");
delay(3000);
}
Serial.println("Begin ok!");
/**
* Set encoder gain coefficient, accuracy value to rotate one detent
* Accuracy range: 1~51, the minimum is 1 (light up one LED about every 2.5 turns), the maximum is 51 (light up one LED every one detent rotation)
* Return range: 1-51
*/
sensor.setGainCoefficient(51);
/**
* Get current encoder gain coefficient, accuracy value to rotate one detent
* Accuracy range: 1~51, the minimum is 1 (light up one LED about every 2.5 turns), the maximum is 51 (light up one LED every one detent rotation)
* Return range: 1-51
*/
uint8_t gainCoefficient = sensor.getGainCoefficient();
Serial.print("Encoder current gain coefficient: ");
Serial.println(gainCoefficient);
Serial.println();
delay(1000);
}
void loop()
{
/**
* Detect if the button is pressed
* Return true, button pressed. Return false: button unpressed
*/
if(sensor.detectButtonDown()){
/**
* Set the encoder count value
* value Range[0, 1023], setting invalid when out of range
* In this example, when the button is detected to be pressed, reset the encoder value to zero, then all the LEDs that light up before switch off.
*/
sensor.setEncoderValue(0);
}
/**
* Get encoder current count value
* Return value range: 0-1023
* Map the encoder value to servo angle
*/
uint16_t encoderValue = sensor.getEncoderValue();
Serial.print("The encoder currently counts: ");
Serial.println(encoderValue);
pos = map(encoderValue, 0, 1023, 10, 170);
myservo.write(pos);
Serial.println();
delay(1000);
}
Result

Was this article helpful?
