Section outline

  •  

    Motors are what give robots the ability to move – to spin wheels, lift arms, or rotate sensors. In this section, we’ll explore how to connect and control basic DC motors using an Arduino board.

    • ⚙️ Types of Motors Used in Robots

      • DC Motor – Spins in one direction or the other. Simple and fast.
      • Servo Motor – Rotates to a specific angle (0° to 180°). Great for precise control.
      • Stepper Motor – Moves in small steps. Used in CNC machines and printers.

      🔌 Connecting a DC Motor to Arduino

      You can't connect a DC motor directly to an Arduino. Why?

      • Motors need more current than Arduino can provide.
      • To reverse motor direction, we need a special circuit.

      This is where a motor driver like the L298N or L293D comes in. It acts like a middleman that lets Arduino control the motor safely.

      📦 Components Needed

      • 1 x Arduino Uno
      • 1 x L298N or L293D Motor Driver
      • 2 x DC motors (like BO motors)
      • 1 x External power source (battery or USB power bank)
      • Jumper wires

       

    • 🔁 Controlling Motor Direction with Code

      Here's a simple example of Arduino code to spin a DC motor in both directions using a motor driver:

      // Motor pins
      int in1 = 7;
      int in2 = 8;
      
      void setup() {
        pinMode(in1, OUTPUT);
        pinMode(in2, OUTPUT);
      }
      
      void loop() {
        // Forward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        delay(1000);
      
        // Reverse
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        delay(1000);
      }
      

      📘 How the Code Works

      • in1 and in2 are connected to the motor driver.
      • Setting one HIGH and the other LOW moves the motor in a specific direction.
      • Switching the HIGH/LOW values reverses the direction.

       

    • 🧠 Important Tips

      • Always use a separate power supply for motors to avoid damaging Arduino.
      • If motors don’t spin, check wiring and motor driver connections.
      • Use delay() for testing but consider more advanced timing later.

      🏁 Up Next

      Now that your robot can move, let’s teach it how to see where it’s going! In the next section, we’ll add line sensors and learn how to detect paths.