Arduino Code

Load up the following sketch onto your Arduino.  

Adafruit Arduino - Lesson 13. DC Motor

int motorPin = 3;
void setup() 
  pinMode(motorPin, OUTPUT);
  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);

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.

When the sketch starts, it prompts you, to remind you that 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 could type any number here, so the 'if' statement on the next line only does an analog write with this number if the number is between 0 and 255.

Last updated on 2015-05-04 at 04.27.11 PM Published on 2012-12-17 at 10.54.43 AM