Hal asks:
I need a sketch for arduino duo motor shield to control the speed of a dc motor.
I can turn the motor on and off but need for code for speed control. Is htis just a loop that sets the value of digitalWrite(motor1Pin, value); if so what is the numeric range of the value and how does it work?
You asked this question at the right time! I just finished a simple DC motor controller for a quadcopter I am making to show my students 🙂 (I know……everyone is making quadcopters)
Controlling the speed of a motor is a very simple process with Arduino thanks to a built in function and technique called pulse-width modulation or PWM. Since you are using an Uno, you actually have the ability to independantly vary the speed of 6 DC motors (on pins 3, 5, 6, 9, 10 and 11). The PWM is controller by the analogWrite(pin, value) command which when executed will pulse anywhere between 0 and 255 duty cycle at 490Hz (this is why motors that are being PWM’d make noise at low duty cycles) The lower the number the slower the motors rotation and vice-versa. One of the cool things about the PWM command is that it actually runs in the “background” of your sketch. Once you execute the command, it will continue at that duty-cycle until you change it. This makes it great for things like quadcopters!
In order to drive anything much more then an LED, you need an external piece of hardware to supply more current….hence your motor shield!
The Arduino Motor Shield provides you with a dual full-bridge driver which can deliver 2A per channel to 2 DC motors or 1 stepper motor. The channels are connected to D3 and D11, direction pins to D12 & D13, brake on D9 and D8 and current sensing on A0 and A1, making this a pretty sweet shield. I do not have the shield, so I apologize if this isn’t 100% kosher, but here is a sketch that should sweep your motor through its complete range of speed control:
//set our constants
const int dirA = 12;
const int dirB = 13;
const int brakeA = 9;
const int brakeB = 8;
const int senseA = 0;
const int senseB = 0;
const int pwmA = 3;
const int pwmB = 11;
void setup() {
Serial.begin(9600); //initialize our debug baudrate
//set our outputs
pinMode(dirA, OUTPUT);
pinMode(dirB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
//set direction
digitalWrite(dirA, LOW);
digitalWrite(dirB, LOW);
}
void loop() {
for (int i = 0; i < 256; i++) {
analogWrite(pwmA, i);
analogWrite(pwmB, i);
Serial.print(“Duty-cycle: “);
Serial.println(i);
Serial.print(“Current draw: “);
Serial.print(analogRead(senseA));
Serial.print(” “);
Serial.println(analogRead(senseB));
delay(500);
}
//stop the motors
Serial.println(“Braking motors…..”);
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
delay(2000);
Serial.println(“Done.”);
}
I hope this answered your question and good luck with your motors!
Next up is Chris with a question about teaching in areas that you are not considered an expert in!
Don’t forget, everyone is invited to ask a question!
Click here!
“Ask an Educator” questions are answered by Adam Kemp, a high school teacher who has been teaching courses in Energy Systems, Systems Engineering, Robotics and Prototyping since 2005.