Input weird behaviour on Arduino Due and Leonardo

Hi
I am trying to create a keyboard with arduino Due /Leonardo and i can not get a stable input on both boards ' its like some keys are repeating themselves .

I have switched to arduino Mega
Changed the "keyboard.write" to "Serial.write" and the inputs are stable on the arduino Mega.

I did not change the electric wiring between the different boards , so its definitely something with the boards themself .

Any one had this problem ?
attached wiring diagram

Post your code! And don't forget the code tags (that's the </> button in the editor)!

Does your PC have a US keyboard? Keyboard.h library is not designed to be used with other keyboard layouts.

/*

 */
#include "Keyboard.h" 

// the setup routine runs once when you press reset:
void setup() {

 
pinMode(30, INPUT);           // set pin to input
pinMode(31, INPUT);           // set pin to input
pinMode(32, INPUT);           // set pin to input
pinMode(33, INPUT);           // set pin to input
pinMode(34, INPUT);           // set pin to input
pinMode(35, INPUT);           // set pin to input
pinMode(36, INPUT);           // set pin to input
pinMode(37, INPUT);           // set pin to input
pinMode(38, INPUT);           // set pin to input
pinMode(39, INPUT);           // set pin to input
pinMode(40, INPUT);           // set pin to input
pinMode(41, INPUT);           // set pin to input
pinMode(42, INPUT);           // set pin to input
pinMode(43, INPUT);           // set pin to input
pinMode(44, INPUT);           // set pin to input
pinMode(45, INPUT);           // set pin to input
//pinMode(46, INPUT);           // set pin to input




/*
 *
 *
 *
 */

// initialize serial communication at 9600 bits per second:
//   Keyboard.begin();
  Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
 
 if(digitalRead(30)==HIGH){
   
    Serial.write(65);  //A
  }
  if(digitalRead(31)==HIGH){
    Serial.write(66);  //B
  }
  if(digitalRead(32)==HIGH){
     Serial.write(67);  //C
  }
  if(digitalRead(33)==HIGH){
     Serial.write(68);  //D
  }
  if(digitalRead(34)==HIGH){
     Serial.write(69);  //E
  }
  if(digitalRead(35)==HIGH){
     Serial.write(70);  //F
  }
  if(digitalRead(36)==HIGH){
    Serial.write(71);  //G
  }
  if(digitalRead(37)==HIGH){
     Serial.write(72); //H
  }
  if(digitalRead(38)==HIGH){
     Serial.write(73); //I
  }
   if(digitalRead(39)==HIGH){
     Serial.write(74); //J
  }
   if(digitalRead(40)==HIGH){
     Serial.write(75);  //K
  }
   if(digitalRead(41)==HIGH){
     Serial.write(76); //L
  }
   if(digitalRead(42)==HIGH){
     Serial.write(77);  //M
  }
   if(digitalRead(43)==HIGH){
     Serial.write(78); // N
     
  }
 
   if(digitalRead(44)==HIGH){
     Serial.write(79); //O
  }
   if(digitalRead(45)==HIGH){
     Serial.write(80); //P
     
  }
 
 

  delay(100);        // delay in between reads for stability
}

OP's image

Red = 5V? If so, you have routed 5V directly to pin 31 of the Mega and it should always read HIGH.

Based on the code you need pulldown resistors. Read up on floating inputs, you're lucky that it (seems to) woork on a Mega.

Or use internal pullups, wire button between pin and GND and reverse the logic (pressed will be LOW).

I'm quite sure the provided sketch won't work with the provided wiring, not on a Mega and never on a Leonardo. The circuit is simply wrong.
You might move the resistor to replace the left red line and connect the LED output directly to GND. But then you have to check for a LOW state of the input as the default will be HIGH. And you should debounce it, as physical button almost always bounce (go on and off a few times very fast if pushed).

Sorry didnt notice i connected the input in the schematic wrong , posted update schematic , the LED is for 2 reasons , make sure the key works and prevent backflow to the input from the ground

Can't do that.
The pin won't see a well defined LOW through the LED/resistor (semi-floating pin).

Connect the button between pin and ground, and use internal pull up on the pin.
pinMode(buttonPin, INPUT_PULLUP);

Connect the LED/resistor between 5volt and pin.

The pin will now be LOW when pushed, and HIGH when not pushed.
Take care of this 'inverted logic' in your code.
Leo..

Sadly that is what i started with , an i get the same result , i switch to this set up because i figured that there might be some resistance on the ground and then i get some current flowing back to the input pins .

what i am trying to figure out is why is this or the set up you suggested worjing on the Mega and not working on Leonardo nor Due
(i try again the your set up and post back )

A LED/resistor is like a zener diode, and can't be used to properly pull a switch pin up (or down).
If you use internal pull up one the pin, as I explained, it should work for both 3.3volt (Due) and 5volt Arduinos.
Post a real picture and the code if you're still having problems.
Leo..

Thanks , it works now , must have had something interfering the first time .

This solved it for me :slight_smile: