Servos
Servos are geared down motors with one important feature – they provide angular feedback which means you can tell it to turn to a specific angle (within 0-180°). This makes for very easy control of arm linkages, open/closing of doors/flaps, or creating metronome-like movement.
In this recipe, we will use a potentiometer (variable resistor that operates like a volume control knob) to change the angular position of the servo arm.
Hobby servos have a limitation on their strength, i.e. torque in turning its arm. Despite it’s internal gearing that gives it much more torque than a plain DC motor, you will reach the limits of what a hobby servo quickly. While you probably won’t be able to get a single servo to open/close a door, a cheap servo designed with suitable mechanical leverage can easily serve as miniature robotic arms or in articulated assemblies.
If you are producing scale models of an interactive prototype, hobby servos should provide more than enough power. The only thing to be aware of is the amount of electrical power you need to feed to a large amount of servos – they add up very quickly.
Servos come in a wide variety of torque/speed ratings – the high-performance ones tend to creep up in price quite significantly. As with any component, check the datasheet for their operational ratings.
More on Servos
Here’s a nice video comparing the different types of ‘hobby’ servos out there. While this is presented from the perspective of remote-controlled flight hobbyists, it is still offers a useful introduction to this world of affordable servos:
Here’s also a web article from Adafruit covering the same oveview on servos.
A good place to check out a wide variety of hobby servos is ServoCity. Because it’s based in the US, I don’t suggest ordering due to the long and expensive shipping times, but their website provides plenty of information on various different types of servos available on the market.
Wiring & Code
For a refresher on how to use the code in this recipe, click here.
Select a microcontroller platform in the tabs below to view the wiring and code meant for the platform:
-
Breadboard diagram
Libraries
Install the following libraries via your Arduino IDE’s Library Manager, if you haven’t already:
Servo
This is a built-in library, you don’t need to install it (but you’ll still need the#include "Servo.h"
statement)
Code
For a refresher on how to use the code in this recipe, click here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "Servo.h" // tells the Arduino IDE we need to use this library Servo myservo; // set up a servo for use // code in this setup function runs just once, when Photon is powered up or reset void setup() { Serial.begin(115200); // Open a connection via the Serial port – useful for debugging myservo.attach(9); delay(5000); // Common practice to allow board to 'settle' at startup } // code in this loop function runs forever, until you cut power! void loop() { updateServo(); } // CUSTOM FUNCTIONS void updateServo() { // here we are assuming just a basic analogue sensor attached to A0 int sensorReading = analogRead(A0); int scaled = map(sensorReading, 0, 1023, 0, 180); // tweak these numbers accordingly depending on your sensor type! scaled = constrain(scaled, 0, 180); // make sure scaled ranged is within 0-180° Serial.println(scaled); myservo.write(scaled); // add a tiny delay so as not to overwhelm the servo delay(5); } /* Please note that the code provided here is licensed under the MIT license. The MIT License (MIT) Copyright © 2024 Chuan Khoo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-
COMING SOON…
More code samples for this component for other platforms will be added here over time.