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 again use the light sensor in your basic kit as an example to drive the servo arm.

By now you should be confident with the idea that analogue sensors can be swapped about easily; with some tweaking of code you will be able to get the swapped sensor to work reasonably well before needing to write more complex code for very specific requirements.

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 articulated assemblies.

Fortunately, since you are only producing scale models of the 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, 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:

https://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors/overview

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 shipping times, but their website provides plenty of information on various different types of servos: https://www.servocity.com/servos/hitec-servos


Libraries Used

(learn how to import them in the Build IDE):


Code

 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
// set up a servo for use
Servo myservo;

// code in this setup function runs just once, when Photon is powered up or reset
void setup() {
    Serial.begin(9600);             // Open a connection via the Serial port – useful for debugging
    myservo.attach(D0);

    delay(5000);                    // Common practice to allow board to 'settle' after connecting online
}


// code in this loop function runs forever, until you cut power!
// for the A/D blackbox, there is nothing much in here except to update the Blynk App
void loop() {
    updateServo();
}


void updateServo() {
    int sensorReading = analogRead(A0);

    int scaled = map(sensorReading, 0, 4095, 0, 180);   // tweak these numbers accordingly!
    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(10);
}