字幕表 動画を再生する
Hi my name is Massimo Banzi and I like to make stuff.
Welcome to another Arduino tutorial video.
Today we are going to learn
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.
The DC motor is a simple
electro-mechanical device
that you can see here.
It is normally powered
by a 9V battery
We are going to build a circuit
that will let Arduino
turn on and off this motor.
And we will be using that
to control this colour wheel
that we have manufactured
using an old CD.
In the kit, you will find parts
in order to build the wheel adapter,
and you will find the paper
that you can glue on top of the CD.
There are some issues that we have to
take care of.
First of all the DC motor here
works normally at more than 5V,
which is the standard voltage
that Arduino operates at,
and it 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 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 motor requires a current
which is much higher,
and we risk burning
the Arduino pin
if we try to hook it up directly.
There is also another issue
that we have to be aware of:
when you turn on and off
an electric 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.
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 'mosfet transistor'
This is essentially
a switch that can be
turned on and off by
applying - or not -
a voltage to a certain pin
of the mosfet transistor.
The mosfet here has 3 pins
called source, drain, and gate.
The mosfet is essentially
an electronic switch that can be
turned on or off by
applying a voltage on a pin
called 'gate'.
So this mosfet transistor
has 3 pins called gate,
source and drain.
If you apply a voltage to the gate pin
it connects
the gate and the source together,
as if I was pressing
a button on a switch,
but this is all done electronically.
So I can use this
to connect the battery
to the motor,
and since the 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
that can destroy your equipment.
Even if the mosfet
is quite strong
it is still very sensitive
to these negatives spikes of voltage,
so we have added to the circuit
this "flywheel diode"
that 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 that are
larger than we can
normally manage with an Arduino pin;
it lets us operate at a voltage,
which is higher
than the standard Arduino voltage.
As I said,
Arduino operates at 5V
but the battery here is 9V.
Using the mosfet allows us
to switch on and off bigger loads
that operate 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 protect
the mosfet and we have a fairly reliable
and robust way to turn on and off,
but even change the speed, if we want,
of this DC motor.
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 the 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,
the pin on the left hand side
is the 'gate'.
We are going to wire it up
to pin number 9
on your Arduino.
The pin on the right hand side,
that's the ground.
We are going to
connect it with the jumper wire
to the ground rail
here on the breadboard.
Then we are going to connect
the ground from the battery
together with the ground
on the breadboard so that the battery
and the Arduino
have the ground in common.
This is a condition
needed so that
the power supply on the Arduino
and the battery have the ground in common
so that the voltages are all referring
to the same ground
and the circuit can operate properly.
So the circuit works like this.
We connect the 9v
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 the mosfet connects to the
actual ground on the circuit.
So when the Arduino pin
turns on and off,
a 5v voltage
will be applied to 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 pull-down resistor,
so we connect power to the button,
button to resistor, resistor to ground,
and the point where the button
and the resistor connect,
that's where we connect the wire
to take that voltage
and bring it to pin 2 on the Arduino.
Every time I press the button,
the Arduino detects that condition
and turns on the mosfet.
Here we have a motor
and here is a small adapter
which adapts the motor shaft
to this pinwheel
that we manufactured using al old CD
and a piece of paper
that you can find in the kit.
Once I created the adapter,
I am going to put a little bit of glue on it,
so that the cd is not going to
fly away the moment I turn on the motor.
Let's put a few drops of glue.
Let's try.
You can see now that it is picking up speed
and it's turning into this interesting
cappuccino colour.
If I release the button
the motor starts to slow down.
That's pretty good.
So this was our example
and now let's have a look at the code.
Starting from the beginning
we have a couple of constants.
switchPin,
that maps the switch to pin number 2,
and motorPin
that maps the motor on pin 9
Then we have a variable called
switchState = 0;
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()
There is a pinMode(motorPin, OUTPUT);
that defines that the pin
that connects to the mosfet
and controls the motor is an output.
And pinMode(switchPin, INPUT)
that basically says that the pin
connecting to the pushbutton is an input.
Then let's now look at the main loop.
Inside the loop we
begin by reading the state of the button
by saying
switchState = digitalRead(switchPin);
This reads the current state
of the button and then places
HIGH or LOW values
inside the switchState variable.
After that we have an if-statement.
If (switchState == HIGH),
so if the button is pressed,
digitalWrite(motorPin, HIGH);
which turns on the motor.
else digitalWrite(motorPin, LOW);
This if-statement
looks at the state of the button
and 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 connects
and it starts the motor.
This is all the code that is needed
to build this simple application.
Now you can
hack 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 the motor when you press once,
or if you press again it turns it off,
or 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.
Remember: build it,
hack it and share it,
because Arduino is you!