Arduino Lesson 14. Servo Motors - Adafruit Industries

1y ago
7 Views
2 Downloads
735.94 KB
15 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Grady Mosby
Transcription

Arduino Lesson 14. Servo MotorsCreated by Simon sson-14-servo-motorsLast updated on 2022-04-25 06:14:01 PM EDT Adafruit IndustriesPage 1 of 15

Table of ContentsOverview3Parts3 Part QtyThe Breadboard Layout for 'Sweep'9If the Servo Misbehaves10Arduino Code for 'Sweep'10The Breadboard Layout for 'Knob'12Arduino Code for 'Knob'13Servo Motors13Inside a Servo14Other Things to Do14 Adafruit IndustriesPage 2 of 15

OverviewIn 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 willadd a pot to control the position of the servo.PartsTo build the projects described in this lesson, you will need the following parts. Adafruit IndustriesPage 3 of 15

PartQtyServo Motor 1 Adafruit IndustriesPage 4 of 15

10 kΩ variable resistor (pot)1 Adafruit IndustriesPage 5 of 15

Half-size Breadboard1 Adafruit IndustriesPage 6 of 15

Arduino Uno R31 Adafruit IndustriesPage 7 of 15

Jumper wire pack1100 µF capacitorOptional Adafruit IndustriesPage 8 of 15

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 isthe control lead and this is usually orange or yellow. This control lead is connected todigital pin 9.The servo is conveniently terminated in a socket into which we can push jumperwires, to link it to the breadboard and then to the Arduino. Adafruit IndustriesPage 9 of 15

If the Servo MisbehavesYour servo may behave erratically, and you may find that this only happens when theArduino is plugged into certain USB ports. This is because the servo draws quite a lotof power, especially as the motor is starting up, and this sudden high demand can beenough 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 (470uFor greater) between GND and 5V on the breadboard.The capacitor acts as a reservoir of electricity for the motor to use, so that when itstarts, 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 to5V. The negative lead is also often marked with a '-' symbol.Arduino Code for 'Sweep'Load up the following sketch onto your Arduino. You should find that the servoimmediately 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 ArduinoExamples under the folder 'servo'. You can if you prefer just run that sketch./*Adafruit Arduino - Lesson 14. Sweep*/#include <Servo.h>int servoPin 9; Adafruit IndustriesPage 10 of 15

Servo servo;int angle 0;// servo position in degreesvoid setup(){servo.attach(servoPin);}void loop(){// scan from 0 to 180 degreesfor(angle 0; angle < 180; angle ){servo.write(angle);delay(15);}// now scan back from 180 to 0 degreesfor(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, anArduino library has been created so that you can just instruct the servo to turn to aparticular angle.The commands for using a servo are like built-in Arduino commands, but because youare not always going to be using a servo in your projects, they are kept in somethingcalled a library. If you are going to use commands in the servo library, you need to tellthe Arduino IDE that you are using the library with this command:#include <Servo.h>As usual, we then use a variable 'servoPin' to define the pin that is to control theservo.This line:Servo servo;defines a new variable 'servo' of type 'Servo'. The library has provided us with a newtype, like 'int' or 'float' that represents a servo. You can actually define up to eightservos in this way, so if we had two servos, then we could write something like this: Adafruit IndustriesPage 11 of 15

Servo servo1;Servo servo2;In the 'setup' function we need to link the 'servo' variable to the pin that will controlthe servo using this command:servo.attach(servoPin);The variable 'angle' is used to contain the current angle of the servo in degrees. Inthe 'loop' function, we use two 'for' loops to first increase the angle in one directionand 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.The Breadboard Layout for 'Knob'Our next step is to add a pot so that we can control the position of the servo byturning the knob.You just need to add the pot and a lead from its slider to A0 on the Arduino. Adafruit IndustriesPage 12 of 15

