Vernier Software and Technology
Vernier Software & Technology
Arduino and Vernier Sensors

Servo Motors

Servo motors are popular with many Arduino users, because they allow for precise control of angular position. A standard servo is small, but strong, and energy-efficient, because it draws power proportional to its load.

The sample sketch, VernierTutorialDCUServo, controls the position of a servo arm in response to raw voltage from any Vernier Analog (BTA) sensor. You will need to include the servo.h library at the beginning of your sketch. If you are using a servo motor with a standard 3-pin plug, you can connect it directly to the 3 pins on the Vernier DCU labeled “Servo.” Be sure to verify that the orientation of the servo connector is correct (black lead to GND and white or yellow lead to 4). If your servo motor does not have a 3-pin plug, you can wire it to the DCU screw terminal using the wiring diagram below.

  • Servo Control line (white or yellow wire) to DCU line D4
  • Servo 5V (red wire) to DCU line XP (power)
  • Servo GND (black wire) to DUC line GND

Plug the sensor and DCU into the Analog 1 and Digital 2 ports, respectively, on the Vernier Arduino Interface Shield or into Analog and Digital Protoboard Adapters wired as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section. An external power supply, such as the LabQuest Power Supply, is required to power the servo motor.

/* VernierTutorialDCUServo (v 2017)
 * This sketch controls the arm on a servo motor in response
 * to the voltage from a Vernier analog (BTA)) sensor.
 * Connect the servo motor to the 3-pin connector on the DCU
 * labeled "Servo" (black lead to GND, white or yellow lead to
 * 4) or wire it to the DCU screw terminal. 
 * Plug the DCU into the Digital 2 port on the Vernier Arduino 
 * Interface Shield or into a Digital Protoboard Adapter wired 
 * to Arduino pins 6, 7, 8, and 9. 
 * Plug the sensor into the Analog 1 port on the Vernier
 * Arduino Interface Shield or into an Analog Protoboard Adapter 
 * wired to Arduino pin A0.

#include <Servo.h> //include library functions fpr servo motor
Servo myservo; //create servo object to control a servo

int sensorPin=0; //initialize global variable for pin assignment to sensor
int sensorVoltage; //create global variable for the sensor reading

void setup() {
  myservo.attach(9); //attach the servo object to Arduino pin 9 
  pinMode(9,OUTPUT); //setup the servo
  pinMode(6, OUTPUT); //setup DCU line for a servo motor 
  pinMode(7, OUTPUT); //setup DCU line for a servo motor
  pinMode(8, OUTPUT); //setup DCU line for a servo motor
  digitalWrite(6, LOW); //turn off DCU line
  digitalWrite(7, LOW); //turn off DCU line
  digitalWrite(8, LOW); //turn off DCU line

void loop() {
  sensorVoltage = analogRead(sensorPin); //read raw voltage (value between 0 and 1023) 
  sensorVoltage = map(sensorVoltage, 0, 1023, 0, 179); //scale voltage to angle measure (value between 0 and 179) 
  myservo.write(sensorVoltage); //set servo position according to scaled value 
  delay(15); //wait 15 milliseconds for servo to reach position 



Vernier Engineering Contest

Vernier recognizes instructors for creative uses of Vernier sensors to introduce engineering concepts and/or practices. Vernier Engineering Award »

Go to top