ASK AN EDUCATOR! “How can I control the speed of a motor with Arduino?”
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;
“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.
We are angry, frustrated, and in pain because of the violence and murder of Black people by the police because of racism. We are in the fight AGAINST RACISM. George Floyd was murdered, his life stolen. The Adafruit teams have specific actions we’ve done, are doing, and will do together as a company and culture. We are asking the Adafruit community to get involved and share what you are doing. The Adafruit teams will not settle for a hash tag, a Tweet, or an icon change. We will work on real change, and that requires real action and real work together. That is what we will do each day, each month, each year – we will hold ourselves accountable and publish our collective efforts, partnerships, activism, donations, openly and publicly. Our blog and social media platforms will be utilized in actionable ways. Join us and the anti-racist efforts working to end police brutality, reform the criminal justice system, and dismantle the many other forms of systemic racism at work in this country, read more @ adafruit.com/blacklivesmatter
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.