PWM motor control
There is often the situation where the power of the motors must be controlled. One convenient way to do this is that we don’t power the motor full time, but we can turn off the motor for short period of time. For an example we can turn the motor on for 1 ms and turn it off for 1 ms. In this case the motor will not get 100% of power, but the motor’s average power will be 50%.
Since we are changing the pulse width of logical 1 with the respect to width of logical 0, this technique is called pulse width modulation
or shorter PWM
.
This modulated output is controlled by the analogWrite(pin, pwm)
function. Modulatio can be performed on pins: 3, 5 and 6 of the RobDuino modul. The value of pwm
parameter can be on a scale of 0 - 255., where 0 is 0% and 255 is 100% of electrical power served.
Tasks:
- Write new functions for driving the robot left and right with reduced power of the motors:
moveLeftPWM();
moveRightPWM();
In one case you will might find yourself in trouble of controlling the power of the motor since both pins are not able to perform
PWM
output. In this case you can remember that the motor’s power is 0 W also if both pins are in state of logical1
.An example of reducing power of both motors in function
moveForwardPWM()
is here:
void robotForwardPWM()
{
digitalWrite( LEFT_MOTOR_PIN_1, LOW);
analogWrite( LEFT_MOTOR_PIN_2, 150);
digitalWrite( RIGHT_MOTOR_PIN_1, LOW);
analogWrite( RIGHT_MOTOR_PIN_2, 150);
}
Similar to this function you can write other functions to.
- Change the functions
moveLeft()
andmoveRight()
in S-R-A loop with new ones with less power on motors.
#include <RobotMovingFunctions.h>
const int LIGHT_SENSOR_PIN = A0;
const int SURFACE_BRIGHTNESS_REFERENCE = 400;
void setup()
{
setIOpins();
pinMode(LIGHT_SENSOR_PIN , INPUT);
}
void loop()
{
int light_sensor_value = analogRead(LIGHT_SENSOR_PIN );
if ( light_sensor_value < SURFACE_BRIGHTNESS_REFERENCE ){
moveLeft();
} else {
moveRight();
}
delay(10);
}
: PWM motor control. {#lst:350_PWM_motor_control}
-
Also add
analogWrite(LEFT_MOTOR_PIN_A, 0);
to functionstopTheRobot()
to stop thePWM
control of the motor. And do similar code for theright motor
. - Add a parameter
PWM_value
to each function to set theduty cicle
of the controlled output.moveLeftPWM(int PWM_value)
moveRightPWM(int PWM_value)
- Save
moveRightPWM(int PWM_value)
andmoveLeftPWM(int PWM_value)
functions into header fileRobotMovingFunctions.h
Questions:
- How can we control the average power of the motor?
- How can we control the average power of the motor in both directions if we are not able to control
PWM
both output pins of the motor? - Explain the purpose of programming function
analogWrite(pin, pwm)
. - Explain the meaning of the
pin
andpwm
parameters in functionanalogWrite
.
Summary: Using PWM to Control Motor Power
Pulse Width Modulation (PWM) is a technique used to control the power delivered to electrical devices, such as motors, by modulating the width of the pulses in a pulse train. This is particularly useful for controlling the speed of DC motors, enabling smooth acceleration and deceleration.
PWM and Motor Control
By adjusting the duty cycle of the PWM signal—the proportion of the on-time to the total cycle time—you can effectively control the average voltage and current reaching the motor, thus controlling its speed. A higher duty cycle means more power is delivered to the motor, resulting in higher speed, while a lower duty cycle reduces the power and slows the motor.
Function analogWrite()
In Arduino, the analogWrite()
function is used to output a PWM signal to a pin. It simulates an analog output by producing a rectangular wave (a series of HIGH and LOW signals) of varying duty cycle.
Syntax:
analogWrite(pin, value);
Parameters:
-
pin
: The pin on which to generate the PWM signal. It must be capable of PWM output (usually marked with a~
on the Arduino board). -
value
: An integer between 0 and 255 that represents the duty cycle of the PWM signal.0
means a 0% duty cycle (the signal is always LOW), effectively turning the motor off.255
means a 100% duty cycle (the signal is always HIGH), delivering full power to the motor.
Example Usage:
int motorPin = 6; // Pin connected to the motor's PWM input
void setup() {
pinMode(motorPin, OUTPUT); // Set the motor pin as an output
}
void loop() {
analogWrite(motorPin, 128); // Set motor speed to 50% of the maximum power
delay(1000); // Run at this speed for 1 second
}
In this example, connecting motorPin
to a PWM-capable pin allows it to output a PWM signal that represents 50% power, causing the motor to run at half speed. This approach allows precise and efficient control of motor speed without the need for complex circuitry, making PWM a versatile tool for engineers and hobbyists alike.
Issues: with PWM for Motor Control
-
Motor Noise
- Description: PWM can introduce electrical noise, which might cause interference with other electronic components or degrade motor performance.
- Solutions: Use capacitors across the motor terminals to filter out high-frequency noise. Additionally, ensure proper grounding and shielding of cables to minimize interference.
-
Inadequate Resolution
- Description: The PWM resolution may not be sufficient for precise control, especially at low speeds.
- Solutions: Consider using a higher-resolution PWM output if available, or a motor driver board that supports higher resolution. Fine-tune the PWM values in the software to achieve the desired speed range.
-
Heat Generation
- Description: Motors can overheat if driven at high power for extended periods, particularly at 100% duty cycle.
- Solutions: Implement a feedback system to monitor motor temperature and reduce power when overheating is detected. Use heat sinks or cooling fans to dissipate excess heat.
-
Mechanical Wear
- Description: Frequent starting and stopping due to PWM can lead to mechanical wear and tear on the motor components.
- Solutions: Gradually ramp the PWM duty cycle up or down to minimize abrupt changes in speed. Use software to implement smoothing functions to reduce mechanical stress on the motor.
-
Reduced Torque at Low Speeds
- Description: Motors often exhibit reduced torque at low PWM values, which may not provide enough power to start the motor.
- Solutions: Use PWM in combination with gear reduction to maintain performance at lower speeds, or apply a higher initial pulse to get the motor moving before reducing the PWM duty cycle.
-
Voltage Drops
- Description: The rapid switching of PWM can cause voltage drops across the power supply, affecting other components.
- Solutions: Use a dedicated power supply for the motor and ensure power lines are appropriately rated to handle the current. Additionally, add decoupling capacitors to the power lines to stabilize voltage.
Addressing these issues involves combining hardware solutions with software optimizations to ensure reliable and efficient motor control using PWM.