Arduino Code for 'Knob'The code to make the servo follow the knob's position is simpler than to make itsweep./*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);int angle reading / 6;servo.write(angle);}// 0 to 1023// 0 to 180-ishThere is now a second variable called 'potPin'.To set the position of the servo, we take an analog reading from A0. This gives us avalue 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.Servo MotorsThe position of the servo motor is set by the length of a pulse. The servo expects toreceive 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 centreposition and if it is 2 milliseconds it will be at 180 degrees. Adafruit IndustriesPage 13 of 15

The end points of the servo can vary and many servos only turn through about 170degrees. You can also buy 'continuous' servos that can rotate through the full 360degrees.Inside a ServoThe 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 willnot go back together properly.Other Things to DoOpen the 'sweep' sketch and try reducing the delays from 15 milliseconds to say 5milliseconds. Notice how much faster the servo turns.Try modifying the 'knob' sketch so that instead of taking the servo angle from theposition of the knob, it takes it from the Serial Monitor, so that you can control theservo from your computer.Hint: to make your sketch read the number of degrees from the Serial Monitor, youcan use the function Serial.parseInt(). This reads a number from the Serial Monitor.Click Here for the Next Lessonhttps://adafru.it/aUFAbout the Author Adafruit IndustriesPage 14 of 15

Simon Monk is author of a number of books relating to Open Source Hardware. Thefollowing 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 (https://adafru.it/aM5). Adafruit IndustriesPage 15 of 15

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.

Related Documents:

slider in Processing. We will use firmata for the communication between Processing and Arduino and have the servo library handle the servo control from the Arduino board. Just upload the Servo firmata onto your Arduino board, in Arduino go to File Examples Firmata Servo Firmata

Oct 06, 2010 · Servo - Preferably Spektrum . Choose correct servo horn adapter using servo horn chart and press onto servo. 3. After servo is centered, install servo horn on servo using the screw provided with your servo so that in the centered position the servo

Arduino and Servo Motor Tutorial By: Matthew Jourden Brighton High School b. Arduino Shield: mounts on top of Arduino Uno board lining up the pins. The use of the shield is to expand the flexibility of the Arduino Uno board. 2. Link the Arduino Shield on top of the Arduino Board linking the proper pins to each port

Arduino compatible components. Personal computer running Arduino software Arduino software is free to download and use from: www.arduino.cc Arduino board Such as: Arduino Uno Freetronics Eleven Genuino Uno or any Arduino compatible board that has a standard Arduino UNO header l

Servo solenoid valve, pilot operated Servo-distributeur, piloté Regelventil für Blockeinbau Cartridge-type servo solenoid valve Servo-distributeur en cartouche HRV: High Response Valve HRV – Regelventil NG 6 HRV – Servo solenoid valve NG 6 HRV – Servo-distributeur NG 6 Regelventil NG 6 Servo solenoid valve NG 6 Servo-distributeur NG 6

arduino-00 -win.zip Recommended Path c:\Program Files\ ( - version #) Step 3: Shortcut Icon Open c:\program files\arduino-00 Right Click Arduino.exe (send to Desktop (create shortcut)) \ ( - version #) Step 4: Plug In Your Arduino Plug your Arduino in: Using the included USB cable, plug your Arduino board into a free USB port. Wait for a box to .

Hence we given interesting top five easy to make Arduino projects with code and library link. Happy learning Arduino 1. Heart Rate Monitor AD8232 Interface Arduino 2. Fingerprint sensor-scanner with Arduino 3. Giving Voice Recognition Ability to Arduino 4. Soil Moisture Sensor and Arduino 5. How to Interface RFID with Arduino?

MaxPlus Motors SERVO MOTORS 2 Parker Hannifin Corporation Compumotor Division Automation compumotor.com MaxPlus Brushless Rotary Servo Motors The MaxPlus family is redefining performance, flexibility and reliability, with the industry's broadest range of brushless servo motors - from 1 1/2" to 12" (40 mm to 320 mm). Each unit