Index>BBC Micro:bit>BBC Micro:bit Starter Kit>Lesson 35 The Way to Use a Rotary Encoder
No related product information!

Lesson 35 The Way to Use a Rotary Encoder

3126

   In this lesson, we will carry out an interesting experiment to read the value of a rotary encoder with a Micro:bit.

35.1 Components to be prepared

image.png


35.2 Rotary Encoder

35.2.1 Rotary Encoder

The rotary encoder is used to measure the speed and cooperate with the PWM technology to achieve fast speed regulation. The photoelectric rotary encoder can convert the mechanical displacements such as the angular displacement and angular velocity of the output shaft into corresponding electrical pulses through digital conversion (REP).In the course,we use incremental rotary encoders,which are rotary sensors that convert rotational displacement into a series of digital pulses. The rotation allows you to count the number of output pulses during forward and reverse rotation, and unlike potentiometers, there is no limit to this rotation counting. With the keys on the rotary encoder, it can be reset to the initial state, i.e. counting from 0.

image.png

image.png


35.2.2 Working principle of potentiometer

In the process of actual use, this encoder adopts the coaxial operation mode during operation, which means that the equipment can help the equipment to make better measurements through the shaft installation during operation. The purpose is to be able to achieve the same speed operation, so as to ensure more accurate data measurement. According to the different working principles of the rotary encoder, the output mode of the device is generally divided into two ways of single output and double output, each of which can be well adapted to different use characteristics. During the operation of the equipment, it is mainly measured by the corresponding pulse number, and the encoder is provided with the corresponding data by the phase difference between the angles.

image.png 


    By a photoelectric code wheel with a shaft in the center, it has a ring-shaped, dark reticle, and is read by photoelectric transmitting and receiving devices to obtain four sets of sine wave signals combined into A, B, C, D, each sine wave The phase difference is 90 degrees (360 degrees relative to a cycle), the C and D signals are reversed and superimposed on the A and B phases to enhance the stable signal; another Z phase pulse is output per revolution to represent the zero reference Bit. Since the two phases of A and B differ by 90 degrees, the forward and reverse rotation of the encoder can be judged by comparing phase A or phase B. The zero reference position of the encoder can be obtained through the zero pulse.

The material of the encoder code disc is glass, metal and plastic. The glass code disc is a thin line deposited on the glass. It has good thermal stability and high precision. The metal code disc is directly engraved with or without engraved lines, which is not fragile. However, because the metal has a certain thickness, the accuracy is limited, and its thermal stability is an order of magnitude worse than that of glass. The plastic code wheel is economical and its cost is low, but the accuracy, thermal stability, and life span are all worse.

There are many optical channel engravings on the optical code disk of the absolute value rotary encoder, and each engraving in turn is arranged by 2 lines, 4 lines, 8 lines, 16 lines ... Each line is dark and dark, and a set of unique binary codes (Gray codes) from the 0th power of 2 to the n-1th power of 2 is obtained. This is called an n-bit absolute encoder. Such an encoder is determined by the mechanical position of the photoelectric code disc, and it is not affected by power outages and interference.

Each position determined by the mechanical position of the absolute rotary encoder is unique. It does not need to be memorized, it does not need to find a reference point, and it does not need to keep counting. When it is necessary to know the position and read its position whenever. In this way, the anti-interference characteristics of the encoder and the reliability of the data are greatly improved.


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

35.4 Circuit

    You should connect the components according to the following circuit diagram, and view the pictures with the function “Zoom in”: 

image.png



35.5 MakeCode programming

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


35.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_Sensor\Code\Lesson_35\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


35.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. as shown below:

image.png


4.Rotate the encoder clockwise or counterclockwise and click the button Show console Deviceon the left, as shown below

image.png 


    5.The following waveform diagram can be seen. image.pngrepresents the analog value of the rotary encoder when rotating clockwise or counterclockwise. Rotate clockwise, and the analog value of image.png will become larger.Rotate counterclockwise, and the analog value of image.png will become smaller. image.png represents the button state of the rotary encoder.Press the rotary encoder,and image.png will change into image.png.Rotate the encoder, and image.png will change into image.png

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 theShow console Deviceagain, as shown in the following figure:

image.png

2. If no experimental phenomenon is observed, you should check whether the USB cable of Micro:bit is successfully connected. If not, please download the program again.

3.After clicking the button Show console Device】 on the left, if no waveform is shown on the right, please rotate the encoder again, and you will see the waveform. If no waveform has been shown yet, please repeat the above steps.

image.png

4.After clicking the button image.png, if the button image.png isn’t shown on the left, please rotate the encoder, and the button image.png will show on the left.

image.png

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


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

image.png 



(1)When rotating the encoder counterclockwise, reduce 1 from the analog count, and change the【press】of recording button state to 0 simultaneously.

image.png 


(2)When rotating the encoder clockwise, please add 1 to the analog count and change the【press】of recording button state to 0 simultaneously.

image.png


(3) When pressing the rotary encoder, please set the count to 0 and change the【press】of recording button state to 1.

image.png



35.6 Python programming

35.6.1 Circuit

You should connect the components according to the following circuit diagram, and view the pictures with the function “Zoom in”:

 

image.png


35.6.2 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_Sensor\Code\Lesson_35\PythonCode

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


image.png 


3.Click buttonFlash】 to download the program to the Micro:bit. Click the button image.png immediately (click the button image.png when the Micro:bit indicator is flashing), and the output data will be read on the console, as shown below:


image.png


After downloading the program to Micro:bit successfully, click the button image.png, and it will be blank on the console:

image.png



4.Rotate the encoder clockwise or counterclockwise, and the encoder value will be output on the console, as shown below:

image.png


Note

1. If no value can be read on the console when rotating the encoder, please check whether the wiring of the rotary encoder is connected or not.

2. If no value can be read on the console, please click the button Flash】 again, click the button image.png immediately, and rotate the encoder. If no data are output, please repeat the above steps or check whether the wiring is connected correctly.

3. If the following prompt shows, please connect the Micro:bit USB cable and download the program again.

image.png 

4. After clicking the button image.png, the information will be shown on the console as follows. If no data is output when rotating the encoder,please click image.pngagain. When the indicator light of Micro:bit stops flashing, click the button image.png again.

image.png

 

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


35.6.2 Learn the code program


The description of the course code in this section is as follows.

   

(1) Determine whether the rotating shaft of the rotary encoder is rotating. If it rotates, mark this state as 0 with flag.


15

16

17

18

last_B_status = pin11.read_digital()

while (not pin5.read_digital()):

   current_B_status = pin11.read_digital()

   flag = 0


    (2) If the rotary encoder rotates, determine the rotation direction from state 0 to state 1 with if (last_B_status == 0) and (current_B_status == 1), and it rotates counterclockwise, counter-1.Determine the rotation direction from state 1 to state 0 with if (last_B_status == 0) and (current_B_status == 1), and it rotates counterclockwise, counter+1.


1

2

3

if flag == 0:

  flag = 1

   if (last_B_status == 0) and (current_B_status == 1):

        counter -= 1

if (last_B_status == 1) and (current_B_status == 0):

        counter += 1