Advertisement
microrobotics

MX1919-DIP

Apr 5th, 2023
5,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's an example code for controlling a DC motor using the MX1919 driver and an Arduino:
  3.  
  4. This code will rotate both DC motors clockwise at full speed for 2 seconds, stop for 1 second, rotate counterclockwise at half speed for 2 seconds, and stop for 1 second. The cycle will then repeat.
  5.  
  6. Connect the MX1919-DIP's IN1 and IN2 inputs for Motor 1 to the Arduino's digital pins 3 and 2, respectively, and connect the IN3 and IN4 inputs for Motor 2 to the Arduino's digital pins 5 and 4, respectively. Make sure to provide an appropriate power supply to the driver's motor voltage and ground pins, and connect the motors to the driver's output terminals (Motor 1 to OUT1 and OUT2, Motor 2 to OUT3 and OUT4).
  7. */
  8.  
  9. // Motor 1
  10. const int pwmPin1 = 3;   // PWM pin connected to the driver's IN1
  11. const int dirPin1 = 2;   // Direction pin connected to the driver's IN2
  12.  
  13. // Motor 2
  14. const int pwmPin2 = 5;   // PWM pin connected to the driver's IN3
  15. const int dirPin2 = 4;   // Direction pin connected to the driver's IN4
  16.  
  17. void setup() {
  18.   pinMode(pwmPin1, OUTPUT);
  19.   pinMode(dirPin1, OUTPUT);
  20.   pinMode(pwmPin2, OUTPUT);
  21.   pinMode(dirPin2, OUTPUT);
  22. }
  23.  
  24. void loop() {
  25.   // Rotate both motors clockwise at full speed
  26.   digitalWrite(dirPin1, HIGH);
  27.   digitalWrite(dirPin2, HIGH);
  28.   analogWrite(pwmPin1, 255); // 255 is the maximum value for analogWrite (full speed)
  29.   analogWrite(pwmPin2, 255); // 255 is the maximum value for analogWrite (full speed)
  30.   delay(2000); // Run the motors for 2 seconds
  31.  
  32.   // Stop both motors for 1 second
  33.   analogWrite(pwmPin1, 0);
  34.   analogWrite(pwmPin2, 0);
  35.   delay(1000);
  36.  
  37.   // Rotate both motors counterclockwise at half speed
  38.   digitalWrite(dirPin1, LOW);
  39.   digitalWrite(dirPin2, LOW);
  40.   analogWrite(pwmPin1, 127); // 127 is half of the maximum value for analogWrite (half speed)
  41.   analogWrite(pwmPin2, 127); // 127 is half of the maximum value for analogWrite (half speed)
  42.   delay(2000); // Run the motors for 2 seconds
  43.  
  44.   // Stop both motors for 1 second
  45.   analogWrite(pwmPin1, 0);
  46.   analogWrite(pwmPin2, 0);
  47.   delay(1000);
  48. }
  49.  
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement