Arduino - DC Motor



    Sohail Gulam


    In this chapter, we will interface different types of motors with the Arduino board (UNO) and show you how to connect the motor and drive it from your board.

    There are three different type of motors βˆ’

    DC motor Servo motor Stepper motor A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.

    int motorPin = 9;
    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);
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.