Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The Pololu G2 High-Power Motor Driver is available in several variants, but the basic control logic is similar for all. Here's an example code to control a Pololu G2 High-Power Motor Driver (18v25, 24v13, 18v17, or 24v21) using an Arduino:
- This code sets the motor direction to forward, ramps up the motor speed from 0 to maximum, and then ramps down the speed to 0. It then reverses the motor direction and repeats the same process.
- Connect the Pololu G2 High-Power Motor Driver to the Arduino as follows:
- PWM pin to digital pin 9 on the Arduino
- DIR pin to digital pin 8 on the Arduino
- Additionally, make the following connections:
- Connect the motor driver's GND pin to the GND pin on the Arduino
- Connect the motor driver's VIN and GND pins to an appropriate power supply for your motor
- Connect the motor to the motor driver's OUTA and OUTB pins
- Please note that you may need to adjust the pin numbers in the code depending on the specific pins you choose to use for connecting the motor driver to your Arduino.
- */
- #include <Arduino.h>
- const int pwmPin = 9; // Connect this pin to the PWM pin of the motor driver
- const int dirPin = 8; // Connect this pin to the DIR pin of the motor driver
- void setup() {
- pinMode(pwmPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- }
- void loop() {
- // Set motor direction to forward
- digitalWrite(dirPin, HIGH);
- // Ramp up the motor speed
- for (int speed = 0; speed <= 255; speed++) {
- analogWrite(pwmPin, speed);
- delay(10);
- }
- // Ramp down the motor speed
- for (int speed = 255; speed >= 0; speed--) {
- analogWrite(pwmPin, speed);
- delay(10);
- }
- // Set motor direction to reverse
- digitalWrite(dirPin, LOW);
- // Ramp up the motor speed
- for (int speed = 0; speed <= 255; speed++) {
- analogWrite(pwmPin, speed);
- delay(10);
- }
- // Ramp down the motor speed
- for (int speed = 255; speed >= 0; speed--) {
- analogWrite(pwmPin, speed);
- delay(10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement