Description
Servo motors are great for projects where you want to make something else turn. This could be anything from a hand, like in the video below, to a vehicle. This project shows how to write a program to make a servo turn. For information on how to make the box and hand, please purchase the digital download. It gives complete directions for creating the box and attaching the servo, and it has three different coding ideas.
Programming Language
MakeCode Blocks
Difficulty Level
Intermediate
Age Range
11 and up
Materials
The materials needed to create this project are:
- micro:bit with microUSB cable
- battery pack (optional)
- TowerPro SG92R mini standard servo
- 3 alligator clips with male connectors
If you plan to make the waving hand, you will need:
- Crafting supplies to decorate hand
- Hot glue or some other adhesive to attach the hand to the servo
- Hand cutout
Programming the micro:bit
Develop the problem statement
I am going to make my program have a switch of some sort to turn it off and on, so I am using this problem statement:
Turn the servo off and on. When it is on, turn the servo back and forth between 0° and 180° until it is turned off
Write the algorithm
The algorithm is written as generically as possible, taking into account that I am using an event-driven language:
- When ‘servo on’ signaled:
- Set servo to on
- Repeat while servo on:
- Rotate servo to 0 degrees
- Rotate servo to 180 degrees
- End Repeat
- End ‘servo on’ event
- When ‘servo off’ signaled
- Set servo to off
- End ‘servo off’ event
Write the pseudocode
This is the place where I want to target the fact that the servo doesn’t operate at the same speed as the code. Because a machine is slower than computer code, an event called a ‘Race Condition’ can occur. This is where the device misses some of the commands since it’s still processing older commands:
- On Start
- Create variable ‘Moving’ and set to Off, or 0
- Rotate servo to 90 degrees
- On Button A pushed
- Set Moving to ‘On’
- While Moving
- Rotate servo to 0 degrees
- Pause for 0.5 seconds (500 illiseconds)
- Rotate servo to 180 degrees
- Pause for 0.5 secondsĀ
- End While
- On Button B pushed
- Set Moving to ‘Off’
- Pause for 0.5 seconds
- Rotate servo to 90 degrees
Write and test the code
Academic Connections
Programming Concepts
- Loops
- Conditional Logic
Math
- Geometry and the concept of degrees