Lesson 4 – Position Servos

Hummingbird Components

1 Position Servo, 1 Single Color LED

Python Concepts

Math operators

Teacher Materials

Get Access

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°.

If you want to complete this lesson with a robot, try building an animal or a jitterbug.

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.’

To set the position servo to a particular angle, use the setPositionServo() method. This method requires two parameters. The first parameter is the port number of the servo (1-4), and the second is an angle from 0°-180°.

Exercise 1

Try out this sample code. The position servo should move back and forth between two angles five times. Then modify this code to move the servo back and forth from 60° to 120°.

Exercise 2

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°.

Exercise 3

Try out this sample code. Then modify this code to move the servo to only 90°.

Exercise 4

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.

Exercise 5

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.

Extra Challenge

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.

Extra Challenge

Now that you know how to use the position servo, try building Little Bot or a basic mechanism!