Welcome to Building:bit Superkit repository
5.Drive 180° servo
Drive 180° servo
Drive 180° servo1. Learning Objectives2. Servo Wiring3. Code Analysis4. Write and download the program5. Experimental phenomenon
1. Learning Objectives
In this course, we mainly learn how to drive the servo connected to the superbit expansion board through Python programming.
The servo interface is located on the expansion board as shown in the figure below.

2. Servo Wiring
Insert the servo wiring into the S1 interface of the Super:bit expansion board, and the orange servo wiring into the yellow pin of S1, as shown in the figure below.

3. Code Analysis
For the program of this course, please see the Drive 180° servo.py file.
xxxxxxxxxxfrom microbit import *import microbitimport superbitFirst, import the libraries needed for this lesson from microbit: the superbit library is dedicated to the superbit expansion board;
xxxxxxxxxxdisplay.show(Image.HEART)superbit.servo180(superbit.S1, 0)microbit.sleep(1000)display.show(Image.HEART): Display a heart pattern on the microbit dot matrix;
superbit.servo180(superbit.S1, 0): Initialize the servo to 0°;
microbit.sleep(1000): Delay 1 second.
xxxxxxxxxxwhile True:superbit.servo180(superbit.S1, 0)microbit.sleep(1000)superbit.servo180(superbit.S1, 90)microbit.sleep(1000)superbit.servo180(superbit.S1, 180)microbit.sleep(1000)superbit.servo180(superbit.S1, 90)microbit.sleep(1000)Infinite loop to control the servo to rotate to different angles.
4. Write and download the program
- Open the Mu software and enter the code in the editing window. Note! All English and symbols should be entered in English mode, use the Tab key (tab key) for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check whether our code has any errors. If a cursor or underline appears on a line, it means a syntax error. Please check and modify it. If there is no error, the lower left corner will prompt that there is no problem with the detection.

- Click the 'REPL' button to check whether the Superbit library has been downloaded. If not, please refer to [Preparation before class] --> [2.4 Python Programming Guide].

- After the program is written, connect the computer and the microbit mainboard with a microUSB data cable. Please click the 'Flash' button to download the program to the micro:bit mainboard. (You need to click the 'REPL' button again to turn off the import library file function before you can download the program normally).

- If the download fails, please confirm whether the microbit is properly connected to the computer via the microUSB data cable and the Superbit Python library has been imported.
5. Experimental phenomenon
After the program is successfully downloaded, the micro:bit dot matrix will display a heart pattern, and then we can see that the servo starts to rotate, 0°->90°->180°->90°, with a time interval of 1 second.
If you need to restart, press the reset button on the back of the micro:bit motherboard.