TVZ Mechatronics Team


Zagreb University of Applied Sciences, Professional Study in Mechatronics

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 loadCurrent (mA)
3.77300145
612000170
7.214000180
1223000260

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.

/media/uploads/tbjazic/l298n_breakout_board.jpg

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.

/media/uploads/tbjazic/l298n_breakout_schematics.png

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. Also, you can add the LED1 whose brightness you will control as in the 6.2, and here it serves as a speed indicator for the motor. Since we are controlling the driver with two pins, think how you need to configure the second pin, if first one serves for controlling motor speed. 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 still control speed of the motor but add functionality that when you press the button, motor changes its direction of rotation. To detect rising edge of the signal, when button is pressed you will need to use class InterruptIn. Since during the execution of such program, when button is pressed, the engine would suddenly change its direction of rotation. To prevent sudden change of direction, we need to import class RateLimiter which slows down the aforementioned sudden change of direction by introducing the ramp principle, which gradually accelerates or slows down speed rotation of the motor determined by potentiometer.

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. This class also includes before mentioned class RateLimiter from the previous task.

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.

Control of speed and direction of rotation of the motor - additional task

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

Modify the program by importing class RateLimiter and implement its functions.

Congratulations!

You have completed all the exercises in the Controlling DC motor topic.

Return to TVZ Mechatronics Team Homepage.


All wikipages