Arduino Full Stack
  • Preface
  • Part 1 - Introduction
  • Chapter 1. Getting Started with Arduino
    • 1.1 Arduino UNO R3
    • 1.2 LVR Arduino Exntesion Board
    • 1.3 Sensor Kits for Arduino
    • 1.4 Race Car Chassis Model
    • 1.5 Arduino Online Resource
    • 1.6 Arduino IDE for Prototyping
    • 1.7 FIRST Arduino Project - Hello World
  • Chapter 2. Programming Grammar
    • 2.1 AVR C
    • 2.2 Digital IO
    • 2.3 Analog IO
    • 2.4 More About IO
    • 2.5 Time Functions
    • 2.6 Interrupt Functions
    • 2.7 Communication Classes
    • 2.8 Arduino Libraries
  • Part 2 - Sensors
  • Chapter 3. LED Sensors
    • 3.1 LED Blinking
    • 3.2 LED SMD RGB
    • 3.3 Mini LED Red Green GND-Shared
    • 3.4 LED RGB GND-Shared
    • 3.5 Photoresistor
    • 3.6 LED Light Cup
    • 3.7 LED Red Green GND-Shared
    • 3.8 LED 7 Colors
  • Chapter 4. Infrared Sensors
    • 4.1 Infrared Emitter and Receiver
    • 4.2 Laser Emitter
    • 4.3 Infrared Remote Control
    • 4.4 IR Obstacle Avoidance
    • 4.5 IR Line Tracking
  • Chapter 5. Audio Sensors
    • 5.1 Passive Buzzer
    • 5.2 Active Buzzer
    • 5.3 Microphone Big Sound
    • 5.4 Microphone
  • Chapter 6. Magnetic Sensors
    • 6.1 Hall Magnetic Sensor
    • 6.2 Mini Reed
    • 6.3 Linear Hall Magnetic Sensor
    • 6.4 Reed
    • 6.5 Analog Hall
  • Chapter 7. Touch Sensors
    • 7.1 Tap Sensor
    • 7.2 Shock Sensor
    • 7.3 Touch Sensor
  • Chapter 8. Thermist Sensors
    • 8.1 Temperature Sensor - DS18B20
    • 8.2 Analog Thermister
    • 8.3 Digital Temperature
    • 8.4 Temperature and Humidity Sensor
  • Chapter 9. Switches
    • 9.1 Button
    • 9.2 Switch -Mercury Tilt
    • 9.3 Switch - Ball
    • 9.4 Relay
  • Chapter 10. Other Sensors
    • 10.1 Joystick
    • 10.2 Flame
    • 10.3 Pulse Monitor
    • 10.4 Rotary Encoders
  • Chapter 11. More Discussions on Sensors
    • 11.1 Arducam
    • 11.2 IMU - BNO055
  • Part 3 - Motors
  • Chapter 12. Motors
    • 12.1 DC Motor
    • 12.2 AC Motor
    • 12.3 Stepper Motor
    • 12.4 Servo Motor
  • Part 4 - Display
  • Chapter 13. Display
    • 13.1 Nixie Light LG5011B
    • 13.2 Crystal Display 1602A
    • 13.3 Crystal Display 12864
  • Part 5 - Communication
  • Chapter 14. Communication
    • 14.1 Serial
    • 14.2 Wifi - ESP8266
    • 14.3 Bluetooth
    • 14.4 Integrate Wifi & Bluetooth - ESP32
  • Part 6 - Arduino Based Mini Automated Vehicle
  • Chapter 15. Assemble a Mini Automated Vehicle
    • 15.1 4-Wheel DC-Motor Driven
    • 15.2 Arduino UNO and LVR Arduino Extension Board
    • 15.3 Speed Sensors
    • 15.4 Servo Motor for Pan Tilt Control
    • 15.5 Tracking Sensor
  • Chapter 16. Remote Control
    • 16.1 IR Remote Control
    • 16.2 Bluetooth Remote Control
    • 16.3 Wifi Remote Control
    • 16.4 Automatic Control
Powered by GitBook
On this page
  • Example 1: Knob
  • Hardware Wiring
  • Sketch
  • Example 2: Sweep
  • Hardware Wiring
  • Sketch
  • Assignment: Pan Tilt
  • Hardware Wiring
  • Sketch
  1. Chapter 12. Motors

12.4 Servo Motor

Previous12.3 Stepper MotorNextPart 4 - Display

Last updated 7 years ago

We provide a Micro Servo SG90 in our race car kit, which looks like:

Example 1: Knob

Hardware Wiring

Sketch

/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // 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(15);                           // waits for the servo to get there
}

Example 2: Sweep

Hardware Wiring

Sketch

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Assignment: Pan Tilt

Hardware Wiring

Sketch

// http://www.robotoid.com/appnotes/arduino-operating-two-servos.html

#include <Servo.h>

int x = 0;
Servo servoPan;          // Define pan servo
Servo servoTilt;         // Define tilt servo

void setup() { 
  servoPan.attach(10);  // Set pan servo to digital pin 10
  servoTilt.attach(9);  // Set tilt servo to digital pin 9
} 

void loop() {            // Loop through motion tests
  forward();             // Example: move forward
  delay(2000);           // Wait 2000 milliseconds (2 seconds)
  reverse();
  delay(2000);
  turnRight();
  delay(2000);
  turnLeft();
  delay(2000);
  stopRobot();
  delay(2000);
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoPan.write(0);
  servoTilt.write(180);
}

void reverse() {
  servoPan.write(180);
  servoTilt.write(0);
}

void turnRight() {
  servoPan.write(180);
  servoTilt.write(180);
}
void turnLeft() {
  servoPan.write(0);
  servoTilt.write(0);
}

void stopRobot() {
  servoPan.write(90);
  servoTilt.write(90);
}

Directly referenced from .

The code can be found at .

Directly referenced from .

Please refer to .

The hardware wiring is left for students to finish as an in-class assignment. The expected result can be referred to .

Please refer to .

Official Arduino Tutorial - Knob
Examples_Arduino - geek-workshop - essence - _001_Servo - _001_Servo_Knob.ino
Official Arduino Tutorial - Sweep
Examples_Arduino - geek-workshop - essence - _001_Servo - _001_Servo_Sweep.ino
Chapter 15.2 - Pan Tilt Control by 2 Servo Motors
Examples_Arduino - geek-workshop - essence - _001_Servo - _001_Servo_PanTilt.ino
Servo Motor
Servo Example 1 - Knob
Servo Example 2 - Sweep