This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:hardware2:sensors_touch [2023/08/18 23:14] – pczekalski | en:iot-open:hardware2:sensors_touch [2023/11/23 12:37] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Touch Sensors ====== | ||
| + | {{: | ||
| + | == Button == | ||
| + | A **pushbutton** is an electromechanical sensor that connects or disconnects two points in a circuit when force is applied. The button output discrete value is either //HIGH// or //LOW// (figure {{ref> | ||
| + | <figure button1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | A **microswitch**, | ||
| + | |||
| + | <figure microswitch1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | The most common use of a pushbutton is as an input device. Both force solutions can be used as simple object detectors or end switches in industrial devices. The button can be connected to any available digital pin configured as input or input with a pull-up. In the configuration presented in figure {{ref> | ||
| + | |||
| + | <figure pullup1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code: | ||
| + | <code c> | ||
| + | |||
| + | int buttonPin = 2; // | ||
| + | int buttonState = 0; //A variable for reading the push button status | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | pinMode(buttonPin, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | //Read the state of the pin where a button is connected | ||
| + | //it is LOW if a button is pressed, HIGH otherwise | ||
| + | //the buttonState variable holds the compliment state of the buttonPin | ||
| + | buttonState = !digitalRead(buttonPin); | ||
| + | //Check if the push button is pressed. If it is, the buttonState variable is HIGH | ||
| + | if (buttonState == HIGH) { | ||
| + | //Print out text in the console | ||
| + | Serial.println(" | ||
| + | } else { | ||
| + | Serial.println(" | ||
| + | } | ||
| + | delay(10); //Delay in between reads for stability | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | == Force Sensor == | ||
| + | |||
| + | A force sensor predictably changes resistance depending on the applied force to its surface. Force-sensing resistors are manufactured in different shapes and sizes, and they can measure not only direct force but also tension, compression, | ||
| + | Force sensors are used as control buttons, object presence detectors, or to determine weight in electronic scales. | ||
| + | |||
| + | <figure forcesensor1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure forcesensor2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code: | ||
| + | <code c> | ||
| + | |||
| + | //Force Sensitive Resistor (FSR) is connected to the analogue 0 pin | ||
| + | int fsrPin = A0; | ||
| + | //The analog reading from the FSR resistor divider | ||
| + | int fsrReading; | ||
| + | |||
| + | void setup(void) { | ||
| + | //Begin serial communication | ||
| + | Serial.begin(9600); | ||
| + | // | ||
| + | pinMode(fsrPin, | ||
| + | } | ||
| + | |||
| + | void loop(void) { | ||
| + | //Read the resistance value of the FSR | ||
| + | fsrReading = analogRead(fsrPin); | ||
| + | // | ||
| + | Serial.print(" | ||
| + | Serial.println(fsrReading); | ||
| + | delay(10); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Capacitive Sensor == | ||
| + | Capacitive sensors are a range of sensors that use capacitance to measure changes in the surrounding environment. A capacitive sensor is a capacitor charged with a certain amount of current until the threshold voltage is reached. A human finger, liquids or other conductive or dielectric materials that touch the sensor can influence the sensor' | ||
| + | Capacitive sensors are input devices and can measure proximity, humidity, fluid level and other physical parameters or serve as an input for electronic device control. Sample sensor and its connection are presented in the figures {{ref> | ||
| + | |||
| + | <figure capacity1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure capacity2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | int touchPin = 2; | ||
| + | |||
| + | //The variable that stores digital value read from the sensor | ||
| + | boolean touchReading = LOW; | ||
| + | //The variable that stores the previous state of the sensor | ||
| + | boolean lastState = LOW; | ||
| + | |||
| + | void setup() { | ||
| + | //Begin serial communication | ||
| + | Serial.begin(9600); | ||
| + | // | ||
| + | pinMode(touchPin, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | //Read the digital value of the capacitive sensor | ||
| + | touchReading = digitalRead(touchPin); | ||
| + | //If the new touch has appeared | ||
| + | if (currentState == HIGH && lastState == LOW){ | ||
| + | Serial.println(" | ||
| + | delay(10); //short delay | ||
| + | } | ||
| + | //Save the previous state to see relative changes | ||
| + | lastState = currentState; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <note important> | ||
| + | </ | ||