WheelDrive Arduino Sketch¶
This sketch is used by Exercise: DRV8833 Dual DC Motor Driver.
Full Source Code¶
The full code is all in one file WheelDrive.ino.
1// WheelDrive - move a pair of DC motors at varying rate and direction
2//
3// Copyright (c) 2016, Garth Zeglin. All rights reserved. Licensed under the
4// terms of the BSD 3-clause license as included in LICENSE.
5//
6// This program assumes that:
7//
8// 1. A DRV8833 dual DC motor driver module is connected to pins 5, 6, 9, and 10.
9// 2. A pair of motors is attached to the driver.
10// 3. The serial console on the Arduino IDE is set to 9600 baud communications speed.
11
12// ================================================================================
13// Define constant values and global variables.
14
15// Define the pin numbers on which the outputs are generated.
16#define MOT_A1_PIN 5
17#define MOT_A2_PIN 6
18#define MOT_B1_PIN 10
19#define MOT_B2_PIN 9
20
21// ================================================================================
22/// Configure the hardware once after booting up. This runs once after pressing
23//// reset or powering up the board.
24void setup(void)
25{
26 // Initialize the stepper driver control pins to output drive mode.
27 pinMode(MOT_A1_PIN, OUTPUT);
28 pinMode(MOT_A2_PIN, OUTPUT);
29 pinMode(MOT_B1_PIN, OUTPUT);
30 pinMode(MOT_B2_PIN, OUTPUT);
31
32 // Start with drivers off, motors coasting.
33 digitalWrite(MOT_A1_PIN, LOW);
34 digitalWrite(MOT_A2_PIN, LOW);
35 digitalWrite(MOT_B1_PIN, LOW);
36 digitalWrite(MOT_B2_PIN, LOW);
37
38 // Initialize the serial UART at 9600 bits per second.
39 Serial.begin(9600);
40}
41
42// ================================================================================
43/// Set the current on a motor channel using PWM and directional logic.
44/// Changing the current will affect the motor speed, but please note this is
45/// not a calibrated speed control. This function will configure the pin output
46/// state and return.
47///
48/// \param pwm PWM duty cycle ranging from -255 full reverse to 255 full forward
49/// \param IN1_PIN pin number xIN1 for the given channel
50/// \param IN2_PIN pin number xIN2 for the given channel
51
52void set_motor_pwm(int pwm, int IN1_PIN, int IN2_PIN)
53{
54 if (pwm < 0) { // reverse speeds
55 analogWrite(IN1_PIN, -pwm);
56 digitalWrite(IN2_PIN, LOW);
57
58 } else { // stop or forward
59 digitalWrite(IN1_PIN, LOW);
60 analogWrite(IN2_PIN, pwm);
61 }
62}
63// ================================================================================
64/// Set the current on both motors.
65///
66/// \param pwm_A motor A PWM, -255 to 255
67/// \param pwm_B motor B PWM, -255 to 255
68
69void set_motor_currents(int pwm_A, int pwm_B)
70{
71 set_motor_pwm(pwm_A, MOT_A1_PIN, MOT_A2_PIN);
72 set_motor_pwm(pwm_B, MOT_B1_PIN, MOT_B2_PIN);
73
74 // Print a status message to the console.
75 Serial.print("Set motor A PWM = ");
76 Serial.print(pwm_A);
77 Serial.print(" motor B PWM = ");
78 Serial.println(pwm_B);
79}
80
81// ================================================================================
82/// Simple primitive for the motion sequence to set a speed and wait for an interval.
83///
84/// \param pwm_A motor A PWM, -255 to 255
85/// \param pwm_B motor B PWM, -255 to 255
86/// \param duration delay in milliseconds
87void spin_and_wait(int pwm_A, int pwm_B, int duration)
88{
89 set_motor_currents(pwm_A, pwm_B);
90 delay(duration);
91}
92
93// ================================================================================
94/// Run one iteration of the main event loop. The Arduino system will call this
95/// function over and over forever.
96void loop(void)
97{
98 // Generate a fixed motion sequence to demonstrate the motor modes.
99
100 // Ramp speed up.
101 for (int i = 0; i < 11; i++) {
102 spin_and_wait(25*i, 25*i, 500);
103 }
104 // Full speed forward.
105 spin_and_wait(255,255,2000);
106
107 // Ramp speed into full reverse.
108 for (int i = 0; i < 21 ; i++) {
109 spin_and_wait(255 - 25*i, 255 - 25*i, 500);
110 }
111
112 // Full speed reverse.
113 spin_and_wait(-255,-255,2000);
114
115 // Stop.
116 spin_and_wait(0,0,2000);
117
118 // Full speed, forward, turn, reverse, and turn for a two-wheeled base.
119 spin_and_wait(255, 255, 2000);
120 spin_and_wait(0, 0, 1000);
121 spin_and_wait(-255, 255, 2000);
122 spin_and_wait(0, 0, 1000);
123 spin_and_wait(-255, -255, 2000);
124 spin_and_wait(0, 0, 1000);
125 spin_and_wait(255, -255, 2000);
126 spin_and_wait(0, 0, 1000);
127
128 // and repeat
129}
130/****************************************************************/