You are viewing an older revision! See the latest version
Controlling DC motor
In this exercise you will control the speed and direction of rotation of the DC motor using the L298N. The DC motor model is RP 280CN-2485-45, with a supply voltage ranging from 3 to 12 V. Since there is no datasheet for the motor, the table below is shown on the ebay, showing the speed of rotation depending on the amount of the voltage with which the motor is driven and the amount of current the motor pulls:
Voltage (V) | Speed (rpm) - without load | Current (mA) |
---|---|---|
3.7 | 7300 | 145 |
6 | 12000 | 170 |
7.2 | 14000 | 180 |
12 | 23000 | 260 |
The board on which the driver L298N is located is shown in the following figure and is relatively inexpensive and it can be purchased on eBay or some other online store for $ 2-3.
The following figure shows the scheme of the board and it should be noted that there are three jumpers ( CON5
, ENA
and ENB
) on it.
For dc motors whose voltages are of lower power, remove jumper CON5
and connect the battery power (5-6 V) to the + 5V and + 12V connectors. Power supply greater than 7 V must be connected to the + 12V connector only, so that jumper CON5
must be mounted on the board. You can find more detailed instructions in the L298N datasheet.
Motor speed control¶
Create a program that controls motor speed using PWM. Adjust the speed using potentiometer Pot
or keystroke through the terminal. Since we are controlling the driver with two pins, consider the need to configure the second pin. Also, try replacing the pins and periodic frequencies in the program and notice the change.
Control of speed and direction of rotation of the motor¶
Modify the program so that with potentiometer Pot
you now control speed and direction of rotation of the motor using PWM. It is necessary to scale the value that mbed reads from the analog input so that when the potentiometer inputs the value 0.5, the motor must stop. If the potentiometer is turned to the left, i.e. if the value is less than 0.5, the motor must start to rotate to one side and if the value is greater than 0.5 the motor must start to rotate to the other side.
Control of speed and direction of rotation of the motor using class HBridgeDCMotor¶
Below is a program that controls the speed and direction of rotation of the motor using the HBridgeDCMotor class. Study the class and library RateLimiter inside it.
Create a new program, import the class HBridgeDCMotor to your compiler and test the code below:
#include "mbed.h" #include "HBridgeDCMotor.h" HBridgeDCMotor motor(p23, p24); int main() { float sampleTime = 10e-3, switchingFrequency = 25e3, rampTime = 5; motor.configure(sampleTime, switchingFrequency, rampTime, rampTime); while(true) { motor.setDutyCycle(1); wait(15); motor.setDutyCycle(-1); wait(15); } }
Your motor should behave as the motor displayed in the following video:
Modify the program so that your motor first rotates for 10 seconds at 25% PWM and then increases to 50% and rotates at that percentage for ten seconds and continue th pattern up to 100%. Next, the motor should rotate the opposite side either in a stepped way as it is made for the first side or a sudden jump at 100% on the opposite side. Try both cases by placing a sudden jump from one direction to the other, so that the rotation time when switching from one direction to another lasts fifteen seconds.
Congratulations!
You have completed all the exercises in the Controlling DC motor topic.
Return to TVZ Mechatronics Team Homepage.