Avec Arduino, 2 fils, un bouton poussoir et un conduit et Arduino, allumer et éteindre une LED (ou autre chose, que vous décidez d’activer ou de désactiver).
Au lieu d’utiliser la touche typique schématique en utilisant une résistance pullup ou le refoulement, comme l’image Fritzing, voici un moyen de contourner cela, en utilisant Arduino et déclarant le bouton épingle comme une entrée numérique, mais puis écriture haute pour que la broche d’entrée numérique.
Dans la fonction de paramétrage :
pinMode (buttonPin, entrée) ;
digitalWrite (buttonPin, HIGH) ;
LED de pin 13 au sol
Il fil comme le schéma de la photo :
Goupille de Arduino 2 sur la broche de bouton.
l’autre côté de la touche est branchée directement à la masse.
Télécharger le code suivant :
//button wired pin 2 to ground directly // constants used here to set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { //initialize the serial port Serial.begin(9600); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); //initialize the buttonPin as output digitalWrite(buttonPin, HIGH); } void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // since we're writing HIGH to the pin when, if it's HIGH , the button isn't pressed, as in there is no connectivity between ground and pin 2: //so do whatever here that you want when the button is not pushed digitalWrite(ledPin, LOW); Serial.println("button not pushed "); } else { // turn LED on, or do whatever else you want when your button is pushed digitalWrite(ledPin, HIGH); Serial.println("button pushed"); } }