MakeCode for a Standard/Hobby Servo Motor

You can learn the basics of Microsoft MakeCode here.

You will need the Crickit block extensions installed. See this guide on doing so.

With Crickit support in MakeCode, you should have a green CRICKIT block group available. Clicking on that group pops out various blocks for Crickit capabilities including those related to motor control as shown below.

Be sure you use the Servo blocks under the CRICKIT group and NOT the Servo blocks under the PINS block group!

Moving the Servo via Buttons

We want to turn the servo horn on command from one side (0 degrees) to the other (180 degrees). 

This program will move a servo to the zero degree point when the Circuit Playground Express button A is pressed. It will move the servo to 180 degrees when button B is pressed.

Depending on your servo, you may find the angle of motion isn't a full 180 degrees!

This trips up everyone the first time they use a servo - while the servos are often talked about in terms of 0 to 180 degree motion, there can be variation from servo to servo. Some metal gear servos only move 90 degrees. Others may need longer 'pulse lengths' and we won't go into that there. There's nothing wrong with your servo just because it doesn't move a full 180 degrees! Visit https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit/makecode-servos#precise-pulses for more details on how to customize the pulse lengths!

Moving a Servo in a Slower, Controlled Fashion

The code below creates a stepped movement from 0 to 180 and back again. The timing is variable with the pause blocks. This is a common use in terms of things like wings flapping or other movements.

If you find the movement a bit jerky, you can increase the for index from 0 to and set it to 180 then take out the divide by 10 in the crickit set servo 1 angle to block. You might have to play with the pause time then if you feel the motion is too slow.

Example for Crickit for Circuit Playground Express and Crickit for Feather

Example for Crickit for micro:bit

This guide was first published on Jul 04, 2018. It was last updated on Jul 04, 2018.
This page (MakeCode) was last updated on Sep 23, 2020.