Sunday 6 July 2014

Arduino Run DC Motor

Hardware Required:
1-Small 6V DC Motor 
2-PN2222 Transistor 
3-1N4001 diode 
4-270 Ω Resistor (red, purple, brown stripes) 
5-Breadboard 
6-Arduino Uno R3 
7-Jumper wire pack

Breadboard Layout
When you put together the breadboard, there are two things to look out for.
Firstly, make sure that the transistor is the right way around. The flat side of the transistor
should be on the right-hand side of the breadboard.
Secondly the striped end of the diode should be towards the +5V power line 
The motor can be connected either way around.

Run The Following Code on Arduino IDE:

int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}

No comments:

Post a Comment