In this lesson, you will learn to create movement with the Hummingbird using position servos. The position servo is a motor that moves to a particular angle. The Hummingbird position servo can move to any angle from 0° to 180°.
Lesson 4 – Position Servos
1 Position Servo, 1 Single Color LED
Plug in the position servo to SERVOS port 1 on the Bit. Make sure the black wire is aligned to ‘-,’ the red wire to ‘+,’ and the white wire to ‘S.’
Write a program that uses a loop to move the position servo to four different positions. The servo should stop at each position eight times.
In the for loop in Exercise 1, the variable i changes from 0 to 4 as the loop is executed, but this variable is not used inside the loop. You can also use the variable i within the loop, and this is very helpful if you want the servo to move more slowly. For example, in this code i changes from 0 to 179, and the value of i is used to set the position of the servo. The servo stays at each angle for 0.1 s. This code will move the servo slowly from 0° to 180°.
You can use arithmetic operators in Python to perform addition (+), subtraction (-), multiplication (*), and division (/) with numbers and variables. This code uses a for loop that includes a subtraction operator. What do you think this code will do? Make a hypothesis, and then try it out.
Connect a single color LED to the Hummingbird. Write a program that slowly moves the position servo from 0° to 100° as the single color LED grows in brightness from 0 to 100. Then the servo should move slowly back to 0° as the brightness of the LED dims back to 0.
Modify you code from Exercise 5 so that the position servo moves from 0° to 180° as the LED grows in brightness from 0 to 100. Then the servo should move from 180° back to 0° as the brightness of the LED dims from 100 back to 0. Hint: You will need an arithmetic operator.