Advertisement
microrobotics

L298M Mini H-Bridge dual motor driver

Apr 5th, 2023 (edited)
4,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Define the pins connected to the L298N motor driver
  2. const int motorA1 = 2; // A-1
  3. const int motorA2 = 3; // A-2
  4. const int motorB1 = 4; // B-1
  5. const int motorB2 = 5; // B-2
  6.  
  7. void setup() {
  8.   // Define the motor control pins as OUTPUT
  9.   pinMode(motorA1, OUTPUT);
  10.   pinMode(motorA2, OUTPUT);
  11.   pinMode(motorB1, OUTPUT);
  12.   pinMode(motorB2, OUTPUT);
  13. }
  14.  
  15. void loop() {
  16.   // Move motor A forward
  17.   digitalWrite(motorA1, HIGH);
  18.   digitalWrite(motorA2, LOW);
  19.  
  20.   // Move motor B forward
  21.   digitalWrite(motorB1, HIGH);
  22.   digitalWrite(motorB2, LOW);
  23.  
  24.   // Delay for some time (motor runs forward)
  25.   delay(2000);
  26.  
  27.   // Stop both motors
  28.   digitalWrite(motorA1, LOW);
  29.   digitalWrite(motorA2, LOW);
  30.   digitalWrite(motorB1, LOW);
  31.   digitalWrite(motorB2, LOW);
  32.  
  33.   // Delay for some time (motors are stopped)
  34.   delay(1000);
  35.  
  36.   // Move motor A backward
  37.   digitalWrite(motorA1, LOW);
  38.   digitalWrite(motorA2, HIGH);
  39.  
  40.   // Move motor B backward
  41.   digitalWrite(motorB1, LOW);
  42.   digitalWrite(motorB2, HIGH);
  43.  
  44.   // Delay for some time (motors run backward)
  45.   delay(2000);
  46.  
  47.   // Stop both motors
  48.   digitalWrite(motorA1, LOW);
  49.   digitalWrite(motorA2, LOW);
  50.   digitalWrite(motorB1, LOW);
  51.   digitalWrite(motorB2, LOW);
  52.  
  53.   // Delay for some time (motors are stopped)
  54.   delay(1000);
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement