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.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.


Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

Join Adafruit on Mastodon

Adafruit is on Mastodon, join in! adafruit.com/mastodon

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7pm ET! To join, head over to YouTube and check out the show’s live chat – we’ll post the link there.

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

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

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org


Maker Business — “Packaging” chips in the US

Wearables — Enclosures help fight body humidity in costumes

Electronics — Transformers: More than meets the eye!

Python for Microcontrollers — Python on Microcontrollers Newsletter: Silicon Labs introduces CircuitPython support, and more! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

Adafruit IoT Monthly — Guardian Robot, Weather-wise Umbrella Stand, and more!

Microsoft MakeCode — MakeCode Thank You!

EYE on NPI — Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — #NewProds 7/19/23 Feat. Adafruit Matrix Portal S3 CircuitPython Powered Internet Display!

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.