CIPAD Leçon 16: Comment contrôler la polarité d'alimentation d'un moteur à sourant continu

3 min read 2 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 will guide you through the process of controlling the polarity of the power supply for a direct current (DC) motor using Arduino. Understanding how to reverse the polarity is crucial for applications that require bidirectional motor control, such as robotics, conveyor belts, and other automation systems.

Step 1: Gather Required Components

Before you start, ensure you have the following components:

  • Arduino board (e.g., Arduino Uno)
  • DC motor
  • Motor driver (H-bridge, such as L298N)
  • Power supply (suitable for your motor)
  • Jumper wires
  • Breadboard (optional for easier connections)

Practical Tip

Make sure your power supply voltage matches the requirements of your DC motor to prevent damage.

Step 2: Wiring the Components

Connect the components according to the following instructions:

  1. Connect the motor to the motor driver:

    • Attach the two terminals of the motor to the output pins of the motor driver (e.g., OUT1 and OUT2).
  2. Connect the motor driver to the Arduino:

    • Connect the input pins of the motor driver (e.g., IN1 and IN2) to two digital pins on the Arduino (e.g., pin 8 and pin 9).
  3. Power the motor driver:

    • Connect the power supply's positive terminal to the VCC of the motor driver.
    • Connect the ground terminal of the power supply and the Arduino together with the GND of the motor driver.
  4. Optional: Use a breadboard

    • For easier connections, you can use a breadboard to manage and organize your wiring.

Common Pitfall

Ensure all connections are secure to avoid intermittent power issues.

Step 3: Writing the Arduino Code

Now, write the Arduino code to control the motor. Open the Arduino IDE and enter the following code:

#define motorPin1 8
#define motorPin2 9

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void loop() {
  // Rotate motor forward
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  delay(2000); // Run for 2 seconds

  // Rotate motor backward
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  delay(2000); // Run for 2 seconds

  // Stop the motor
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  delay(2000); // Stop for 2 seconds
}

Explanation of the Code

  • The motorPin1 and motorPin2 are defined to control the direction of the motor.
  • In the setup function, the pins are set as outputs.
  • The loop function alternates the motor direction every 2 seconds.

Step 4: Uploading the Code to Arduino

  1. Connect your Arduino to the computer using a USB cable.
  2. Select the correct board and port in the Arduino IDE.
  3. Click on the upload button to transfer the code to the Arduino.

Practical Tip

Check for any compilation errors in the IDE before uploading.

Step 5: Testing the Motor Control

After successfully uploading the code:

  1. Power on the motor driver using the external power supply.
  2. Observe the motor as it runs forward for 2 seconds, then reverses for another 2 seconds.
  3. The motor should stop for 2 seconds and repeat the cycle.

Common Pitfall

If the motor does not operate as expected, double-check all connections and the code for any errors.

Conclusion

You have now successfully set up a system to control the polarity of a DC motor using an Arduino. This foundational skill is essential for various robotics and automation projects. Next, consider exploring advanced topics such as speed control using PWM or integrating sensors for feedback. Happy building!