The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

Motor shield V2 with UNO. Speed control and direction

by stevex on Sun Oct 27, 2013 1:42 am

Hello-

I'm a beginner and would like to know how to run 2-4 DC motors with potentiometer speed control and a forward-off-reverse switch for each motor.

thanks for your help.

-steve
stevex
 
Posts: 14
Joined: Wed Oct 16, 2013 11:18 pm

Re: Motor shield V2 with UNO. Speed control and direction

by adafruit_support_rick on Sun Oct 27, 2013 5:03 am

Please have a look at our Motor Shield tutorial to learn how to control motors:
http://learn.adafruit.com/adafruit-moto ... or-arduino

For speed control via a potentiometer, you would connect the pot to an analog input on your Uno. Using the analogRead function, your software will read the pot and then adjust the motor speed accordingly.
User avatar
adafruit_support_rick
 
Posts: 10787
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Motor shield V2 with UNO. Speed control and direction

by stevex on Wed Oct 30, 2013 1:14 am

I'm still a bit lost. Can someone post some code using a potentiometer for speed control rather than a set speed?
stevex
 
Posts: 14
Joined: Wed Oct 16, 2013 11:18 pm

Re: Motor shield V2 with UNO. Speed control and direction

by adafruit_support_mike on Wed Oct 30, 2013 2:03 am

The function `analogRead()` returns a value between 0 and 1023. The library function `motor->setSpeed()` takes a number between 0 and 255. Putting them together would look something like this:

Code: Select all | TOGGLE FULL SIZE
    potPin = A0;
    motor->setSpeed( analogRead( potPin ) / 4 );
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8586
Joined: Thu Feb 11, 2010 1:51 pm

Re: Motor shield V2 with UNO. Speed control and direction

by stevex on Wed Oct 30, 2013 9:56 pm

Thanks. Somehow I got it to work with the below sketch. I still feel like I don't know what I'm doing...just copying an pasting and trying different sketches...

This works tho...

Now I'd like to control direction with a switch (on-off-on). I can do this within the Arduino...? or should I wire this up outside the Arduino?

Probably a dumb question, but this is all new to me....

-steve

---------------------------------------------------
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

int potPin = A0;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
 
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
}

void loop() {
  uint8_t i;
 

  myMotor->run(FORWARD);
  myMotor->setSpeed(analogRead(potPin)/4);
  }
Last edited by adafruit_support_mike on Thu Oct 31, 2013 1:03 am, edited 1 time in total.
Reason: Please use CODE tags when posting code
stevex
 
Posts: 14
Joined: Wed Oct 16, 2013 11:18 pm

Re: Motor shield V2 with UNO. Speed control and direction

by adafruit_support_mike on Thu Oct 31, 2013 1:09 am

Don't worry about copying without truly understanding the code. We all do that when we're learning new tech. The first challenge is getting a system to work in a configuration someone else says /should/ work, then you poke at it to see how it reacts. Eventually things start to look familiar, and you start gaining a deeper understanding of the system.

Copy without shame, make mistakes early and often.. just don't make the same ones over and over again.

To deal with buttons, start with this tutorial to learn how to make buttons work with an Arduino: http://learn.adafruit.com/adafruit-ardu ... s/overview

Once you're comfortable with that, you can generalize the problem of turning an LED on and off to the problem of making a motor go one way or the other.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 8586
Joined: Thu Feb 11, 2010 1:51 pm