CIPAD 28: La méthode la plus simple pour Contrôler le sens et la vitesse de rotation de 2 moteur CC

3 min read 4 hours ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a straightforward method for controlling the direction and speed of two DC motors using the L293D dual H-bridge motor driver. Designed for beginners in Arduino programming, this guide will walk you through the necessary steps to set up and program your motors effectively. By the end, you'll have a solid understanding of how to implement this control method in your projects.

Step 1: Gather Required Components

Before starting, ensure you have the following components:

  • Arduino board (e.g., Arduino Uno)
  • L293D motor driver IC
  • Two DC motors
  • Power supply (compatible with your motors)
  • Jumper wires
  • Breadboard (optional, for easier connections)

Practical Tip

Make sure your power supply voltage matches the specifications of your motors to avoid damaging them.

Step 2: Make the Circuit Connection

  1. Connect the L293D to the Arduino:

    • Pin 1 (Enable 1) to Arduino digital pin (e.g., pin 9)
    • Pin 2 (Input 1) to Arduino digital pin (e.g., pin 2)
    • Pin 7 (Output 1) to one terminal of Motor 1
    • Pin 10 (Output 2) to the other terminal of Motor 1
    • Pin 3 (Input 2) to Arduino digital pin (e.g., pin 3)
    • Pin 6 (Enable 2) to Arduino digital pin (e.g., pin 10)
    • Pin 11 (Output 3) to one terminal of Motor 2
    • Pin 14 (Output 4) to the other terminal of Motor 2
  2. Power Connections:

    • Connect the power supply to Pin 8 (Vcc2) of the L293D.
    • Connect the ground (GND) of the L293D to the Arduino GND.

Common Pitfalls to Avoid

  • Ensure all connections are secure to prevent intermittent issues.
  • Double-check the pin configuration to ensure proper functionality.

Step 3: Write the Arduino Code

You will need to upload a simple code to your Arduino to control the motors.

// Define pin numbers
const int enable1 = 9;
const int motor1Pin1 = 2;
const int motor1Pin2 = 3;
const int enable2 = 10;
const int motor2Pin1 = 4;
const int motor2Pin2 = 5;

void setup() {
  // Set motor control pins as outputs
  pinMode(enable1, OUTPUT);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
}

void loop() {
  // Rotate Motor 1 forward
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  analogWrite(enable1, 255); // Set speed

  // Rotate Motor 2 backward
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
  analogWrite(enable2, 255); // Set speed

  delay(2000); // Run motors for 2 seconds

  // Stop motors
  analogWrite(enable1, 0);
  analogWrite(enable2, 0);
  delay(2000); // Stop for 2 seconds
}

Explanation of Code

  • The pinMode function sets up the pins used for motor control.
  • The loop function controls the motors' direction and speed.
  • Use analogWrite to adjust the speed, where 0 is stopped and 255 is maximum speed.

Step 4: Upload the Code and Test

  1. Connect your Arduino to your computer via USB.
  2. Open the Arduino IDE and paste the code.
  3. Select the correct board and port from the tools menu.
  4. Click on the upload button to compile and upload the code.
  5. Once uploaded, observe the motors to ensure they rotate as expected.

Practical Tip

If the motors do not respond, check your wiring and code for any errors.

Conclusion

In this tutorial, you learned how to control two DC motors using the L293D motor driver with an Arduino. You gathered the necessary components, connected them correctly, and uploaded a simple code to manage the motors' direction and speed. As a next step, you can experiment with varying speeds and directions or integrate additional sensors to expand your project’s functionality. Happy coding!