Welcome to Building:bit Superkit repository
3.3 Adjustable fan
Adjustable fan
Adjustable fan1. Learning objectives2. Building blocks3. Sensor wiring4. Code analysis5. Write and download the program6. Experimental phenomenon
1. Learning objectives
In this course, we mainly learn how to adjust the fan angle through Python programming.
2. Building blocks
For the building blocks steps, please refer to the installation drawings of [Assembly Course]--[Adjustable fan] in the materials or the building blocks installation brochure.
3. Sensor wiring
Insert the building blocks motor wiring into the M1 interface of the Super:bit expansion board, and the black wiring into the side close to the battery.
Insert the building blocks servo wiring into the S1 interface of the Super:bit expansion board, and the orange servo wiring into the yellow pin of S1.
The potentiometer is connected to the P0P3 pin.

4. Code analysis
For the program of this course, please see the Adjustable-fan.py file.
from microbit import *import WOM_Sensor_Kitimport superbitFirst, import the libraries needed for this lesson from microbit: the WOM_Sensor_Kit library is used for sensors; the superbit library is dedicated to the superbit expansion board.
xxxxxxxxxxfan1 = Image("00990:90900:99999:00909:09900")fan2 = Image("99009:09099:00900:99090:90099")Customize two fan patterns;
xxxxxxxxxxsuperbit.servo270_V2(superbit.S1, 0)display.show(Image.HEART)sleep(400)Initialize the servo to 0 degrees, and the microbit dot matrix displays a heart;
xxxxxxxxxxwhile True:angle = (WOM_Sensor_Kit.WOM_Knob(pin0)/3.76)superbit.servo270_V2(superbit.S1, int(angle))superbit.motor_control(superbit.M1, 255, 0)display.show(fan1)sleep(100)display.show(fan2)sleep(100)In an infinite loop, read the value of the knob and convert it into an angle, control the 270° servo on S1 to rotate to the corresponding angle; then let the M1 motor rotate forward at a speed of 255 to keep the fan running; then display the two fan patterns in turn, each pattern is displayed for 100 milliseconds, forming an animation effect of fan rotation.
5. 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 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 the microbit mainboard with a microUSB data cable, and 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.
6. Experimental phenomenon
After the program runs successfully, turn on the power switch, a heart pattern will be displayed on the microbit dot matrix, and the adjustable fan will rotate at the maximum speed of 255. Turning the potentiometer can change the angle of the adjustable fan. At the same time, we can see that the microbit dot matrix will display a dynamic windmill rotation pattern.