Créer un capteurs capacitifs
Vous pouvez créer une entrée tactile sur l'une des broche de l'Arduino.
Il ne nécessite aucun matériel spécial, néanmoins un condensateur de 1nF est recommandé en ligne avec la broche pour découpler les bruits de 50Hz.
Connecter un fil ou une plaque métallique à une broche.
Le code fonctionne en mettant la broche à la terre, tournant sur la résistance de pull-up interne, et en mesurant le temps qu'il faut pour que la broche revenienne à l'état haut.
readCapacitivePin renvoie une valeur faible, par exemple "1" au repos
Quand on le touche il retourne environ 5.
Code source
#include "pins_arduino.h" // Arduino pre-1.0 needs this /* * CapacitivePin Fonction Demo * Alexandre Brugnoni 2015 * Utiliser la résistance pullup sur les entrée digitale */ const int pinSense = 4; const int pinLed = 13; void setup() { pinMode(pinSense , INPUT); // pin activée en entrée pour capter les contacts digitalWrite(pinSense , HIGH); // Activer le pullup pinMode(pinLed, OUTPUT); // pin 13 activée en sortie pour pouvoir allumer la led Serial.begin(9600); // initialisation du port série a 9600 Bd } void loop() { long total = readCapacitivePin(4); Serial.println(total); // Ecriture sur le port serie de la valeur lue du pin 4 if ( total >= 3 ){ // si le total est supperieur a 3 (cette valeur peut être a modifier) digitalWrite(pinLed, HIGH); // on allume la led (HIGH) } else { // sinon digitalWrite(pinLed, LOW); // on éteind la led en mettant le voltage du pin 13 à LOW } delay(50); // on attend un peux pour avoir le temps de lire les valeurs sur le port série } // readCapacitivePin // Input: Arduino pin number // Output: A number, from 0 to 17 expressing // how much capacitance is on the pin // When you touch the pin, or whatever you have // attached to it, the number will get higher uint8_t readCapacitivePin(int pinToMeasure) { // Variables used to translate from Arduino to AVR pin naming volatile uint8_t* port; volatile uint8_t* ddr; volatile uint8_t* pin; // Here we translate the input pin number from // Arduino pin number to the AVR PORT, PIN, DDR, // and which bit of those registers we care about. byte bitmask; port = portOutputRegister(digitalPinToPort(pinToMeasure)); ddr = portModeRegister(digitalPinToPort(pinToMeasure)); bitmask = digitalPinToBitMask(pinToMeasure); pin = portInputRegister(digitalPinToPort(pinToMeasure)); // Discharge the pin first by setting it low and output *port &= ~(bitmask); *ddr |= bitmask; delay(1); // Make the pin an input with the internal pull-up on *ddr &= ~(bitmask); *port |= bitmask; // Now see how long the pin to get pulled up. This manual unrolling of the loop // decreases the number of hardware cycles between each read of the pin, // thus increasing sensitivity. uint8_t cycles = 17; if (*pin & bitmask) { cycles = 0;} else if (*pin & bitmask) { cycles = 1;} else if (*pin & bitmask) { cycles = 2;} else if (*pin & bitmask) { cycles = 3;} else if (*pin & bitmask) { cycles = 4;} else if (*pin & bitmask) { cycles = 5;} else if (*pin & bitmask) { cycles = 6;} else if (*pin & bitmask) { cycles = 7;} else if (*pin & bitmask) { cycles = 8;} else if (*pin & bitmask) { cycles = 9;} else if (*pin & bitmask) { cycles = 10;} else if (*pin & bitmask) { cycles = 11;} else if (*pin & bitmask) { cycles = 12;} else if (*pin & bitmask) { cycles = 13;} else if (*pin & bitmask) { cycles = 14;} else if (*pin & bitmask) { cycles = 15;} else if (*pin & bitmask) { cycles = 16;} // Discharge the pin again by setting it low and output // It's important to leave the pins low if you want to // be able to touch more than 1 sensor at a time - if // the sensor is left pulled high, when you touch // two sensors, your body will transfer the charge between // sensors. *port &= ~(bitmask); *ddr |= bitmask; return cycles; }