In this lesson, we will carry out an interesting experiment by programming on the Micro:bit so as to control Servo with Potentiometer.
25.1 Components to be prepared
25.2 Servo
25.2.1 Servo
Servo is a compact package which consists of a DC Motor,a set of reduction gears to provide torque,a sensor and control circuit board.Most Servos only have a 180-degree range of motion via their“horn”.Servos can output higher torque than a simple DC Motor alone and they are widely used to control motion in model cars,model airplanes, robots,etc.Servos have three wire leads which usually terminate to a male or female 3-pin plug.Two leads are for electric power:Positive (2-VCC, Red wire),Negative (3-GND, Brown wire),and the signal line (1-Signal, Orange wire) as represented in the Servo provided in your Kit.
We will use a 50Hz PWM signal with a duty cycle in a certain range to drive the Servo. The lasting time 0.5ms-2.5ms of PWM single cycle high level corresponds to the Servo angle 0 degrees - 180 degree linearly. Part of the corresponding values are as follows:
As can be seen from the above table, the servo rotates from 0 to 180 degrees, and the corresponding pulse width is 0.5-2.5ms. Then the analog voltage value is written to the micro:bit pin ranging from 25.6 to 128.
25.3 Low level and high level
In circuit, the form of binary (0 and 1) is presented as low level and high level.
Low level is generally equal to ground voltage (0V). High level is generally equal to the operating voltage of components.
The low level of Micro:bit is 0V and high level is 3.3V, as shown below. When IO port on Micro:bit outputs high level, low-power components can be directly driven,like LED.
25.4 Circuit
You should connect the components according to the following circuit diagram:
25.5 MakeCode programming
Next, we will use the online MakeCode Editor to complete the experiment in this lesson.
25.5.1 Start programming
(1) Log in to the website
1. You need to enter the URL in the address bar of Google Browser:
https://makecode.microbit.org/
2. After the website is successfully opened, the interface as shown below will appear:
(2) Import a project
1. In the HOME interface, click the "Import" button to import the external ".hex" file:
In the pop-up dialog box, select the "Import File", as shown in the following figure:
Click the "Choose File"
Find the code file for this lesson:
BBC_Microbit_Sensor\Code\Lesson_25\BlockCode
Select the file in ".hex" format and click the Open:
2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:
3.You can see the following interface when successfully opening the file:
25.5.2 Run the program
1. After the program is written, connect micro:bit and PC with a Micro USB cable.
2. After micro:bit is connected to the computer, you need to first "Pair device". Click the button on the right of in the lower left corner, and then click the option, as shown in the following figure:
Then click in the lower right corner
Then the following dialog box will pop up, select , and then click
After the device is successfully paired, the button changes to
3. Start to download the program to Micro:bit, and click the button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded. You will see that the Servo rotates, as you are turning the Potentiometer, as shown below:
[Note]
If no experimental phenomenon has been detected after clicking the button , you need to click the button on the right of the , and then click the , and observe the situation of the RGB LED again, as shown in the following figure:
If you have problems, please send us an email: support@adeept.com
25.5.3 Learn the code program
The following instruction blocks will be applied in the program. Please see the description of the function as follows:
Read the analog voltage value of the P0 pin, map the analog voltage value in the range of 0-1023 to the angle of the servo in the range of 0-180, and then drive the servo to rotate the corresponding angle through the P1 pin.
25.6 Python programming
25.6.1 Run the program
1.Connect micro:bit and PC with a Micro USB cable.
2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:
Find the code file for this lesson:
BBC_Microbit_Sensor\Code\Lesson_25\PythonCode
Select the file in ".py" format and click the Open:
3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:
4.After successfully downloading the program, you will see that the Servo rotates, as you are turning the Potentiometer, as shown below:
If you have problems, please send us an email: support@adeept.com
25.6.2 Learn the code program
Read the analog voltage value of the P0 pin,and then drive the servo to rotate the corresponding angle through the P1 pin.
11 12 13 14 | while True: Pot = pin0.read_analog() pin1.write_analog(Pot) sleep(20) |