DC motor control with three buttons

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
randolphmail
 
Posts: 7
Joined: Mon Dec 06, 2021 12:07 am

DC motor control with three buttons

Post by randolphmail »

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

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

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

Re: DC motor control with three buttons

Post by randolphmail »

Photos of project
IMG_1997.JPG
IMG_1997.JPG (753.7 KiB) Viewed 415 times
IMG_1997.JPG
IMG_1997.JPG (753.7 KiB) Viewed 415 times
Attachments
IMG_1996.JPG
IMG_1996.JPG (960.33 KiB) Viewed 415 times


User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with three buttons

Post by adafruit_support_bill »

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

User avatar
rpiloverbd
 
Posts: 198
Joined: Mon Nov 29, 2021 8:13 am

Re: DC motor control with three buttons

Post by rpiloverbd »

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?

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

Re: DC motor control with three buttons

Post by randolphmail »

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 389 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with three buttons

Post by adafruit_support_bill »

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

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

Re: DC motor control with three buttons

Post by randolphmail »

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

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

Re: DC motor control with three buttons

Post by randolphmail »

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.

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

Re: DC motor control with three buttons

Post by randolphmail »

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

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC motor control with three buttons

Post by adafruit_support_bill »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”