Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

DC motor control with three buttons
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 12:20 am

I've been trying to work on this for a couple of weeks, and can't find the correct answer.

I've built a turntable, and am using an Arduino with Adafruit motor shield to drive a DC motor. I have the sketch set up so it will run low (33rpm) if pin 4 is used. high (45rpm) if pin 8 is used, and off (no rpm) if pin 13 is used.

If I touch the respective pins, each speed will engage, but if I connect a wire to each one, to three different momentary switches, it acts as if all buttons are being pushed, even though they are not. Tha same thing will happen even if the wires are not connected to anything. I have 10k resistors in line with each of the three wires.

My sketch is here:

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

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *myMotor = AFMS.getMotor(2);

int low = 4;
int high = 8;
int off = 13;



void setup() {
  // put your setup code here, to run once:
AFMS.begin();



 
}

void loop() {
  // put your main code here, to run repeatedly:
 
if(digitalRead(low) == HIGH)
{

myMotor->setSpeed(135); //0-255 - experiment with this number till you get 33.3 rpm on platter using stribe disc

myMotor->run(FORWARD);
  }
 

  // for 45 speed

  if(digitalRead(high) == HIGH)
{

myMotor->setSpeed(179); //0-255 - experiment with this number till you get 33.3 rpm on platter using stribe disc

myMotor->run(FORWARD);
  }
 

  // for stop

  if(digitalRead(off) == HIGH)
{

myMotor->setSpeed(0); //0-255

myMotor->run(RELEASE);
  }
 
}
Last edited by adafruit_support_bill on Mon Dec 06, 2021 7:03 am, edited 3 times in total.
Reason: Please use [code] tags when posting code to the forums.

randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 12:35 am

Photos of project

IMG_1997.JPG
IMG_1997.JPG (753.7 KiB) Viewed 264 times


IMG_1997.JPG
IMG_1997.JPG (753.7 KiB) Viewed 264 times
Attachments
IMG_1996.JPG
IMG_1996.JPG (960.33 KiB) Viewed 264 times

randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 12:36 am


randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by adafruit_support_bill on Mon Dec 06, 2021 6:20 am

Please post a wiring diagram showing how the switches are wired.

adafruit_support_bill
 
Posts: 82833
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with three buttons

by rpiloverbd on Mon Dec 06, 2021 6:41 am

Normally in the setup function, we declare the pinmodes. But in your code, I cannot see that declaration. Is it already done inside the AFMS.begin() function?

rpiloverbd
 
Posts: 90
Joined: Mon Nov 29, 2021 8:13 am

Re: DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 9:36 am

adafruit_support_bill wrote:Please post a wiring diagram showing how the switches are wired.


TinkCad doesn't have a motor shield graphic, but this is how it would be wired to the shield. Also, my momentary switches only have two terminals. Pins to one, ground to the other.
Attachments
Screen Shot 2021-12-06 at 8.31.56 AM.png
Screen Shot 2021-12-06 at 8.31.56 AM.png (46.34 KiB) Viewed 238 times

randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by adafruit_support_bill on Mon Dec 06, 2021 9:59 am

That is not going to work as is since all 3 pins are 'floating' until their switch is closed, so the input state will be unpredictable. It is not uncommon for a floating pin to be affected by changes to adjacent pins.

What you want to do is enable the internal pullup resistors with the pinMode command. This will assure that the pin state will be HIGH by default. It will be pulled LOW when the switch completes the circuit to GND.

Then you will need to change the logic in your code to be 'active LOW'.
https://learn.adafruit.com/adafruit-ard ... duino-code

The series resistors serve no purpose and can be removed.
https://learn.adafruit.com/adafruit-ard ... ard-layout

adafruit_support_bill
 
Posts: 82833
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 10:57 am

Thank you. I will try that when I get home tonight.

randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 11:36 pm

adafruit_support_bill wrote:That is not going to work as is since all 3 pins are 'floating' until their switch is closed, so the input state will be unpredictable. It is not uncommon for a floating pin to be affected by changes to adjacent pins.

What you want to do is enable the internal pullup resistors with the pinMode command. This will assure that the pin state will be HIGH by default. It will be pulled LOW when the switch completes the circuit to GND.

Then you will need to change the logic in your code to be 'active LOW'.
https://learn.adafruit.com/adafruit-ard ... duino-code

The series resistors serve no purpose and can be removed.
https://learn.adafruit.com/adafruit-ard ... ard-layout



Awesome! That did the trick. Now I can sleep at night.

I'm posting the corrected code in case someone else in the future is interested.

====

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *myMotor = AFMS.getMotor(2);

int low = 4;
int high = 8;
int off = 13;



void setup() {
// put your setup code here, to run once:
pinMode(low, INPUT_PULLUP);
pinMode(high, INPUT_PULLUP);
pinMode(off, INPUT_PULLUP);

AFMS.begin();


}

void loop() {
// put your main code here, to run repeatedly:

if(digitalRead(low) == LOW)
{

myMotor->setSpeed(135); //0-255 - experiment with this number till you get 33.3 rpm on platter using stribe disc

myMotor->run(FORWARD);
}


// for 45 speed

if(digitalRead(high) == LOW)
{

myMotor->setSpeed(180); //0-255 - experiment with this number till you get 45 rpm on platter using stribe disc

myMotor->run(FORWARD);
}


// for stop

if(digitalRead(off) == LOW)
{

myMotor->setSpeed(0); //0-255

myMotor->run(RELEASE);
}

}
Last edited by randolphmail on Mon Dec 06, 2021 11:45 pm, edited 1 time in total.

randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by randolphmail on Mon Dec 06, 2021 11:41 pm

Turntable, motor and Control box.
Attachments
IMG_2006.JPG
IMG_2006.JPG (859.58 KiB) Viewed 220 times

randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

Re: DC motor control with three buttons

by adafruit_support_bill on Tue Dec 07, 2021 6:09 am

Nice looking build! Good to hear it is all working now for you.

adafruit_support_bill
 
Posts: 82833
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.