DUE digital Input Issues

Hi, I've been using a mega but I ordered a DUE mainly for the USB HOST, while most things work I am having some issues with the Digital Inputs specially when using buttons.

I've been using the Button Library found here http://playground.arduino.cc/Code/Button

It works great on the mega but on the DUE it works erratically, the buttons are activated on their own at random (mainly using the uniquePress function) and the strangest thing is that when i use that library I only have to touch the buttons (little toggle buttons) or even just touch the wire and it will interpreted as if the button was pressed, it's like suddently it becomes overly sensitive.

So i tried this code without using the Button Library

void setup(){
  pinMode(22,INPUT_PULLUP); //debug to led 13
  pinMode(13,OUTPUT); //debug to led 13
  digitalWrite(22,HIGH);
}

void loop(){
if(digitalRead(22)==LOW){
    digitalWrite(13,HIGH);
  } else {
    digitalWrite(13,LOW);
  }
}

The code above works perfectly, the led stays on while i press the switch and off at release. Note that the led won't go on if i start touching the wire from the pin or if i touch the switch like it happens with the Button library.

I even went so far as to modify the Button library to make sure that the buttons as always at INPUT_PULLUP

Another odd thing is that the built in and pin 53 are on by default, if i upload an empty sketch with just setup() and loop() both pin 13 and pin 53 are HIGH pushing 3.25v, is this normal? for them to default to off i gotta set the pinMode for each to OUTPUT at setup() and then they default to LOW

Any suggestions would be appreciated

Thank you in advance!!!

The DUE is a LOT faster than the MEGA, so some input denouncing will help.

Also consider there may be some low- level noise getting into the input when it's open circuit. Add some simple filtering if needed.

Two things that are easy to fix at first glance.

It's a good lesson to learn, and will make your future projects more bulletproof.

The DUE is 3.3 v for I/o. caution: feeding 5v from a mega board directly to a 3.3v due input will typically blow the DUE. you have been warned.

Any suggestions would be appreciated

Probably not, because I am going to suggest that you not use the Button library for something as simple as determining if a switch is pressed.

neroroxxx: the strangest thing is that when i use that library I only have to touch the buttons (little toggle buttons) or even just touch the wire and it will interpreted as if the button was pressed, it's like suddently it becomes overly sensitive.

That's caused by very high-Ohm ("weak") internal pull-up resistors on a DUE, most likely.

With 8-bit Atmega (UNO, MEGA2560) controllers the internal pull-up resistors are in the range 20K to 50K Ohm.

With Arduino DUE the internal pull-ups are in the range 50K to 150K Ohm.

So if your body resistance against ground is less than 150K and you touch the wire, this might pull pdown the pull-up of a DUE towards LOW and may lead to a LOW reading with digitalRead().

If you find that working code for INPUT_PULLUP buttons does no longer work when using a DUE contrroller board, then you better should add external pull-u or pull-down resistors of perhaps 4.7K to your button circuit.

Please be aware that DUE is 3.3V operating voltage only, so when using 4.7K external pull-up resistors in addition to the weak internal pullups. make sure to pull up against 3.3V only, not 5V, because 5V is over-voltage for a DUE board.

lastchancename: The DUE is a LOT faster than the MEGA, so some input denouncing will help.

Input, j'accuse.

Thank you for the answers guys, I was aware of the DUE being 3.3v and what to watch for.

I tried external pullup and pulldowns with no luck, 19k and 4k7, still extremely sensitive, if i tap my desk it will take it as a button press.

Any other ideas?

Here is the most basic example I found of polling a PIO input, see attached schematic :

/*
  PIO with polling input
  Example  which uses a tactile switch that when pressed dims a normally lit onboard LED.
  The switch is configured as an input on PC12/DUE PIN N° 51 and the onboard LED is located on PB27/DUE PIN N°13.
*/


void setup() {

  //We need to associate clock with Port C so we can read input
  //A list of Peripheral ID's can be found on Page 37 in the datasheet.
  
  PMC->PMC_PCER0 = PMC_PCER0_PID13; // PIOC power on
  
  //Enable PB27 and PC12 (Peripheral Enable Register)
  PIOB->PIO_PER = PIO_PB27;
  PIOC->PIO_PER = PIO_PC12;
  
  //Set B27 as output (Output Enable Register)
  PIOB->PIO_OER = PIO_PB27;

  //Set C12 as input (Ouput Disable Register)
  PIOC->PIO_ODR = PIO_PC12;

  //Disable pull-up on both pins (Pull Up Disable Register)
  PIOB->PIO_PUDR = PIO_PB27;
  PIOC->PIO_PUDR = PIO_PC12;
}


void loop() {
  //Very simple polling routine that checks the Pin Data Register's status 
  //and sets the state of the LED accordingly.

  if ((PIOC->PIO_PDSR & PIO_PC12) == PIO_PC12)
    PIOB->PIO_SODR = PIO_PB27;  //Set Ouput Data Register
  else
    PIOB->PIO_CODR = PIO_PB27;  //Clear Output Data Register
  delay(10);

}

Schematic.pdf (102 KB)

Ok, I need a punch! so after testing more, it looks like i got it to work with external pullup/pulldown resistors.

If i use the internal pullup it won't work properly as it was on my original post but after i took apart my breadboard to set it up for external pullup resistors i must have disconnected the ground and 3v3 from one side of the board which was the reason using external pullup/pulldown wasn't working.

Once i wired it properly it works great!

Thank you for the help!!!