Arduino Lesson 14. Servo Motors

Similar documents
Arduino Lesson 13. DC Motors. Created by Simon Monk

Arduino Lesson 9. Sensing Light

Arduino Lesson 16. Stepper Motors

Arduino Lesson 0. Getting Started

Arduino Lesson 1. Blink

Arduino Lesson 4. Eight LEDs and a Shift Register

Arduino Lesson 5. The Serial Monitor

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Arduino Lesson 17. Sending Movement Detector

Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor

Bluetooth + USB 16 Servo Controller [RKI-1005 & RKI-1205]

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Character LCDs. Created by Ladyada. Last updated on :45:29 PM EDT

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

ARDUINO SEVERINO SERIAL SINGLE SIDED VERSION 3 S3v3 (REVISION 2) USER MANUAL

SSH to BeagleBone Black over USB

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing

2.2" TFT Display. Created by Ladyada. Last updated on :15:09 PM EDT

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable. Created by Simon Monk

DS1307 Real Time Clock Breakout Board Kit

Basic Pulse Width Modulation

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

Set up and Blink - Simulink with Arduino

Servo Motors (SensorDAQ only) Evaluation copy. Vernier Digital Control Unit (DCU) LabQuest or LabPro power supply

TEECES DOME LIGHTING SYSTEMS

INTRODUCTION TO SERIAL ARM

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

Adafruit SHT31-D Temperature & Humidity Sensor Breakout

Designing a Schematic and Layout in PCB Artist

Lab 6 Introduction to Serial and Wireless Communication

Introduction to Arduino

Work with Arduino Hardware

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

Playing sounds and using buttons with Raspberry Pi

Adding a Real Time Clock to Raspberry Pi

Board also Supports MicroBridge

Arduino DUE + DAC MCP4922 (SPI)

ECEN 1400, Introduction to Analog and Digital Electronics

DEPARTMENT OF ELECTRONICS ENGINEERING

Basic DC Motor Circuits. Living with the Lab Gerald Recktenwald Portland State University

CHAPTER 11: Flip Flops

Basic DC Motor Circuits

How to Make a Pogo Pin Test Jig. Created by Tyler Cooper

1.8" TFT Display Breakout and Shield

Sending an SMS with Temboo

Six-servo Robot Arm. DAGU Hi-Tech Electronic Co., LTD Six-servo Robot Arm

Home Security System for Automatic Doors

Tiny Arduino Music Visualizer

Thermistor. Created by Ladyada. Last updated on :30:46 PM EDT

