Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example of using the PWM interface with an Arduino.
- This code will make two motors go full speed forward, stop, full speed reverse, and stop, each for one second.
- It uses digitalWrite to control the direction pins, which determine the direction of the motors. It uses analogWrite to control the speed of the motors. In the Arduino language, analogWrite values range from 0 (always off) to 255 (always on).
- Please adapt the pin numbers and rest of the code according to your exact setup and requirements.
- Also, remember to connect the GND of the driver to the GND of the Arduino, and to supply the driver with an appropriate power source.
- */
- const int M1DIR = 8; // Motor 1 Direction
- const int M1PWM = 9; // Motor 1 PWM
- const int M2DIR = 12; // Motor 2 Direction
- const int M2PWM = 11; // Motor 2 PWM
- void setup() {
- pinMode(M1DIR, OUTPUT);
- pinMode(M1PWM, OUTPUT);
- pinMode(M2DIR, OUTPUT);
- pinMode(M2PWM, OUTPUT);
- }
- void loop() {
- // Full speed forward
- digitalWrite(M1DIR, HIGH);
- analogWrite(M1PWM, 255);
- digitalWrite(M2DIR, HIGH);
- analogWrite(M2PWM, 255);
- delay(1000);
- // Stop
- analogWrite(M1PWM, 0);
- analogWrite(M2PWM, 0);
- delay(1000);
- // Full speed reverse
- digitalWrite(M1DIR, LOW);
- analogWrite(M1PWM, 255);
- digitalWrite(M2DIR, LOW);
- analogWrite(M2PWM, 255);
- delay(1000);
- // Stop
- analogWrite(M1PWM, 0);
- analogWrite(M2PWM, 0);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement