In this tutorial you will going to learn how to control and drive the DC Motor on Arduino, you will also learn how to used the analog output PWM or Pulse with Modulation to control the speed of the motor by changing the value of 0 ~ 255 from the serial monitor.
Electronic parts required
1x 6v DC Motor (DC motor is a electrical machines that converts DC Direct Current electrical power into mechanical power. Most common types rely on the forces to produces a magnetic fields.)
1x PN2222 Transistor ( You cannot directly place the motor to the Arduino digital output, there is a change that you could damage the Arduino. Transistor PN222 can be used as a switch that can handle small current from the Arduino digital output to control the bigger current of the motor )
Wiring the motor on to Arduino
Placing the transistor should be in the right way, please see the diagram above to know the transistor pin, the same with the diode. Diode only allows the flow of current in one direction. see also the diagram above for the diode pin out. the striped end of the diode should be towards into +5v at Arduino power line.
The PN222 Transistor has a 3 leads most of the electricity flows from the collector going to the emitter, this will only happen if a small amount of current flowing into the base connection. this small current will supplied by the Arduino digital output.
The digital 5 of the Arduino is connected to the resistor the same like using an LED this resistor will limits the current flowing into the transistor through the base, the diode connected across the connection of the motor, when you turn the power off to the motor, you will get a negative SPIKE of voltage that can damage your Arduino or your transistor, the diode protects against this SPIKE by shorting out any such reverse current from the motor.
Starter #18 Controlling a DC Motor
int MOTOROLL =5;
Serial.begin(9600); // Opening the serial communication at baud rate 9600
Serial.println(“Turning Speed > 0 to 255″); // Serial Output Window
int TurningSpeed =Serial.parseInt();
if(TurningSpeed >=0&& TurningSpeed <=255) // Turning Speed PWM at 0