Momentary Switch - Adafruit Motor Shield V2 for Arduino

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
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

Hi, new when it comes to running steppers - Recently went through the "How To" on the Adafruit Motor Shield V2 for Arduino and have my stepper motor working. I just had a quick question:

How would I correctly wire a momentary (on/off/on) switch into this setup to move the motor on demand? Will there need to be any changes made to any coding

Here is this switch I was looking at: https://www.gamainc.com/product/28pr-mo ... e=standard

Any help is appreciated!!

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

That switch looks like it was designed for changing the direction of a DC or Universal motor by switching power to the motor terminals. It won't work that way with a stepper motor, but you could still connect your switch to the Arduino and change the stepper motor direction via software.

Lesson 6 shows how to connect switches to the Arduino and write code to read the state of the switch: https://learn.adafruit.com/adafruit-ard ... s/overview

User avatar
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

adafruit_support_bill wrote:Lesson 6 shows how to connect switches to the Arduino and write code to read the state of the switch: https://learn.adafruit.com/adafruit-ard ... s/overview
Thanks Bill! I reviewed the lesson and had another question since I am not using a breadboard. Since I am using the Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit - v2.3 stacked on my Arduino Uno - will the switch wires be soldered to the bottom on the arduino board pins or on top of the motor shield? My guess is to the bottom of the Uno to allow for the shield stacking but just wanted to confirm.

Thanks!

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

The easiest way is to connect to the breakout holes on the shield. There is a labeled hole next to each header pin. These are connected directly to the Arduino pins.

User avatar
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

adafruit_support_bill wrote:The easiest way is to connect to the breakout holes on the shield. There is a labeled hole next to each header pin. These are connected directly to the Arduino pins.
Thanks Bill. I also noticed the use of resistors in the Adafruit lesson on the breadboard...Will the Adafruit motor shield still require the use of the resistors attached to the switch in order to ground each pole when the other is in use or is the board configured in a way to reduce that resistor piece. I just don't the board to be fried when using the switch.

Currently using GND and Pin 6 and Pin 7 to control motor function.

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

The Arduino has internal pullup resistors that eliminate the need for those external resistors. You can enable them using the PinMode() command : https://www.arduino.cc/en/Reference/PinMode

User avatar
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

@adafruit_support_bill thank you for the help! It looks like my wiring may not be correct. When I flip the switch in forward or reverse it will just turn off the board. I attached a quick photo - no video allowed - to try and show wiring /what is happening. Any help from the forums is appreciated.

Switch is a 3 post switch -
Center post - 5v
Left post - GND and pin 6
Right post - GND and pin 7

Using INPUT_PULLUP to eliminate the need of the resistors as in the previous post.
Attachments
image1.jpg
image1.jpg (876.41 KiB) Viewed 792 times

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

Please post the code you are using.

Also post either more photos or a wiring diagram. I see several wires connected to the shield, but I can't see what they are connected to.

User avatar
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

@adafruit_support_bill thank you very much for the help! Code and schematic are included.

Code:

Code: Select all

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

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution for the motor
const int motorSpeed = 20;

Adafruit_StepperMotor *motor = AFMS.getStepper(stepsPerRevolution, 2);

#define forwardPin 6
#define backwardPin 7

int steps = 3;

void setup() {
  Serial.begin(9600);   // initialize the serial port:
  AFMS.begin();

  motor->setSpeed(motorSpeed);

  pinMode(forwardPin, INPUT_PULLUP);
  pinMode(backwardPin, INPUT_PULLUP);
}

void loop() {
  int forward = digitalRead(forwardPin);
  int backward = digitalRead(backwardPin);

  if (forward == HIGH && backward == LOW) {
    motor->step(steps, FORWARD, SINGLE);

    return;
  }
  else if (backward == HIGH && forward == LOW) {
    motor->step(steps, BACKWARD, SINGLE);

    return;
  }
  else {
    Serial.println("No Reading");
    
    return;
  }

  return;
}
Attachments
Screen Shot 2017-08-29 at 6.47.55 PM.png
Screen Shot 2017-08-29 at 6.47.55 PM.png (49.52 KiB) Viewed 786 times
Last edited by adafruit_support_bill on Tue Aug 29, 2017 8:14 pm, edited 1 time in total.
Reason: please use the [code] button when uploading code to the forums.

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

You have internal pullup resistors enabled, but external pulldown resistors which effectively neutralize them. Your code appears to be written for pullup resistors. You need to eliminate the pulldowns.

User avatar
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

@adafruit_support_bill sounds good! When I did the physical wiring of the prototype I did not include the pulldown resistors and the issue was still presented. I apologize for the confusion on the schematic.

My next test is going to be to remove the ground that is currently going to each pin and see what happens. Thanks for all the help!

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

With the pulldown resistors removed, I would expect it should work with that code.
What are the other wires in the diagram? I see wires from pins 8, 9, 10 & 11.

User avatar
wolffman007
 
Posts: 13
Joined: Wed Jul 27, 2016 3:15 pm

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by wolffman007 »

@adafruit_support_bill the other wires were just for how I would have hooked up with stepper motor prior to using the Adafruit shield.

SOLUTION: In order to use the code I created with a SPDT (3 Post) switch directly to the Adafruit Shield there are two options but only one that truly works to stop and start the motor with no lag:
1: Using INPUT_PULLUP - Center post to GND. Left and Right post to the defined pin. In my case pin 6 (forward) and pin 7 (reverse). Motor will start / stop when the switch is engaged / disengaged.

Thanks again for all the assistance. The internal pullup resistors seem to really do the trick!

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

Re: Momentary Switch - Adafruit Motor Shield V2 for Arduino

Post by adafruit_support_bill »

Good to hear that is working for you. Thanks for the follow-up.

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

Return to “Arduino Shields from Adafruit”