Arduino Motor DC
Arduino Motor DC
show you how to connect the motor and drive it from your board.
DC motor
Servo motor
Stepper motor
A DC motor (Direct Current 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.
Warning − Do not drive the motor directly from Arduino board pins. This may damage the board.
Use a driver Circuit or an IC.
Components Required
1x PN2222 Transistor
1x Small 6V DC Motor
1x 1N4001 diode
1x 270 Ω Resistor
Procedure
Follow the circuit diagram and make the connections as shown in the image given below.
Precautions
Second, the striped end of the diode should be towards the +5V power line according to the
arrangement shown in the image.
int motorPin = 3;
void setup() {
void loop() {
digitalWrite(motorPin, HIGH);
Code to Note
The transistor acts like a switch, controlling the power to the motor. Arduino pin 3 is used to turn
the transistor on and off and is given the name 'motorPin' in the sketch.
Result
Motor will spin in full speed when the Arduino pin number 3 goes high.
Arduino Code
int motorPin = 9;
void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop() {
if (Serial.available()) {
analogWrite(motorPin, speed);
Code to Note
The transistor acts like a switch, controlling the power of the motor. Arduino pin 3 is used to turn
the transistor on and off and is given the name 'motorPin' in the sketch.
When the program starts, it prompts you to give the values to control the speed of the motor.
You need to enter a value between 0 and 255 in the Serial Monitor.
In the 'loop' function, the command 'Serial.parseInt' is used to read the number entered as text
in the Serial Monitor and convert it into an 'int'. You can type any number here. The 'if'
statement in the next line simply does an analog write with this number, if the number is
between 0 and 255.
Result
The DC motor will spin with different speeds according to the value (0 to 250) received via the
serial port.