Arduino Self-Driving Car Lesson 3: Controlling Motor Speed
Summary
TLDRThis video teaches how to control the speed of a motor using an Arduino and pulse width modulation (PWM). Instead of running a motor at full speed or stopping, the video explains how to gradually ramp up and down the motor's speed. By manipulating the enable pin and using PWM pins, the speed can be adjusted to fine-tune the motor's performance, which is useful in robotic vehicles. The tutorial walks through wiring the circuit in Tinkercad, writing the code, and implementing the analog write command to control motor speed effectively.
Takeaways
- 🔧 Learn how to control the speed of a motor, allowing for variable speeds instead of just stop-and-go.
- 🔋 The motor's speed can ramp up and down gradually, not just switching between full speed and stop.
- 🎮 Using PWM (pulse width modulation) through specific Arduino pins, you can control motor speed more precisely.
- 🔀 Two Arduino pins control the motor’s direction, and now a third pin can manage speed by connecting to the enable pin.
- ⚙️ PWM rapidly turns pins on and off to simulate different average voltages, creating the effect of variable speed.
- 🖥️ The `analogWrite` command controls motor speed, taking values between 0 and 255 to represent different levels of speed.
- 🔄 The script shows how to adjust motor speed independently of direction using the `analogWrite` command.
- 🏎️ This method allows for fine-tuning in cases where the vehicle drifts off course, compensating for motor differences.
- 🛠️ Modify your code to control both motors independently and experiment with different speed values to adjust performance.
- 📐 By adding inputs to your functions, you can make the robot drive forward at varying speeds without rewriting code for each speed.
Q & A
What is the purpose of controlling the motor's speed in a robotic vehicle?
-Controlling the motor's speed allows for smoother movement, helps the vehicle drive and turn at different speeds, and compensates for any drift if the vehicle tends to veer off course when moving straight.
What is pulse width modulation (PWM) and how does it help control motor speed?
-PWM is a technique where the digital pins on the Arduino rapidly switch between high (5V) and low (0V). By adjusting how long the signal stays high or low, PWM can mimic different voltage levels, effectively controlling the motor's speed.
Why can't the Arduino provide true analog output for motor speed control?
-The Arduino lacks a true analog output capability, so it uses PWM to simulate analog signals by rapidly turning the digital pins on and off, creating an average voltage that controls motor speed.
How can you use the analogWrite command to control motor speed?
-The analogWrite command accepts a value between 0 and 255, where 0 is no speed (motor off) and 255 is full speed. By specifying different values, you can control how fast the motor spins.
Why is it important to use PWM pins for motor speed control?
-PWM pins are essential because they are the only pins capable of generating the PWM signal required to simulate analog output, which is necessary for controlling the motor’s speed.
What happens if the PWM value is set too low, for example, 10?
-At very low PWM values like 10, the motor might not spin because the voltage is too low to overcome internal friction. The motor may make a whining noise but will not move.
How can you fix a robotic vehicle that drifts to one side while driving straight?
-You can use the analogWrite command to adjust the speed of the motors independently. For example, if the vehicle drifts to the right, you can slow down the left motor until the vehicle drives straight.
How does adding speed control make the drive forward function more flexible?
-By adding speed inputs to the drive forward function, you can control how fast the vehicle moves without having to write separate functions for different speeds. This makes the function more versatile for different scenarios.
What additional components are needed to make the vehicle react to its environment?
-To make the vehicle react to its environment like a real autonomous car, you need to add sensors that can detect obstacles and other elements in the surroundings. These will be covered in the next steps of the project.
Why is it beneficial to use different speeds for the motors when turning the vehicle?
-Using different speeds for the motors allows for smoother, gradual turns, as opposed to abrupt changes in direction. This gives more precise control over the vehicle's movements.
Outlines
🔧 Introduction to Motor Speed Control
This paragraph introduces the concept of controlling motor speed using an Arduino. It highlights how, instead of just alternating between stopping and full speed, one can gradually ramp up and down motor speed. The focus is on practical applications such as making robotic vehicles drive and turn at different speeds to compensate for steering drift.
💡 Recap and PWM Introduction
Here, the video transitions into a recap of how motors were previously controlled via two Arduino pins, which managed the motor's direction. The concept of pulse width modulation (PWM) is introduced, explaining how this technique allows for speed control by adjusting the duty cycle of the signal sent to the motor's enable pin. This method fakes an analog signal and can vary motor speed from 0 to full speed.
📐 Adjusting Motor Speed with Code
The focus of this paragraph is the implementation of PWM in code. The 'analogWrite' function is discussed in detail, allowing the motor to run at different speeds by specifying a value between 0 and 255. The use of pin 6 for speed control and how changing the analogWrite value affects motor speed in simulations is demonstrated.
🚗 Testing Speed Control and Debugging
The paragraph discusses the results of setting different PWM values (255, 128, and 10) for motor speed. The video shows that very low values may not overcome motor friction, preventing it from spinning at low speeds. It explains that while high-speed control is effective, very low-speed control might not be reliable due to friction. The example also demonstrates how these PWM adjustments affect the vehicle's movement, particularly when trying to drive straight.
🔄 Independent Control of Both Motors
The video now explains how to control the speed of both motors independently, allowing for more precise control of the vehicle's movement. It introduces the concept of tweaking the motor speeds to correct drifting during forward movement. The author provides step-by-step guidance on how to modify the code to handle two motors using PWM pins.
🛠️ Enhancing Flexibility with Speed Inputs
The final paragraph introduces more advanced programming techniques by adding flexibility to the 'drive forward' function. By adding speed input arguments for each motor, the function can now control forward movement at different speeds without needing separate functions. This opens up more possibilities for creating dynamic and adaptable movement in the robot. It sets the stage for adding sensors for real-time environmental interaction.
Mindmap
Keywords
💡Motor Speed Control
💡Pulse Width Modulation (PWM)
💡Arduino
💡H-Bridge
💡Enable Pin
💡Digital Write
💡Analog Write
💡Robot Steering
💡Rotations Per Minute (RPM)
💡Drive Forward Function
Highlights
Learn how to control motor speed to make it rotate at different speeds rather than just alternating between stopping and full speed.
Explanation of using the enable pin on the H-bridge to control motor speed through the Arduino instead of connecting it directly to 5 volts.
Introduction to pulse width modulation (PWM) as a method for simulating analog output to control motor speed using digital pins.
Using the analogWrite command to control the motor's speed by adjusting the duration of high and low signals on the PWM pin.
Demonstration of changing motor speeds by adjusting PWM values: 0-255, with 255 being full speed and lower values reducing the speed proportionally.
Controlling speed and direction independently for each motor, helping to correct issues like a vehicle drifting to one side.
Exploring the limits of motor speed control: very low PWM values (e.g., 10) may not overcome motor friction, leading to no movement.
Adjusting motor speeds to drive in a straight line, compensating for natural drift due to friction or motor imbalance.
Practical example of adjusting speeds to fix drifting issues, demonstrated by slowing one motor to align the vehicle's path.
Using PWM to intentionally create gradual turns or controlled vehicle maneuvers by setting different speeds for the left and right motors.
Incorporating speed control into existing functions like 'drive forward' by adding variable inputs for left and right motor speeds.
Highlighting flexibility in vehicle control by modifying existing drive functions to accept different speed values instead of creating separate functions.
Explaining the analogWrite command’s range of 0-255, based on an 8-bit number system, and its impact on motor speed.
Adapting speed control functions to maintain consistent motor directions while adjusting speeds independently.
Introduction to using sensors for vehicle navigation, which will allow the robot to respond to its environment like a real autonomous car.
Transcripts
in this video you will learn how to
control the speed of a motor so you can
make it rotate at different speeds
instead of just alternating between
stopping and full speed you can see here
I'm running a program that makes the
motor speed ramp up and then ramp back
down gradually instead of just going
instantly to full speed I'm going to
unplug the motor now so you can hear me
a little better this will allow you to
make your robotic vehicle drive and turn
at different speeds and help you
compensate for the steering if it tends
to Drift Off to one side when you try to
make it drive straight let's switch over
to tinkercad and take a look at how we
are going to wire and program the
circuit to control the motor's speed now
don't be intimidated by this circuit you
should have it assembled from a previous
video and we are only going to worry
about one of the motors for now so you
can ignore most of the connections over
here on the left hand side of the H
Bridge we're just going to look at the
connections on this side for the most
part so as a recap each motor is
controlled by two pins on the Arduino
that control the motor's direction if we
set one of those pins High while the
other pin is low it'll make the motor
spin if we switch which pin is high and
low it'll make the motor spin in the
other direction and if we set both pins
low the motor will stop
what we did before though is there's
this extra pin called the enable pin
that we connected directly to 5 volts on
the Arduino and that is always setting
this pin high so the motor is always
going to go full speed what we can do
instead is remove this jumper wire and
connect this enable pin to one of the
arduino's pins that way we will have
three pins controlling each motor two
for the direction and one for the speed
and to do that we need to connect to one
of the Arduino pins with the little
squiggly symbol or tilde next to it and
those are the pins that can do pwm or
pulse width modulation so before we use
those pins let's talk a little bit about
what that means here's what you have
been doing so far with the arduino's
pins you have been using the digital
write command to set the digital pins
either high or low high has a voltage of
5 volts low has a voltage of zero volts
but you cannot choose a voltage in
between for example you can't set a PIN
to two and a half volts to do that you
need an analog signal or a signal that
is continuously variable unlike digital
signals which can only be high or low an
analog signal can take on any value in
between the minimum and the maximum so
for example you could set an analog
voltage to exactly two and a half volts
the problem is that the Arduino does not
have a true analog output however it can
fake an analog output using the digital
pins and something called pulse width
modulation or very rapidly turning the
digital pins on and off so for example
you can imagine that if I very rapidly
switch between 0 volts and 5 volts and I
am low 50 of the time and high fifty
percent of the time than my effective
average voltage would be two and a half
volts so you can do this using the
analog write command you just have to
remember that the analog right command
is not outputting a true analog signal
it is turning the pin on and off very
quickly to kind a fake and intermediate
analog voltage but this lets you do
something like control the speed of a
motor if you want the motor to spin
roughly half speed then you want the
signal to be high half of the time and
low half of the time if you want it to
spin faster then you want the signal to
be high for longer than its low because
this will give you a higher average
voltage
similarly if you want the motor to spin
slower you want the signal to be low for
longer than its high because this will
give you a lower average voltage now
this is a pretty Advanced concept so
don't worry if you don't perfectly
understand all of it next I'll show you
how to implement this concept using the
analog write command in code so going
back to the Arduino remember that I said
we need to use one of the pwm pins not
all of the pins have pwm capabilities so
we need to pick one with the squiggly
symbol or tilde next to it I am going to
choose pin 6 just because that's an open
pwm pin and I'm going to run a jumper
wire from there over to the enable pin
on my H bridge and say I'm going to
choose a different color I'm using blue
for my direction control wires maybe I'm
going to use green for my speed control
wires now let's modify the code to use
the analog right command to control the
speed of this motor so here I have the
program I wrote previously to steer an
autonomous vehicle with two Motors as a
quick recap we declare forward and
backward pins for each motor we set
those pins as outputs and then we wrote
a bunch of different functions like
drive forward or turn right that set
those pins high and low to spin the
motors in the correct directions for
that action
now I have added just a few lines of
code to this program that will let me
control the speed of the left motor
first I have declared a PIN number for
my left motor speed control again I'm
using pin 6 for that
then in my setup function technically I
don't need to but I am setting that pin
as an output now the analog write
command is a little weird it will
actually work even if you don't set the
pin as an output but it doesn't hurt to
do this if you want it as a reminder for
yourself that you're using that pin as
an output
next I am going to call my drive forward
function and you see I'm actually doing
that in setup instead of loop because if
I only do this once it's actually going
to just keep driving forward forever
even though this function has a delay
time in it I never call anything else
after that so the pin high and low
values never change so the the robot or
car will just drive forward forever if
you call this once I could also have
that inside the loop function and keep
calling it each time but that's kind of
redundant if all I want to do is get my
car to drive straight I'm just going to
call that once here then inside my drive
forward function I have added an analog
write command to write a value to my
speed control pin and the one thing we
haven't talked about yet the analog
write command actually accepts a value
between 0 and 255 so that might be a
little counter-intuitive if you're not
familiar with binary numbers you might
expect it something like zero between
zero and 100 for zero to 100 speed but
the range it accepts here is an 8-bit
number in other words 255 is the biggest
number you can store with 8 Bits or
eight ones and zeros if you start
counting at zero so it accepts a number
between 0 and 255 so for example if you
wanted to go about half speed you could
type in 127 or 128 here but 255 is going
to be full speed so I'm going to
demonstrate this by telling it 255 which
basically with the pwm signal just makes
that high all the time it never goes low
and if I hit start simulation here in
tinkercad I'm going to see both of my
motors are running at the same speed if
I look at my RPM or rotations per minute
value they're both running a little over
10 000 RPM however if I stop my
simulation
and again change this number to about
halfway say I'm going to change it to
128 and run my simulation again my left
motor is now spinning slower than the
right motor it's about half the speed at
a little over 5000 RPM so I can just by
changing this number change the speed of
this motor independently of changing its
direction I haven't changed my digital
write commands at all so I can still use
my different functions to change the
direction of the motor but I can also
make the motor spin at different speeds
by adding the analog right command so
here's what you can do at this point if
you haven't been following along get
ready to pause the video delete the
connection from the enable pin directly
to 5 volts replace it with a jumper wire
connection to one of the arduino's pwm
pins I'm using pin six then modify your
code to add these lines to clear a
variable for that speed pin
declared as an output if you want to
again strictly speaking you don't have
to do that delete calls to all of your
functions except for the drive forward
function and then add an analog right
command to the drive forward function
then when you upload the code to your
Arduino experiment with different
numbers here put in different numbers
between 0 and 255 and watch how fast
your motor actually spins
so what you'll see here is that I have
the motor programmed to Loop through
three different speeds I'm going to use
values of 255 128 and 10 with the analog
write command so I'm going to hit the
reset button on my Arduino and plug in
the motor power
so once it resets we should see full
speed there with the 255
and I'm going to drop to about half
speed with 128
and then I would expect it to go very
very slowly at 10 but you see that it
actually doesn't spin at all so I'm
going to loop back through those same
three speeds there and I'll be quiet and
we'll see if you can hear it when I have
it at the 10.
so I don't know if the camera's
microphone is going to pick it up but
there's a very tiny whirring or whining
noise when I have it set to 10 and the
problem is there when you're using a
very low value with the analog write
command it actually isn't enough to
overcome the internal friction of the
motor so the motor is kind of trying to
spin and you might hear sort of a high
pitched buzzing or whining noise but
it's actually not going to go anywhere
so for very very low speeds if you're
trying to make your vehicle sort of
crawl around it might not work that well
but for controlling those higher speeds
or maybe dropping to about half speed
you should be able to control the speed
of your wheels
so you may remember from a previous
video that when I turned this robot on
and put it down and called the drive
forward function until both Wheels to
spin full speed it doesn't go perfectly
straight it kind of drifted off to one
side so let's turn it on and take a look
at what happens here I'm going to turn
it on both my wheels are spinning full
speed
and you see it kind of drifts to the
right it doesn't go perfectly straight
and that means that maybe for whatever
reason there's a little more friction on
the right side or the right motor's
spinning a little slower so if the left
motor spins faster than the right motor
that is going to cause the robot to turn
to the right so if I want to get it to
drive perfectly straight I can use the
analog right command to slow my left
motor down so if you were having trouble
with your vehicle not driving straight
here is your chance to fix that pause
the video here edit your program so you
control the speed of both Motors
independently using the analog right
command then upload it and actually test
it out put your vehicle down let it
drive see if it's drifting to one side
and then adjust the speeds of the motors
to get it to drive straight
let's quickly switch back to tinkercad
and take a look at how we would do this
with the second motor so remember we're
going to need to choose one of the
arduino's pwm pins to control the motor
speed and we're going to disconnect the
jumper wire that is currently connecting
the enable pin directly to 5 volts so
I'm going to delete that wire and then
we're going to connect pin one on the H
bridge over to I'm going to choose pin 5
because that's also a pwm pin on the
Arduino then in my code I'm going to
declare a variable for that pin I'm
using pin 5. again technically I don't
need to use the pin mode command to
declare it as an output but I'm doing it
there and I have also added an analog
right for the right motor speed pin in
my drive forward function so now I can
independently control the speed of the
two Motors when my car is driving
forward
so what you'll see here is I have kept
my right motor at full speed I've kept
that at 255 but I have dropped my left
motor speed down to 200 so let's turn it
on and see how that goes
and that was pretty good maybe not
perfect I might need some fine tuning
where I adjust the speeds a little bit
in one way or the other to get it going
perfectly straight but I have corrected
that Drift Off to one side and for the
most part now it drives straight
now this is where controlling your
vehicle starts to become more open-ended
you have a lot of different options now
that you can independently control the
speed and direction of both wheels for
example you can try to make it drive
forward more slowly you can make it
intentionally do a gradual turn instead
of drifting off to one side when you're
trying to go straight you could
intentionally make it do that by making
the wheel spin at two different speeds
and as you've seen before you can make
it turn in place by making one of the
wheels spin backwards so you have a lot
of different options to maneuver and
control your vehicle as you're
programming it to move around an
environment
you can also change your functions to
make them a little more flexible so for
example say we want to be able to drive
forward at different speeds ideally we
would be able to do that using just one
drive forward function we don't want to
have to write separate functions for
drive fast and drive slow we just want
to be able to change the value of the
speed here in the analog right command
we can do that by adding more inputs or
arguments to the function for example
I'm going to add 2 and call them left
speed and write speed
and now I can replace these hard-coded
255 numbers with those variable names
left speed
and write speed
now when I call the drive forward
function I need to give it two more
inputs so I could write comma 255
255. now when I call drive forward here
that's going to spin both Motors full
speed forward for two seconds but then
say I want to drive forward more slowly
I can write a second call to the drive
forward function say I only want to go
for one second this time and I want to
drop both Motors to half speed so I'm
going to do 128 128 that's going to make
both Motors spin about half speed so
I'll still drive forward but I will
drive forward more slowly and I'm going
to move that down here to
my Loop function so I would just
alternate driving fast and driving slow
forever so
by adding those additional inputs to my
function this is now more flexible and I
can call it to make the robot drive
forward at different speeds I could do
the same thing for all of my other
functions so I have an added speed
control for driving backward or any of
the Turning but you can do the same
thing at Analog write commands and those
additional variables to control the
speed and then you can maneuver the
robot in different directions at
different speeds with the same functions
that you wrote last time once you have
all of these functions that can control
your vehicle's movement that allows you
to hard code a certain path for it to
follow but it still can't react to its
environment like a real autonomous car
would to do that we'll need to add
sensors that can detect obstacles and
other things in its environment we'll do
that next
Ver Más Videos Relacionados
شرح طريقة تحكم step motor من خلال الاردوينو Arduino
ESP32 Servo Motor Web Server with Arduino IDE
MATLAB simulation on speed control of induction motor | Scalar Control of induction motor
How does an Induction Motor work?
Altivar 320 Drive Configuration: HMI Dial Speed Control Tutorial | Schneider Electric Support
PowerFlex 750 Series HIM Startup
5.0 / 5 (0 votes)