Arduino Lesson 7: The Moving Motor – Make Your Arduino Control a Motor!

In this lesson, we’ll explore the world of motors and learn how to control them using Arduino! Get ready to make things move with your own mini-robot!

Objective: Learn how to control a DC motor using Arduino.

Materials:

  • Arduino Uno board
  • Breadboard
  • DC motor
  • Motor driver module (L298N)
  • Jumper wires
  • 9V battery (or power supply)
  • Computer with Arduino IDE software installed

Steps:

  1. Review: Discuss previous lessons and components, introducing the concept of motors and motor drivers.
  2. Meet the Components: Introduce the DC motor and the motor driver module. Explain their functions.
  3. Building the Circuit: Connect the motor, motor driver, Arduino board, and battery to the breadboard. Make sure to connect the motor driver to the Arduino’s digital output pins according to the motor driver’s specifications.

Step 1: Connecting the Motor Driver

  1. Find the pins on the motor driver: The L298N module typically has pins for motor control (like IN1, IN2, IN3, IN4), power (like 12V, GND), and enabling (like EN).
  2. Connect the motor driver to the Arduino: Connect the motor driver’s IN1 and IN2 pins to digital output pins on the Arduino (e.g., pins 9 and 10). Connect the EN pin to a digital output pin as well (e.g., pin 11).
  3. Connect the motor driver’s power pins to the battery: The 12V pin goes to the positive terminal of the battery, and the GND pin goes to the negative terminal.

Step 2: Connecting the Motor

  1. Find the two wires on the DC motor: One is positive (+) and the other is negative (-).
  2. Connect the positive wire of the motor to the motor driver’s output pins: Connect the motor’s positive wire to one of the output pins (e.g., OUT1 on the motor driver).
  3. Connect the negative wire of the motor to a ground rail on the breadboard:

Step 3: Programming the Arduino

  1. Open the Arduino IDE.
  2. Copy and paste the following code:
const int motorPin1 = 9; // Motor control pin 1
const int motorPin2 = 10; // Motor control pin 2
const int enablePin = 11; // Enable pin for motor driver

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin, OUTPUT); 
  digitalWrite(enablePin, HIGH); // Enable the motor driver
}

void loop() {
  digitalWrite(motorPin1, HIGH); 
  digitalWrite(motorPin2, LOW);  // Rotate motor forward
  delay(2000);
  
  digitalWrite(motorPin1, LOW); 
  digitalWrite(motorPin2, HIGH);  // Rotate motor backward
  delay(2000);

  digitalWrite(motorPin1, LOW); 
  digitalWrite(motorPin2, LOW);  // Stop the motor
  delay(1000);
}
  1. Upload the code to the Arduino board.

Step 4: Observe the Motor

  • You should see the motor spinning forward, backward, and then stopping, according to the code.

Congratulations! You’ve made your Arduino control a motor!

Let’s Experiment:

  • Change the delay: Try adjusting the delay() values to make the motor spin faster or slower.
  • Add a button: Connect a button to the motor driver’s enable pin (EN) to control when the motor is running.
  • Explore different motors: Try using different types of motors to see how they respond to control.

Keep experimenting with motors and have fun with Arduino!