Using an Arduino Motor Shield with an L298N chip

Using an Arduino Motor Shield with an L298N chip

This project demonstrates how to control two DC motors using an Arduino Uno and a motor shield based on the L298N motor driver chip. This versatile setup allows for independent control of each motor's speed and direction, along with braking functionality. The shield simplifies the wiring and provides a compact solution for driving motors in various projects.

Here are some project ideas using this setup:

  • Build a small robot with independent wheel control.
  • Create a motorized platform for a camera or sensor.
  • Design a simple conveyor belt system.
  • Control the movement of a small vehicle or toy.

Hardware/Components

  • Arduino Uno
  • L298N Motor Shield
  • Two DC motors
  • External power supply (for larger motors)
  • Connecting wires

Wiring Guide

The motor shield plugs directly onto the Arduino Uno. Connect the motors to the screw terminals labeled "Motor A" and "Motor B" on the shield (in video at 08:27). The polarity of the motor connections doesn't matter initially, as the code controls the direction. If the motor spins the wrong way, simply swap the wires. For small motors consuming less than 500mA, power can be drawn directly from the Arduino. Larger motors will require an external power supply connected to the shield's power terminals (in video at 09:03).

%%WIRING%%

Code Explanation

The code defines pin assignments for each motor's direction, speed, and brake. It's crucial to use the correct pins corresponding to the motor shield's design. The code also includes a helpful function, moveMotor(), for simplified control (in video at 13:21).


// Pin assignments for Motor A & B (DO NOT CHANGE)
const int MotorPinA = 12; 
const int MotorSpeedPinA = 3; 
const int MotorBrakePinA = 9;

const int MotorPinB = 13; 
const int MotorSpeedPinB = 11;
const int MotorBrakePinB = 8;

const int CW  = HIGH; // Clockwise direction
const int CCW = LOW; // Counter-clockwise direction

The moveMotor() function takes the motor ('A' or 'B'), direction (CW or CCW), and speed (0-255) as arguments (in video at 13:21). This function simplifies motor control within the loop() function.


void moveMotor(char motor, int dir, int speed) {
  // ... (implementation details omitted)
}

The brake() function allows you to apply or release the brake for a specific motor (in video at 14:06). It takes the motor ('A' or 'B') and brake state (1 for brake, 0 for release) as arguments.


void brake(char motor, int brk) {
  // ... (implementation details omitted)
}

Live Project/Demonstration

The video demonstrates controlling individual motors, adjusting speed and direction, and applying the brake. It also shows how to control two motors simultaneously with different parameters, including running a large motor alongside a smaller one (in video at 19:14).

Chapters

  • [00:00] Introduction to the Arduino Motor Shield
  • [00:59] Explanation of the Motor Shield
  • [06:41] Overview of the L298N Datasheet
  • [08:27] Hardware Demonstration and Motor Connections
  • [09:28] Code Explanation: Basic Motor Control
  • [12:36] Demonstration: Single Motor Control
  • [13:21] Code Explanation: Advanced Functions (moveMotor, brake)
  • [14:39] Demonstration: Advanced Motor Control
  • [17:43] Demonstration: Dual Motor Control
  • [19:14] Demonstration: Controlling Motors with Different Power Requirements
148-Basic code to control a DC motor using an Arduino motor shield
زبان: C++
149-Advanced code to control a DC motor using an Arduino motor shield
زبان: C++
کپی شد!

منابع و مراجع

فایل‌ها📁

هیچ فایلی موجود نیست.