DC Motor

Materials Needed:

  • (1) Small 6V DC Motor
  • (1) PN2222 Transistor
  • (1) 1N4001 diode
  • (1) 270 Ω Resistor (red, purple, brown stripes)
  • (1) Half-size breadboard
  • (1) Arduino Uno R3
  • Jumper Wires

Code:

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);

    }

  }

Layout: