hi
my name is Massimo Banzi I like to make
stuff welcome to another Arduino
tutorial video today we're gonna be
learning how to use Arduino to move
things in the real world to do that we
need to learn how to control a DC motor
using Arduino so the DC motor is a
simple electromechanical device that you
see here that it's normally powered by a
9-volt battery and we're going to be
building a circuit that lets Arduino
turn on and off this motor and we'll be
using that to control this color wheel
that we have manufactured using an old
CD so in the kit you will find parts in
order to build the wheel adapter and you
will find a paper that you can glue on
top of the CD so what are the issues
that we have to take care of well first
of all the DC motor here works normally
at more than five volts which is the
standard voltage that the Arduino
operates at and requires more current
than a single Arduino pin can provide
normally we can just hook up a regular
led to an Arduino pin because the amount
of electrical current that the LED it
needs in order to operate is low enough
that you can power it with an Arduino
pin but in the case of the DC motor the
DC motor requires a current which is
much higher and we risk burning the
Arduino pin if we try to hook it up
directly
there's also another issue that we have
to be aware of is that when you turn on
and off a motor an electrical motor when
you turn it off actually it generates a
spike of negative voltage that can
actually go back
into your equipment and destroy some of
the parts so in order to solve this
problem we are going to use a new
component that we haven't used in the
other videos which is called the MOSFET
transistor this is essentially a switch
that can be turned on and off by
applying or or not a voltage to a
certain pin of the MOSFET transistor so
the MOSFET here has three pins called
source drain and gate the power MOSFET
is essentially an electronic switch that
can be turned on or off by applying a
voltage on a pin called the gate so this
MOSFET transistor has three pins called
gate source and drain so if you apply a
voltage to the gate pin it connects the
gate and the source together like I was
pressing a button on a switch but it is
all done electronically so I can use
this to connect the battery to the motor
and since that MOSFET is sitting in
between it basically connects and
disconnects the motor from the battery
and I can control this through software
that I write on the Arduino board when
you turn off an electric motor it
normally generates a spike of negative
voltage they can actually destroy your
equipment so even if the MOSFET is quite
strong it's still very sensitive to
these negative spikes of voltage so we
have added to the circuit this flywheel
diode
it's called typically conducts only when
the motor generates these dangerous
spikes of voltage and protects the
MOSFET from burning so what the MOSFET
is doing for us
it lets us control
loads there are larger then we can
normally do with an Arduino pin it lets
us operate at a voltage which which is
higher than the standard Arduino voltage
so as I said Arduino operates at 5 volts
but with the battery here it's 9 volts
so using the MOSFET allows us to switch
on and off bigger loads that operates at
voltages that are higher than the
Arduino standard operating voltage it
protects us because if something happens
the MOSFET blows up at worst but using
the diode the way we hooked it up here
we can actually protect the MOSFET then
we have a fairly reliable and robust way
to turn on and off but even change the
speed if we want of this DC mode so
let's look at how we can build this
circuit first of all we place the MOSFET
on the breadboard and then we connect
that negative the black wire of the
motor right in the middle pin then if
you look at the MOSFET from the front
where you can see the markings on the
front then the pin on the left-hand side
that's the gate so we're gonna wire it
up to P number 9 on your Arduino and
then the pin on the right hand side
that's the ground so we're gonna connect
it with the jumper wire to the ground
rail here on the breadboard then we're
gonna connect the ground from the
battery together with the ground on the
breadboard so that the battery and the
Arduino I've got the ground in common
this is a condition needed so that the
power supply on the Arduino and the
battery I've got the ground in common so
the voltages are all referring to the
same ground and the circuit can operate
properly it works like this we connect
the 9-volt coming from the battery
directly to the motor and then from the
motor we connect the ground pin of the
motor to the MOSFET and then AM ostracon
the actual ground on the circuit so when
the Arduino pin turns on and off a five
volts voltage will be applied on the
gate when the gate receives the voltage
from the Arduino pin it will connect the
motor to ground and the motor will start
to spin when we remove the voltage from
the gate pin the MOSFET will open and
the circuit will break and the motor
will stop running let's look at the
sensor part of the circuit in our case
the sensor is a button so we wire up the
circuit in the usual way we have a
button here we have the resistor which
is a pulldown resistor so we connect
power to the button button to resistor
resistor to the ground and the point
where the button and the resistor
connect is where we connect a wire to
take that voltage and bring it to pin
number two on the Arduino so every time
I press the button the Arduino detect
that condition and turns on the MOSFETs
so here we have a motor and there's a
small adapter that adapts the motor
shaft with this pin wheel that we
manufactured using an old CD and a piece
of paper that you can find in the kit so
once I created the adapter I'm gonna put
a little bit of glue on it so that the
CD is not gonna fly away the moment I
turn on the motor so let's put a few
drops of glue let's try
you see now that it's picking up speed
and it's turning to this interesting
cappuccino color and if I release the
button the motor starts to slow down
that's pretty good okay so this was our
example and now let's have a look at the
code so starting from the beginning we
have a couple of constants switch pin
which maps the switch to pin number two
and motor pin but not the motor on to
pin number nine and then we have a
variable called switch state equal zero
which will contain the state of the push
button and it will be used in an if
statement to determine if the motor has
to be on or off
then let's look at the setup it is that
we have pinmode motor pin output so that
defines the pin that connects to the
mosfet and controls the motor is an
output and pin mode switch pin input
that basically says that the pin
connecting to the push pattern it's an
input then let's now look at the loop he
said the loop we begin by reading the
state of the button by saying switch
state equal digital read the switch pin
so this reads the current state of the
button and in places high or low inside
the switch state variable after that we
have an if statement if switch state
equal equal high so if the button is
pressed digital right motor pin high so
that turns on the motor else digital
right motor pin low and this if
statement looks at the state of the
button if the button is pressed we turn
on the pin if the button is released we
turn off the pin when the pin is on the
MOSFET
and starts the motor this is all the
code that's needed to build this simple
application now you can hack this the
software and add more functionality for
example if you look online you may be
able to find some code that teaches you
how to turn on and off the motor when
you press or if you press again it turns
off so in order to operate a toggle
switch or you can learn how to change
the speed of the motor so the number of
things you can do now with this project
are a lot if you remember to build it
hack it and then share it because
Arduino is you