Arduino Controlling a Small DC Motor
So I received a question some time ago (sorry to whomever posted it):
What pin do I hook up a motor to?
The short answer: you don't, not directly.
A direct current, or DC, motor is the most common type of motor. DC motors
normally have just two leads, one positive and one negative. If you connect these
two leads directly to a battery, the motor will rotate. If you switch the leads, the
motor will rotate in the opposite direction.
The Arduino can only provide 40mA at 5V on its digital pins. Most motors require
more current and/or voltage to operate. A transistor can act as a digital switch,
enabling the Arduino to control loads with higher electrical requirements. The
transistor in this example completes the motor's circuit to ground. Arduinos are
great to direct drive an led or two, but a DC motor will draw too much current, and
will probably fry the Arduino. Here is the circuit to drive the motor:
(In this case, the transistor I'm using is a P2N2222AG, not much different
from a standard 2N2222 - it's what I had within reach when I made the video.)
Now, if you watch the
video, you will see that
this circuit differs from
the one I show in the video,
in that there is an extra 330
Ohm resistor in parallel to
the 10 K Ohm resistor, this
is because the 10 K resistor
was too high a value, the
voltage to turn on the transistor
was dropped too much, and the transistor would not turn on. With the
330 Ohm and 10K ohm in parallel, the circuit works just fine.
Here is the breadboard layout created in Fritzing:
Here is the video:
Here is the Sketch:
int motorPin = 9; // DC motor is connected to this pin
void setup() {
pinMode(motorPin, OUTPUT);
}
void loop() {
motorOnThenOff();
}
/*******************************************************/
/* MotorOnTheOff turns the motor on, then off */
/* Code is more-or-less the same as turning an LED */
/* on and off */
/*******************************************************/
void motorOnThenOff(){
int onTime = 1000; // How long does it run?
int offTime = 2000; // Turn it off
digitalWrite(motorPin, HIGH); // This command turns the motor on
delay(onTime); // run for this many milliseconds
digitalWrite(motorPin, LOW); // We're done? Turn motor off
delay(offTime); // Wait for offtime to complete
}
Have fun!
γγγΎγ
A cosmic game of chess, sort of, at least, that's how I am portraying things in this 3 - part video series. The moves which led up to Easter, and check. Part 3 (who knows when that will arrive) will describe mate - an event in the future. When I was much younger, I used to play a chess game called "Battle Chess" it was a chess game which used all the standard rules of regular chess, but the pieces walked to their spots on the board, then they attacked when taking an opponant's piece. This was as close as I could come visually.
View the video: