Advertisement
microrobotics

L293D Dual Motor Driver IC, 1.2A Peak - DIP16 THT

Apr 5th, 2023
1,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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:
  3.  
  4. 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.
  5. */
  6.  
  7.  
  8. // Include the Arduino library
  9. #include <Arduino.h>
  10.  
  11. // Pin connections to L293D
  12. const int motor1_enable = 3; // Enable pin for Motor 1
  13. const int motor1_input1 = 2; // Input 1 pin for Motor 1
  14. const int motor1_input2 = 4; // Input 2 pin for Motor 1
  15.  
  16. const int motor2_enable = 5; // Enable pin for Motor 2
  17. const int motor2_input1 = 6; // Input 1 pin for Motor 2
  18. const int motor2_input2 = 7; // Input 2 pin for Motor 2
  19.  
  20. void setup() {
  21.   // Set the motor control pins as outputs
  22.   pinMode(motor1_enable, OUTPUT);
  23.   pinMode(motor1_input1, OUTPUT);
  24.   pinMode(motor1_input2, OUTPUT);
  25.  
  26.   pinMode(motor2_enable, OUTPUT);
  27.   pinMode(motor2_input1, OUTPUT);
  28.   pinMode(motor2_input2, OUTPUT);
  29. }
  30.  
  31. void loop() {
  32.   // Move Motor 1 forward at full speed
  33.   digitalWrite(motor1_input1, HIGH);
  34.   digitalWrite(motor1_input2, LOW);
  35.   analogWrite(motor1_enable, 255); // 255 for full speed, can be adjusted from 0 to 255
  36.  
  37.   // Move Motor 2 forward at full speed
  38.   digitalWrite(motor2_input1, HIGH);
  39.   digitalWrite(motor2_input2, LOW);
  40.   analogWrite(motor2_enable, 255); // 255 for full speed, can be adjusted from 0 to 255
  41.  
  42.   delay(2000); // Run the motors for 2 seconds
  43.  
  44.   // Stop both motors
  45.   digitalWrite(motor1_enable, LOW);
  46.   digitalWrite(motor2_enable, LOW);
  47.  
  48.   delay(1000); // Wait for 1 second
  49.  
  50.   // Move Motor 1 backward at half speed
  51.   digitalWrite(motor1_input1, LOW);
  52.   digitalWrite(motor1_input2, HIGH);
  53.   analogWrite(motor1_enable, 128); // 128 for half speed
  54.  
  55.   // Move Motor 2 backward at half speed
  56.   digitalWrite(motor2_input1, LOW);
  57.   digitalWrite(motor2_input2, HIGH);
  58.   analogWrite(motor2_enable, 128); // 128 for half speed
  59.  
  60.   delay(2000); // Run the motors for 2 seconds
  61.  
  62.   // Stop both motors
  63.   digitalWrite(motor1_enable, LOW);
  64.   digitalWrite(motor2_enable, LOW);
  65.  
  66.   delay(1000); // Wait for 1 second
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement