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 }
Next
Stepper MotorsPrevious
DC Motor Control with PWMContents
Introduction- Arduino Terminology
- About Vernier Sensors: Analog (BTA) and Digital (BTD)
- Connecting Vernier Sensors to Arduino
- Using the VernierLib Library
- Using Vernier Analog (BTA) Sensors with Arduino
- Using Vernier Digital (BTD) Sensors with Arduino
- Using the Vernier Digital Control Unit (DCU) with Arduino
- Displaying Sensor Data on an LCD Screen
- Using Motors with Arduino
- Calibrating Vernier Analog Sensors
- Importing Data into Graphical Analysis 4
- Importing Data into Logger Pro®
- Projects and Ideas
- Turn on a Buzzer Based on a Motion Detector Reading
- Produce a Tone Out that Depends on Sensor Reading
- Controlling a RGB LED with a 3-Axis Accelerometer
- Controlling a Mousetrap with a Photogate
- Laser Pointer Controlled by a Motion Detector
- Add a Display For Temperature Readings
- Auto-ID for Digital (BTD) Sensors
- Auto-ID for Analog (BTA) Sensors with Display
- Pendulum Driver
- Levitating Ping Pong Ball
- DCU-Controlled Birthday Candle
- Controlling a Robotic Hand
Vernier Engineering Contest
Vernier recognizes instructors for creative uses of Vernier sensors to introduce engineering concepts and/or practices. Vernier Engineering Award »