ε: Voltage output of Signal Generator (also called the Source voltage or Applied

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

A REST API for Arduino & the CC3000 WiFi Chip

Electronic Brick of Current Sensor

ABB Drives. User s Manual HTL Encoder Interface FEN-31

Arduino Motor Shield (L298) Manual

IR Communication a learn.sparkfun.com tutorial

Adafruit Proto Shield for Arduino

1 Coffee cooling : Part B : automated data acquisition

Lab 3 - DC Circuits and Ohm s Law

BUILDING INSTRUCTIONS

Lab 3 Rectifier Circuits

Lab 11 Digital Dice. Figure Digital Dice Circuit on NI ELVIS II Workstation

AXE114S BINARY CLOCK. revolution Revolution Education Ltd. Web: Version /09/08 AXE114.PMD.

Electronics 5: Arduino, PWM, Mosfetts and Motors

Arduino Lab 1 - The Voltage Divider

Arduino project. Arduino board. Serial transmission

revolution Contents: Introduction Power 28-pin Project Board with input/output cables

AirCasting Particle Monitor Bill of Materials

Ocean Controls RC Servo Motor Controller

RGB LED Strips. Created by lady ada. Last updated on :00:18 PM EST

Servo Info and Centering

Surveillance System Using Wireless Sensor Networks

Building the AMP Amplifier

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

Pulse width modulation

RC2200DK Demonstration Kit User Manual

TSL2561 Luminosity Sensor

1602 LCD adopts standard 14 pins(no backlight) or 16pins(with backlight) interface, Instruction of each pin interface is as follows:

DC Motor with Shaft Encoder

ABB Drives. User s Manual. Pulse Encoder Interface Module RTAC-01

8 coil stator 11 coil stator

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Building A Computer: A Beginners Guide

MCP4725 Digital to Analog Converter Hookup Guide

The self-starting solar-powered Stirling engine

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

Using an IR Remote with a Raspberry Pi Media Center

MANUAL FOR RX700 LR and NR

Capacitive Touch Sensor Project:

ADC-20/ADC-24 Terminal Board. User Guide DO117-5

Matrix and 7-Segment LED Backpack with the Raspberry Pi

C4DI Arduino tutorial 4 Things beginning with the letter i

Resistance, Ohm s Law, and the Temperature of a Light Bulb Filament

PRODUCTIVITY THROUGH INNOVATION 600 CONTROL DIRECT DRIVE TECHNICAL/OPERATION MANUAL

Experiment 3: Magnetic Fields of a Bar Magnet and Helmholtz Coil

Lab E1: Introduction to Circuits

Transcription:

Arduino Lesson 14. Servo Motors Created by Simon Monk Last updated on 2013-06-11 08:16:06 PM EDT

Guide Contents Guide Contents Overview Parts Part Qty The Breadboard Layout for 'Sweep' If the Servo Misbehaves Arduino Code for 'Sweep' The Breadboard Layout for 'Knob' Arduino Code for 'Knob' Servo Motors Inside a Servo Other Things to Do 2 3 4 4 4 6 7 8 10 11 12 13 14 Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 2 of 14

Overview In this lesson, you will learn how to control a servo motor using an Arduino. Firstly, you will get the servo to sweep back and forth automatically and then you will add a pot to control the position of the servo. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 3 of 14

Parts To build the projects described in this lesson, you will need the following parts. Part Qty Servo Motor 1 10 kω variable resistor (pot) 1 Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 4 of 14

Half-size Breadboard 1 Arduino Uno R3 1 Jumper wire pack 1 100 µf capacitor Optional Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 5 of 14

The Breadboard Layout for 'Sweep' For this experiment, the only thing connected to the Arduino is the servo motor. The servo motor has three leads. The color of the leads varies between servo motors, but the red lead is always 5V and GND will either be black or brown. The other lead is the control lead and this is usually orange or yellow. This control lead is connected to digital pin 9. The servo is conveniently terminated in a socket into which we can push jumper wires, to link it to the breadboard and then to the Arduino. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 6 of 14

If the Servo Misbehaves Your servo may behave erratically, and you may find that this only happens when the Arduino is plugged into certain USB ports. This is because the servo draws quite a lot of power, especially as the motor is starting up, and this sudden high demand can be enough to drop the voltage on the Arduino board, so that it resets itself. If this happens, then you can usually cure it by adding a high value capacitor (470uF or greater) between GND and 5V on the breadboard. The capacitor acts as a reservoir of electricity for the motor to use, so that when it starts, it takes charge from the capacitor as well as the Arduino supply. The longer lead of the capacitor is the positive lead and this should be connected to 5V. The negative lead is also often marked with a '-' symbol. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 7 of 14

Arduino Code for 'Sweep' Load up the following sketch onto your Arduino. You should find that the servo immediately begins to turn first in one direction and then back in the other. The sketch is based on the standard 'sweep' sketch that you can find in the Arduino Examples under the folder 'servo'. You can if you prefer just run that sketch. /* Adafruit Arduino - Lesson 14. Sweep */ #include <Servo.h> int servopin = 9; Servo servo; int angle = 0; // servo position in degrees void setup() { servo.attach(servopin); } void loop() { // scan from 0 to 180 degrees for(angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for(angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); } } Servo motors are controlled by a series of pulses and to make it easy to use them, an Arduino library has been created so that you can just instruct the servo to turn to a particular angle. The commands for using a servo are like built-in Arduino commands, but because you are not always going to be using a servo in your projects, they are kept in something called a library. If you are going to use commands in the servo library, you need to tell the Arduino IDE that you are using the library with this command: Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 8 of 14

#include <Servo.h> As usual, we then use a variable 'servopin' to define the pin that is to control the servo. This line: Servo servo; defines a new variable 'servo' of type 'Servo'. The library has provided us with a new type, like 'int' or 'float' that represents a servo. You can actually define up to eight servos in this way, so if we had two servos, then we could write something like this: Servo servo1; Servo servo2; In the 'setup' function we need to link the 'servo' variable to the pin that will control the servo using this command: servo.attach(servopin); The variable 'angle' is used to contain the current angle of the servo in degrees. In the 'loop' function, we use two 'for' loops to first increase the angle in one direction and then back in the other when it gets to 180 degrees. The command: servo.write(angle); tells the servo to update its position to the angle supplied as a parameter. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 9 of 14

The Breadboard Layout for 'Knob' Our next step is to add a pot so that we can control the position of the servo by turning the knob. You just need to add the pot and a lead from its slider to A0 on the Arduino. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 10 of 14

Arduino Code for 'Knob' The code to make the servo follow the knob's position is simpler than to make it sweep. /* Adafruit Arduino - Lesson 14. Knob */ #include <Servo.h> int potpin = 0; int servopin = 9; Servo servo; void setup() { servo.attach(servopin); } void loop() { int reading = analogread(potpin); // 0 to 1023 int angle = reading / 6; // 0 to 180-ish servo.write(angle); } There is now a second variable called 'potpin'. To set the position of the servo, we take an analog reading from A0. This gives us a value of between 0 and 1023. Since the servo can only rotate through 180 degrees, we need to scale this down. Dividing it by six will give us an angle between 0 and 170, which will do just fine. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 11 of 14

Servo Motors The position of the servo motor is set by the length of a pulse. The servo expects to receive a pulse roughly every 20 milliseconds. If that pulse is high for 1 millisecond, then the servo angle will be zero, if it is 1.5 milliseconds, then it will be at its centre position and if it is 2 milliseconds it will be at 180 degrees. The end points of the servo can vary and many servos only turn through about 170 degrees. You can also buy 'continuous' servos that can rotate through the full 360 degrees. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 12 of 14

Inside a Servo The following short video shows you what is going on inside a servo. Beware though, if you dismantle your servo like this, there is a good chance that it will not go back together properly. Adafruit Industries http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors Page 13 of 14

Other Things to Do Open the 'sweep' sketch and try reducing the delays from 15 milliseconds to say 5 milliseconds. Notice how much faster the servo turns. Try modifying the 'knob' sketch so that instead of taking the servo angle from the position of the knob, it takes it from the Serial Monitor, so that you can control the servo from your computer. Hint: to make your sketch read the number of degrees from the Serial Monitor, you can use the function Serial.parseInt(). This reads a number from the Serial Monitor. About the Author Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil Genius (http://adafru.it/868) and Programming the Raspberry Pi (http://adafru.it/am5). Adafruit Industries Last Updated: 2013-06-11 08:16:07 PM EDT Page 14 of 14