r/ArduinoProjects • • Apr 21 '23

what happened hear

when i touch jumper wire this happened closing circuit without grounding/circuit loop and sending keystroke how is this possible. i am testing simple one pushbutton keyboard code.

0 Upvotes

7 comments sorted by

1

u/Nearby-Budget-5520 Apr 21 '23
#include "Keyboard.h"
const int buttonPin = 2; 
int buttonState = 0; 

void setup() {
  pinMode(buttonPin, INPUT); 
  Keyboard.begin(); 
}

void loop() {
  buttonState = digitalRead(buttonPin); 
  if (buttonState == HIGH) { 
    Keyboard.press('x'); 
    delay(100);
    Keyboard.releaseAll();
}
}

2

u/tipppo Apr 21 '23

Classic case of "floating input". When an input pin is not somehow pulled to 5V or GND it will pick up electrical noise from the air. You need to add either a pulldown resistor between the input and GND, a pullup resistor from the input to 5V, or declare the pin as pinMode(buttonpin, INPUT_PULLUP); to use the Ardunio's built in pullup resistor. If you use pullup then you need to reverse your logic. Either: if (!buttonState == HIGH) (add a "!") or if (buttonState == LOW)

2

u/[deleted] Apr 21 '23

[removed] — view removed comment

1

u/Nearby-Budget-5520 Apr 21 '23

no just double sided tape