Tutorial 7: Control a Motor

LEDs get boring fast and everyone loves robots. Motors allow you to turn your programming thoughts into mechanical movement and that's when the fun really begins.

In this tutorial I show you how to connect a motor to your Arduino. Connecting a motor to your Arduino is an easy way to do damage, so I explain how you can control the motor without having your Arduino produce magic smoke. Have fun!


DIFFICULTY
EASY
CIRCUITRY KNOWLEDGE
SOME
C++ PROGRAMMING
LITTLE
ABOUT
0
MINUTES
  • Current / power limitations of an Arduino
  • How to use a NPN BJT transistor
  • How to connect a low power motor to your Arduino
  • Concept of a flyback diode
You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!


int PWMPin = 9 ;
int intensity = 0 ;

void setup() {
  pinMode(PWMPin, OUTPUT) ;
  Serial.begin(9600) ;
}

void loop() {
  intensity += 1 ;
  if (intensity > 255){
    intensity = 0 ;
  }

  analogWrite(PWMPin, intensity) ;
  Serial.println(intensity) ;
  delay(20) ;
}

}
You will need to download a few things to get the most out of this tutorial: