5 buttons 2 need polarity reversed

Hi,
Ive got an odd situation I cant work out.
I have 5 buttons all wired like this:

button 1,3,5 work fine but 2 and 4 need to have +ive and -ive reversed (compared with the other 3) to have them register high when pressed.

I have used the stateChangeDetection example and expanded that out for all 5 buttons but also running it button by button on the same pin is the same.
The problem is across both 2 uno's and a mega2560.

Is there something obvious I am missing or doing wrong?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What do you have to wire reversed, LED? Button?

Can you post a picture of your project so we can see your layout?

Thanks.. Tom.. :slight_smile:

here is the code

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won't change:
const int  s1 = 47;    // the pin that the pushbutton is attached to
const int  s2 = 48;
const int  s3 = 49;
const int  s4 = 50;
const int  s5 = 51;
const int  s6 = 12;
const int ledPin = 13;

int buttonPushCounter = 0;   // counter for the number of button presses
int s1s = 0;         // current state of the button
int s2s = 0;         // current state of the button
int s3s = 0;         // current state of the button
int s4s = 0;         // current state of the button
int s5s = 0;         // current state of the button
int s6s = 0;         // current state of the button
int lb1s = 0;     // previous state of the button
int lb2s = 0;     // previous state of the button
int lb3s = 0;     // previous state of the button
int lb4s = 0;     // previous state of the button
int lb5s = 0;     // previous state of the button
int lb6s = 0;     // previous state of the button









void setup() {
  // initialize the button pin as a input:
  pinMode(s1, INPUT);
  pinMode(s2, INPUT);
  pinMode(s3, INPUT);
  pinMode(s4, INPUT);
  pinMode(s5, INPUT);
//  pinMode(s6, INPUT_PULLUP);

  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  s1s = digitalRead(s1);
  s2s = digitalRead(s2);
  s3s = digitalRead(s3);
  s4s = digitalRead(s4);
  s5s = digitalRead(s5);
//  s6s = digitalRead(s6);  


  if (s1s != lb1s) {
    if (s1s == HIGH) {
      buttonPushCounter++;
      Serial.println("1 on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("1 off");
    }
    delay(50);
  }
  lb1s = s1s;

  
  if (s2s != lb2s) {
    if (s2s == HIGH) {
      buttonPushCounter++;
      Serial.println("2 on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("2 off");
    }
    delay(50);
  }
  lb2s = s2s;

    if (s3s != lb3s) {
    if (s3s == HIGH) {
      buttonPushCounter++;
      Serial.println("3 on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("3 off");
    }
    delay(50);
  }
  lb3s = s3s;

  if (s4s != lb4s) {
    if (s4s == HIGH) {
      buttonPushCounter++;
      Serial.println("4 on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("4 off");
    }
    delay(50);
  }
  lb4s = s4s;

  if (s5s != lb5s) {
    if (s5s == HIGH) {
      buttonPushCounter++;
      Serial.println("5 on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("5 off");
    }
    delay(50);
  }
  lb5s = s5s;
/*
  if (s6s != lb6s) {
    if (s6s == HIGH) {
      buttonPushCounter++;
      Serial.println("6 on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("6 off");
    }
    delay(50);
  }
  lb6s = s6s;

*/
  
    // turns on the LED every four button pushes by checking the modulo of the
    // button push counter. the modulo function gives you the remainder of the
    // division of two numbers:
    if (buttonPushCounter % 4 == 0) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  


}

The other code that the above is based off is this

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won't change:
const int  buttonPin = 7

;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

To get button number 2 and 4 to go HIGH when pressed I have to reverse the polarity on the power to those batteries. IE change them from +5v to the button to be GND to the button.
Once that is done when the button is pressed it goes HIGH.

All 5 buttons are exactly the same and in the code taken from the example menu of the software with only one digital pin used I can physically plug in button 1,3,5 and they all work wired from the image in my first post, with button 2,4 they are straight away ON according to serial monitor but they are all wired exactly the same way

imblunt:
To get button number 2 and 4 to go HIGH when pressed I have to reverse the polarity on the power to those batteries. IE change them from +5v to the button to be GND to the button.
Once that is done when the button is pressed it goes HIGH.

All 5 buttons are exactly the same and in the code taken from the example menu of the software with only one digital pin used I can physically plug in button 1,3,5 and they all work wired from the image in my first post, with button 2,4 they are straight away ON according to serial monitor but they are all wired exactly the same way

I don't understand this..
Are ALL your switches the same?
Can you post a link to data/spec or where you bought the switches please?
Are they ALL wired the same.
Have you swapped your switches around the so you can see if it is a switch problem or wiring?
Do you have a DMM to measure circuit voltages.
I think we need to see a picture of your project so we can, see how you have wired your project.
Thanks.. Tom.... :slight_smile:

I don't understand it either.

Yes all the switches are the same, removing the switch from the circuit and using a jumper cable has the same symptoms.
I don't have a spec sheet for the switches but they are a generic momentary push button switch available from jaycar.
I have rewired this circuit 3 times with new cabling and pullup resistors to try and eliminate where the problem is.
However every time that I do that the problem is still there.


The buttons are mounted on a board at the top of the frame they then feed to the circuit board on the right of the frame which has a 10K pull up that also has the + from the arduino and the feed to the switch and arduino D pin.
Note there are a number of other things in this photo that are also in the process of being cabled up hence the rats nest at this point.

Hi,
What is this board?
Where are the buttons?
Can you please draw a circuit diagram of your COMPLETE circuit?


Have you ANY basic code to check JUST the switches being pressed?

Tom.... :slight_smile:

Hi,

The buttons are mounted on a board at the top of the frame they then feed to the circuit board on the right of the frame which has a 10K pull up that also has the + from the arduino and the feed to the switch and arduino D pin.
Note there are a number of other things in this photo that are also in the process of being cabled up hence the rats nest at this point.

Your Fritzy shows PULL DOWN resistors!!!!
Please post the code you are using?
What pins are the buttons connected too?
Tom... :slight_smile:

See below circuit diagram.

S1, S3, S5 work in the drawing with the X S2 and S4 dont.
In the drawing with the tick all 5 work.

The board is a mega 2560 and a genuine uno and the works across different pins

TomGeorge:
Hi,
Your Fritzy shows PULL DOWN resistors!!!
Please post the code you are using?
What pins are the buttons connected too?

Your correct it is a pull down and not pull up. Its been a long day. also in message 2 change the work batteries for buttons.

The code is available in message 2. currently it is connected as per the code in the first block in message 2.
However if I connect them to other pins the same behaviour exists.

Hi,
What code are you using?
Tom... :slight_smile:

Hi,
Just have the buttons connected and run this code.
Open the Serial Monitor in the IDE, select 9600 baud.
Note what is displayed when;
NO button is pressed;
Then when a button in turn is pressed, keep the button pressed for at least a second so the display will update.

int button1Pin = 2;
int button2Pin = 3;
int button3Pin = 4;
int button4Pin = 5;
int button5Pin = 6;


void setup()
{
  Serial.begin(9600);
  Serial.println("***BUTTON TEST CODE***");
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(button5Pin, INPUT);
}


void loop()
{
  Serial.print("Button1 = ");
  Serial.print(digitalRead(button1Pin));
  Serial.print("\tButton2 = ");
  Serial.print(digitalRead(button2Pin));
  Serial.print("\tButton3 = ");
  Serial.print(digitalRead(button3Pin));
  Serial.print("\tButton4 = ");
  Serial.print(digitalRead(button4Pin));
  Serial.print("\tButton5 = ");
  Serial.println(digitalRead(button5Pin));


  delay(500);
}

Tom... :slight_smile:

There is simply no reason to wire different buttons differently.

In general, they should be wired from pin to ground, with a pull-up resistor if using pinMode of INPUT_PULLUP is not sufficiently reliable.

Agreed with #11. That's the way to wire buttons, and unless using long wires you shouldn't need external resistors at all. Makes life just that little bit easier :slight_smile:

Note that a button will record LOW when pressed in that case, HIGH when unpressed.