Index>BBC Micro:bit>BBC Micro:bit Starter Kit>Lesson 33 Control a Servo with a Potentiometer
No related product information!

Lesson 33 Control a Servo with a Potentiometer

2797

    In this lesson, we will carry out an interesting experiment to use a potentiometer to control a servo with a Micro:bit.

33.1 Components to be prepared

image.png


33.2 Potentiometer

33.2.1 Potentiometer

The potentiometer is a resistance element with three terminals and the resistance value can be adjusted according to a certain change law, which is equivalent to a variable resistor. Because its role in the circuit is to obtain a certain relationship with the input voltage (external voltage) to output Voltage, so called potentiometer. Potentiometers can be divided into rotary potentiometers, push-pull potentiometers, straight slide potentiometers, etc. according to the adjustment method. Our course experiment uses a rotary potentiometer.Its three pins are showed as below:

image.png 

33.2.2 Working principle of potentiometer

The rotary potentiometer is an adjustable resistance element. It is composed of a resistor and a rotating system. When a voltage is applied between the two fixed contacts of the resistive body, the position of the contact on the resistive body is changed by the rotating system, and a voltage that has a certain relationship with the position of the moving contact can be achieved between the moving contact and the fixed contact. Potentiometer can be used to adjust the voltage and current.

Our course uses a rotary potentiometer. Its structure is as shown in the figure below. By rotating the knob, the position of pin 2 is changed, thereby changing the resistance value from pin 2 to both ends. In the experiment. Connect pin 1 and pin 3 to the GND and 5V of the development board respectively. And then read the voltage divided by the pin 2 of the potentiometer through the analog input pin A0. The range is between 0V and 5V. The analog input function of Arduino has 10-bit precision, that is, it can convert the voltage signal of 0 to 5V into an integer form of 0 to 1024.

image.png


33.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.

image.png


33.4 Circuit

    You need to connect the components according to the following circuit diagram. You can use the “enlarge” function to view the picture.


image.png


33.5 MakeCode programming

Next, we will use the online MakeCode Editor to complete the experiment in this lesson.


33.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:

image.png



(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png



Click the "Choose File"

image.png


Find the code file for this lesson:

BBC_Microbit_kit\Code\Lesson_33\BlockCode

Select the file in ".hex" format and click the Open:

image.png


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:

image.png


3.You can see the following interface when successfully opening the file:

image.png


33.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 image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3. Start to download the program to Micro:bit, and click the image.png 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. Rotate the potentiometer, and the servo will follow the rotation, as shown in the following figure:

image.png 


[Note]:

  

    1.If no experimental phenomenon has been detected after clicking the button image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the LED again, as shown in the following figure:

image.png

2.If there is still no experimental phenomenon, you need to unplug and then plug in the USB cable connected to the Micro:bit, and then download the program again.


  If you have problems, please send us an email: support@adeept.com


33.5.3 Learn the code program

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.

image.png


33.6 Python programming

33.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:

image.png 


Find the code file for this lesson:

BBC_Microbit_Kit\Code\Lesson_33\PythonCode

Select the file in ".py" format and click the Open:

image.png 


3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:

image.png


4.After downloading the program, rotate the potentiometer, and the servo will follow the rotation, as shown in the following figure.

image.png

【Note】:

   If the servo does not rotate when you rotate the potentiometer, please check whether the wiring of the potentiometer and servo is wrong or loose.


    If you have problems, please send us an email: support@adeept.com


33.5.2 Learn the code program


The codes for this lesson are explained as follows. 

(1)Define map functions to convert values in one range to values in another range.


2

3

def map(value, fromLow, fromHigh, toLow, toHigh):

  return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow


(2)Set the period of PWM signal to 20 ms. Read the analog voltage value of P0 foot, convert the analog voltage value in the range of 0-1023 to the analog voltage value in the range of 25.6-128, and then output the corresponding PWM signal to rotate the servo at the corresponding angle.


5

6

7

value = pin0.read_analog()

pin1.set_analog_period(20)

pin1.write_analog(map(value, 0, 1023, 25.6, 128))