### Project 16:Motor ![](./media/Python_32655f47.png) 1\. **Description** The Keyestudio 4WD Mecanum Robot Car is equipped with 4 DC reduction motors, also called gear reduction motor, which is developed on the ordinary DC motor. It has a matching gear reduction box which provides a lower speed but a larger torque. Furthermore, different reduction ratios of the box can provide different speeds and torques. Gear motor is the integration of gearmotor and motor, which is applied widely in steel and machine industry Micro:bit motor driver shield comes with a STC8G and HR8833 chip. In order to save the IO port resource, we control the rotation direction and speed of 4 DC gear motors with the HR8833 chip. **Details about chips:** ![](./media/Python_d7132b53.jpg) Front ![](./media/Python_4919ce3b.png) Back ![](./media/Python_fbfa17f7.png) STC8G1K08 Chip circuit ![](./media/Python_47cdde6b.png) HR8833 Motor driver circuit 2\. **Preparation** - Insert micro:bit board into the slot of keyestudio 4WD Mecanum Robot CarV2.0 - Place batteries into battery holder - Dial power switch to ON end - Connect micro:bit to the computer via an USB cable - Open the offline version of Mu. 3\. **Test Code1** Enter Mu software and open the file“microbit-Motor Driving-1\.py”to import code. You can also input code in the edit window yourself. (**Note: All English words and symbols must be written in English**.) Click“Files”to import“keyes_mecanum_Car.py”library file to micro:bit . Click“Check”to examine errors in the code. The program proves wrong if underlines and cursors are shown. If the code is correct, connect the micro:bit to your computer and click“Flash”to download the code to the micro:bit board. ![](./media/Python_71476377.png) ```python from microbit import * from keyes_mecanum_Car_V2 import * mecanumCar = Mecanum_Car_Driver_V2() while True: display.show(Image.ARROW_S) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) display.show(Image.ARROW_N) mecanumCar.Motor_Upper_L(0, 100) mecanumCar.Motor_Lower_L(0, 100) mecanumCar.Motor_Upper_R(0, 100) mecanumCar.Motor_Lower_R(0, 100) sleep(1000) display.show(Image.ARROW_E) mecanumCar.Motor_Upper_L(0, 100) mecanumCar.Motor_Lower_L(0, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) display.show(Image.ARROW_W) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(0, 100) mecanumCar.Motor_Lower_R(0, 100) sleep(1000) display.show(Image("00900:""09990:""99999:""99999:""09090")) mecanumCar.Motor_Upper_L(0, 0) mecanumCar.Motor_Lower_L(0, 0) mecanumCar.Motor_Upper_R(0, 0) mecanumCar.Motor_Lower_R(0, 0) sleep(1000) ``` 4\. **Test Result1** After downloading the code to the board successfully, **external power supply(turn the DIP switch to ON)**,and press the reset button on micro:bit. ![Img](./media/Python_bb3e1312.png) Then the car will go forward for 1s, back for 1s, turn left for 1s, right for 1s, turn anticlockwise for 1s, clockwise for 1 and stop 1s. Matrix also displays the patterns. 5\. **Test Code2** Enter Mu software and open the file“microbit-Motor Driving-2\.py”to import code. You can also input code in the edit window yourself. (**Note: All English words and symbols must be written in English**.) Click“Files”to import“keyes_mecanum_Car.py“library file to micro:bit. Click“Check”to examine errors in the code. The program proves wrong if underlines and cursors are shown. If the code is correct, connect the micro:bit to your computer and click“Flash”to download the code to the micro:bit board. ![](./media/Python_96230faf.png) ```python from microbit import button_a, button_b, display, Image, sleep from keyes_mecanum_Car_V2 import * mecanumCar = Mecanum_Car_Driver_V2() show_L = Image("90000:""90000:""90000:""90000:""99999") show_O = Image("09990:""90009:""90009:""90009:""09990") a = 0 b = 0 def run_L(): global b sleep(1000) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) mecanumCar.Motor_Upper_L(0, 100) mecanumCar.Motor_Lower_L(0, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(650) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) mecanumCar.Motor_Upper_L(0, 0) mecanumCar.Motor_Lower_L(0, 0) mecanumCar.Motor_Upper_R(0, 0) mecanumCar.Motor_Lower_R(0, 0) b = 0 def run_O(): global b sleep(1000) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) mecanumCar.Motor_Upper_L(0, 100) mecanumCar.Motor_Lower_L(0, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(620) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) mecanumCar.Motor_Upper_L(0, 100) mecanumCar.Motor_Lower_L(0, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(620) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) mecanumCar.Motor_Upper_L(0, 100) mecanumCar.Motor_Lower_L(0, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(620) mecanumCar.Motor_Upper_L(1, 100) mecanumCar.Motor_Lower_L(1, 100) mecanumCar.Motor_Upper_R(1, 100) mecanumCar.Motor_Lower_R(1, 100) sleep(1000) mecanumCar.Motor_Upper_L(0, 0) mecanumCar.Motor_Lower_L(0, 0) mecanumCar.Motor_Upper_R(0, 0) mecanumCar.Motor_Lower_R(0, 0) b = 0 while True: if button_a.was_pressed(): a = a + 1 if a >= 3: a = 0 if button_b.was_pressed(): b = 1 if (a == 1): display.show(show_L) if b == 1: run_L() elif a == 2: display.show(show_O) if b == 1: run_O() ``` 6\. **Test Result2** After downloading the code to the board successfully, **external power supply(turn the DIP switch to ON)**,and press the reset button on micro:bit. ![Img](./media/Python_bb3e1312.png) When the button A and B are firstly pressed, micro:bit will show “L”, the route of the car is“L”. When they are pressed again,“口”is shown on micro:bit, and route of the car is“口”. The car will repeat this pattern. 7\. **Code Explanation** ![Img](./media/Python_70b4e70f.png) ![Img](./media/Python_e3250a8a.png)