micro:bit How to use a servo to make a waving hand

Waving Hand made with servo and micro:bit

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 making the box and hand, please purchase the digital download. It gives complete directions for creating the box, attaching the servo, and it has three different coding ideas.

Programming Language

MakeCode Blocks

Difficulty Level

Beginner

Ages 11 and up

Materials

All materials are optional. This example can be done entirely within the MakeCode for micro:bit environment on your computer.

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’ll need the following:

  • Crafting supplies to decorate hand
  • Hot glue or some other adhesive to attach the hand to the servo
  • Hand cutout

Designing the Project

Develop the problem statement

Here is the problem statement:

Rotate the servo between 0° 180°

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, but does take 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°
      • Rotate servo to 180°
    • 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 micro:bit, and make it more transferrable into MakeCode Blocks. I’m going to add pseudocode to represent a variable, which we’ll include when we write the actual code. I’m also going to address an issue called a ‘Race Condition’, which is can occur when a physical device can’t perform commands as fast as the computer code is sending them:

  • On Start
    • Create variable ‘Moving’ and set to Off, or 0
    • Rotate servo to 90°
  • On Button A pushed
    • set Moving to ‘On’
    • While (Moving)
      • Rotate servo to 0°
      • Pause for 0.5 seconds (500 milliseconds)
      • Rotate servo to 180°
      • Pause for 0.5 seconds
    • End While
  • On Button B pushed
    • Set Moving to ‘Off’
    • Pause for 0.5 seconds
    • Rotate servo to 90°

Write and Test the Code

Here is the code I wrote:

Code for turning a servo

Concepts Involved

Math used

  • Geometry and the concept of degrees

Programming Concepts

  • Loops
  • Conditional Logic
  • Boolean Logic