CIPAD, Leçon 18 dernière partie : Les ponts en H , commandes manuelle et télécommande IR.
Table of Contents
Introduction
This tutorial guides you through the final project of Lesson 18 in the CIPAD Arduino programming course. You will learn how to control a direct current (DC) motor manually and via an infrared (IR) remote control. This lesson wraps up concepts introduced in previous lessons, helping you consolidate your knowledge and expand your skills in Arduino programming.
Step 1: Assemble the Circuit
To begin, you need to set up the circuit that connects the components necessary for controlling the DC motor.
-
Gather Components
- Arduino board (e.g., Arduino Uno)
- DC motor
- H-bridge motor driver (e.g., L298N)
- IR remote control and receiver
- Power supply (for the motor)
- Jumper wires
- Breadboard (optional for organization)
-
Connect the Components
- Connect the DC motor to the H-bridge motor driver.
- Connect the H-bridge to the Arduino using the following pin connections:
- IN1 to a digital pin (e.g., pin 9)
- IN2 to another digital pin (e.g., pin 10)
- Enable pin (EN) to a PWM-capable pin (e.g., pin 11)
- Connect the IR receiver to the Arduino:
- Signal pin to a digital pin (e.g., pin 2)
- VCC to +5V
- GND to GND
-
Power the Circuit
- Ensure that the H-bridge is powered appropriately, typically from an external power source, depending on the motor specifications.
Step 2: Program the Arduino
Next, you'll need to upload the code that allows the Arduino to interpret signals from the IR remote and control the motor.
-
Install Required Libraries
- Use the Arduino IDE to install the IRremote library for handling IR signals. You can do this via the Library Manager.
-
Write the Code
- Start a new sketch and include the IRremote library at the top:
#include <IRremote.h>
- Define the pins:
const int motorPin1 = 9; const int motorPin2 = 10; const int enablePin = 11; const int recv_pin = 2;
- Initialize the IR receiver:
IRrecv irrecv(recv_pin); decode_results results;
- In the
setup()
function, initialize the motor pins and start the IR receiver:
void setup() { pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(enablePin, OUTPUT); irrecv.enableIRIn(); }
- In the
loop()
function, check for IR signals and control the motor:
void loop() { if (irrecv.decode(&results)) { switch (results.value) { case YOUR_REMOTE_CODE_FOR_FORWARD: digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); break; case YOUR_REMOTE_CODE_FOR_BACKWARD: digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); break; case YOUR_REMOTE_CODE_FOR_STOP: digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); break; } irrecv.resume(); } }
- Replace
YOUR_REMOTE_CODE_FOR_FORWARD
, etc., with the actual hexadecimal codes from your remote. You can find these by running a simple sketch to print received codes to the Serial Monitor.
-
Upload the Code
- Connect your Arduino to your computer and upload the sketch. Open the Serial Monitor to check if the IR signals are being received.
Step 3: Test the Setup
After uploading the code, it's time to test your setup.
-
Power Up the Circuit
- Ensure all connections are secure and power the circuit.
-
Use the Remote Control
- Press the buttons assigned in your code to control the motor:
- One button for forward movement
- One for backward movement
- One for stopping the motor
- Press the buttons assigned in your code to control the motor:
-
Observe Behavior
- Ensure the motor responds correctly to the IR remote commands. If it doesn't, double-check the wiring and code for any errors.
Conclusion
You have now successfully built a project that allows you to control a DC motor using both manual commands and an IR remote control. This project integrates concepts from previous lessons and provides a practical application of your Arduino skills.
For further exploration, consider modifying the code to add more features, like speed control using PWM or adding sensors for more complex interactions. Happy coding!