ASK AN EDUCATOR! “How can I control the speed of a motor with Arduino?”

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.print(“Current draw: “);
Serial.print(” “);


//stop the motors
Serial.println(“Braking motors…..”);
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);


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.

Make a robot friend with Adafruit’s CRICKIT – A Creative Robotics & Interactive Construction Kit. It’s an add-on to our popular Circuit Playground Express, FEATHER and other platforms to make and program robots with CircuitPython, MakeCode, and Arduino. Start controlling motors, servos, solenoids. You also get signal pins, capacitive touch sensors, a NeoPixel driver and amplified speaker output. It complements & extends your boards so you can still use all the goodies on the microcontroller, now you have a robotics playground as well.

Join 7,500+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython in 2018 – Python on Microcontrollers is here!

Have an amazing project to share? Join the SHOW-AND-TELL every Wednesday night at 7:30pm ET on Google+ Hangouts.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Follow Adafruit on Instagram for top secret new products, behinds the scenes and more https://www.instagram.com/adafruit/

Maker Business — Fewer startups, and other collateral damage from the 2018 tariffs

Wearables — Light as a Worbla feather

Electronics — How to make your own magnetic field probe!

Biohacking — The State of DNA Analysis in Three Mindmaps

Python for Microcontrollers — One year of CircuitPython weeklies!

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !

No Comments

No comments yet.

Sorry, the comment form is closed at this time.