In this tutorial we learn how to control a servo motor using a potentiometer, that is we will control the position of the servo motor by changing the resistance of a resistor. We will apply several concepts of the C++ programming language.


  1. One Dwenguino board
  2. Enclosed USB cable
  3. One servo motor
  4. One potentiometer
  5. One soldering iron and some tin
  6. Optionally a Dwenguino breadboard to easily build your circuit and some wires

Connecting a servo

A servo typically has a connector with three wires. Two of these wires provide the servo with the correct voltage, these are called the power lines. We use the the third cable to send the signal that controls the position of the servo. Most of the time, the two power lines are colored black (convention for ground) and red (convention for 5 V), but they can be colored differently. The Dwenguino board has two connectors: servo1 and servo2 (see where they are placed). We use these connectors to drive a servo motor. To move this motor we need to supply a current between 200 and 400 mA, the servo connectors in the Dwenguino can supply these amount of current.

Connect the servo to the connector servo1. Do it in such a way that the dark brown or black wire corresponds with the "-" sign as shown in the picture.


Connecting the potentiometer

A voltage divider/potential divider is resistor in series circuit that scales the output voltage to a particular ratio of the input voltage applied. The figure below explains the circuit :

voltage divider

Vout = (Vin*R2)/ (R1 + R2)

Vout is the output potential which depends on the input voltage applied(Vin) and resistors (R1 and R2) in series,which means the current flowing through R1 will also flow through R2 without being divided.
As you see in the above equation, as the value of R2 changes, the Vout scales accordingly with respect to the input voltage, Vin.
Typically a potentiometer is a potential divider which can scale the output voltage of the circuit based on the value of the variable resistor which is scaled using the knob. It has 3 pins: GND, Signal, +5V as shown in the diagram below:


Connect the GND-pin of the potentiometer to the GND of the expansion connector, the Signal-pin to pin A0 of the expansion connector and pin 5V to the 5V of the expansion connector.

Controlling the servo

We start by loading the libraries. We will use the Arduino Servo library for controlling servo-motors.

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Dwenguino.h>
#include <Servo.h> // the servo library

Next we create an object called myservo of class Motor. This object represents the servo motor that we want to control. We attach pin SERVO_1 to it. After that, we initialize the potentiometer signal pin to A0 (hence it is assigned to 0, if it is A1 you need to assign it 1,...). Additionally, we declare a variable val to store the value read from the potentiometer.

Servo myservo; // create servo object
int potpin = 0;  // analog pin used to connect the potentiometer
int val;               // variable to read the value from the analog pin 

In the setup function we initialise both, the Dwenguino and our servo motor myservo.

void setup() { 
  my servo.attach(SERVO_1);  // attaches the servo on pin SERVO_1

In the loop we continuously read the value of the potentiometer (line 14) and store it in the variable val. The function analogRead returns a value between 0 and 1023. Please refer to the light measuring tutorial) for more details.
Since we want to move the servo motor, we scale the variable val to values between 0 and 180, which represent the angle of the shaft of the servo motor in degrees. We do this rescaling of val with the function map.
In the next line we tell the object myservo (representing the servo motor) to write the value to set the position of the servo motor. Wait a moment in order to give time for the servo to go to its new position.

void loop() { 
  val = analogRead(potpin);  // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);  // sets the servo position according to the scaled value 
  delay(10);  // waits for the servo to get there 

The entire program can be found in File > Examples > Dwenguino > SweepServo

The servo control signal

The desired position of the servo is send in the form of a PWM signal by the microcontroller. PWM stands for Pulse-Width Modulation. A PWM signal is an electrical signal of which the voltage periodically generates pulses. The width of these pulses determines the servo position. So when we change the width of the pulses with the setCommand() function, we will change the position of the servo. This is illustrated in the figure below. The PWM signal for steering the servo typically has a period of 20 ms and the width of the pulse varies between 0.7 and 2.3 ms.

servo PWM

Related Post