Welcome to Building:bit Superkit repository
6.Drive motor
Drive motor
Drive motor1. Learning objectives2. Motor wiring3. Code analysis4. Write and download the program5. Experimental phenomenon
1. Learning objectives
In this course, we mainly learn how to drive the motor connected to the superbit expansion board through Python programming.
The motor interface is located on the expansion board as shown in the figure below.

2. Motor wiring
The motor wiring is inserted into the M1 interface of the Super:bit expansion board, and the black wiring is close to the battery side, as shown in the figure below.

3. Code analysis
For the program of this course, please see the Drive 270° Building Block Servo.py file.
from 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)display.show(Image.HEART): Display the heart pattern on the microbit dot matrix;
xwhile True:superbit.motor_control(superbit.M1, 255, 0)microbit.sleep(1000)superbit.motor_control(superbit.M1, 0, 0)microbit.sleep(1000)superbit.motor_control(superbit.M1, -255, 0)microbit.sleep(1000)Infinite loop to control the state of the motor.
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, use the Tab key for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check if there are any errors in our code. If a cursor or underline appears in 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 microbit mainboard with a microUSB data cable, 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 connected to the computer normally via the microUSB data cable and the Superbit Python library has been imported.
5. Experimental phenomenon
After the program is downloaded successfully, the micro:bit dot matrix will display a heart pattern, as shown in the figure below. Then we can see that the motor starts to rotate forward for 1 second, stops for 1 second, and reverses for 1 second, and keeps looping in this state.

If you need to restart, press the reset button on the back of the micro:bit motherboard.