Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- L293D is a motor driver IC that can drive two DC motors or one stepper motor simultaneously. To help you get started, here's an example code for controlling two DC motors using an Arduino and L293D Dual Motor Driver IC:
- This code will drive two DC motors connected to the L293D motor driver. The motors will move forward at full speed for 2 seconds, stop for 1 second, move backward at half speed for 2 seconds, and stop for 1 second. This loop will continue indefinitely.
- */
- // Include the Arduino library
- #include <Arduino.h>
- // Pin connections to L293D
- const int motor1_enable = 3; // Enable pin for Motor 1
- const int motor1_input1 = 2; // Input 1 pin for Motor 1
- const int motor1_input2 = 4; // Input 2 pin for Motor 1
- const int motor2_enable = 5; // Enable pin for Motor 2
- const int motor2_input1 = 6; // Input 1 pin for Motor 2
- const int motor2_input2 = 7; // Input 2 pin for Motor 2
- void setup() {
- // Set the motor control pins as outputs
- pinMode(motor1_enable, OUTPUT);
- pinMode(motor1_input1, OUTPUT);
- pinMode(motor1_input2, OUTPUT);
- pinMode(motor2_enable, OUTPUT);
- pinMode(motor2_input1, OUTPUT);
- pinMode(motor2_input2, OUTPUT);
- }
- void loop() {
- // Move Motor 1 forward at full speed
- digitalWrite(motor1_input1, HIGH);
- digitalWrite(motor1_input2, LOW);
- analogWrite(motor1_enable, 255); // 255 for full speed, can be adjusted from 0 to 255
- // Move Motor 2 forward at full speed
- digitalWrite(motor2_input1, HIGH);
- digitalWrite(motor2_input2, LOW);
- analogWrite(motor2_enable, 255); // 255 for full speed, can be adjusted from 0 to 255
- delay(2000); // Run the motors for 2 seconds
- // Stop both motors
- digitalWrite(motor1_enable, LOW);
- digitalWrite(motor2_enable, LOW);
- delay(1000); // Wait for 1 second
- // Move Motor 1 backward at half speed
- digitalWrite(motor1_input1, LOW);
- digitalWrite(motor1_input2, HIGH);
- analogWrite(motor1_enable, 128); // 128 for half speed
- // Move Motor 2 backward at half speed
- digitalWrite(motor2_input1, LOW);
- digitalWrite(motor2_input2, HIGH);
- analogWrite(motor2_enable, 128); // 128 for half speed
- delay(2000); // Run the motors for 2 seconds
- // Stop both motors
- digitalWrite(motor1_enable, LOW);
- digitalWrite(motor2_enable, LOW);
- delay(1000); // Wait for 1 second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement