Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

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.


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.

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

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!

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

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


Maker Business — How 3M is able to ramp up production of N95 masks

Wearables — Marker, meet flower

Electronics — Footprint Fail!

Python for Microcontrollers — The Python on Microcontrollers Newsletter: Adafruit CLUE, Nina and Thea, and much more! #Python #Adafruit #CircuitPython @circuitpython @micropython @ThePSF

Adafruit IoT Monthly — Repurposed Smart Home Displays, Open-Source 5G Networks, and more!

Microsoft MakeCode — Black Girls Code Virtual Camps

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

New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — New Products 7/1/20 featuring Adafruit H3LIS331 Ultra High Range Triple-Axis Accelerometer – STEMMA QT / Qwiic!

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.