Index>Robot Kit>Alter All in One Kit for RPi>Lesson 7 How to Control DC Motor
No related product information!

Lesson 7 How to Control DC Motor

1236

    In this lesson, we will learn how to control DC Motor.

7.1 Components used in this course


image.png


7.2 The introduction of DC Motor

Alter robot products use DC motor as a power device. DC motor is a device that converts DC electrical energy into mechanical energy. It is widely used to drive various equipment, such as electric fans, remote control cars, electric windows, etc.The DC motor is very suitable as the walking mechanism of the robot.


7.3 Wiring diagram (Circuit diagram)

When the DC Motor module is in use, it needs to be connected to the motorA or motorB interface on the Robot HAT drive board. The yellow wire is connected to the yellow pin, the red wire is connected to the red pin, and the brown wire is connected to the black pin, as shown below:

image.png


7.4 How to control Motor 

7.4.1 Run the program of this course

1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. Enter the Course Code folder, this folder stores the sample code program to control the robot, enter the following command:

cd adeept_alter/02CourseCode/01ComponentCode

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5. The 06Motor folder stores the sample code of this course. Enter the command to enter this folder:

cd  06Motor

image.png


6. Enter the command to display the contents of the current directory:

ls

image.png


7.Motor.py is a python program.You can run this program on the Raspberry Pi by directly by typing the following commands:

sudo  python3  Motor.py

image.png


8.ctrlServo.py can control the rotation of the servo, directly enter the following commands to run this program on the Raspberry Pi:

sudo  python3  ctrlServo.py

image.png


9.After successfully running the program, you will observe that the Motor will rotate regularly.


10.If you want to terminate the running program, you can press the shortcut key Ctrl+C on the keyboard.



7.4.2 The main code program of this lesson

After the above hands-on practice, you already know how to use and run our course sample code program. You must be curious about how our code program is programmed to control the DC Motor on the Raspberry Pi. Let’s get to know the main code program.Here we use Sublime IDE to view and edit the code program of this lesson, please refer to the content of lesson 2 for specific methods.

1. Import related dependencies and initialize.

image.png


Motor_EN = 4, Motor_Pin1 = 26, Motor_Pin2 = 21 are the parameters of the corresponding interface motorA.

image.png

If you need to change to MotorB, then you can modify it as shown below:

image.png

2. Start the motor. The pwm_A.start(100) is used to start the PWM; the pwm_A.ChangeDutyCycle(100) method is used to adjust the duty cycle and speed.

image.png


3. Control the motor to rotate in the opposite direction. By switching high and low levels (HIGH and LOW), the motor rotates in the opposite direction. The GPIO.cleanup() method clears the occupied GPIO port and turns off the motor.

image.png