AT2016: C7

Sensores

Resistencias pull-up y pull down

1) Revisión sensores DIY
2) Conexión de sensores a Arduino
2.1) Conexión de sensores digitales: resistencia pull-up y pull-down
https://www.youtube.com/watch?v=bKv0nj4gkFk (español)

https://www.youtube.com/watch?v=wxjerCHCEMg (inglés, más completo)

https://www.youtube.com/watch?v=BxA7qwmY9mg (inglés completo ++)

2.1.1) Pull-up:
Ejercicio:
https://www.arduino.cc/en/Tutorial/InputPullupSerial
– uso de INPUT_PULLUP

void setup() {
//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);

}

void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);

// Keep in mind the pullup means the pushbutton’s
// logic is inverted. It goes HIGH when it’s open,
// and LOW when it’s pressed. Turn on pin 13 when the
// button’s pressed, and off when it’s not:
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
}

3) Conexión de sensores analógicos: divisor de corriente
https://learn.sparkfun.com/tutorials/voltage-dividers

4) Calibración automática
/*
Calibration

Demonstrates one technique for calibrating sensor input. The
sensor readings during the first five seconds of the sketch
execution define the minimum and maximum of expected values
attached to the sensor pin.

The sensor minimum and maximum initial values may seem backwards.
Initially, you set the minimum high and listen for anything
lower, saving it as the new minimum. Likewise, you set the
maximum low and listen for anything higher as the new maximum.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0
* LED attached from digital pin 9 to ground

created 29 Oct 2008
By David A Mellis
modified 30 Aug 2011
By Tom Igoe

http://arduino.cc/en/Tutorial/Calibration

This example code is in the public domain.

*/

// These constants won’t change:
const int sensorPin = A0; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to

// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value

void setup() {
// turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the first five seconds
while (millis() < 5000) { sensorValue = analogRead(sensorPin); // record the maximum sensor value if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

// signal the end of the calibration period
digitalWrite(13, LOW);
}

void loop() {
// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}

———————

5) Función Map
Referencia: https://www.arduino.cc/en/Reference/Map

/* Map an analog value to 8 bits (0 to 255) */void setup() {}void loop(){ int val = analogRead(0); val = map(val, 0, 1023, 0, 255); //revisar en serial print el valor mayor y menor analogWrite(9, val);Serial.println(val);
}

6) Función Constrain
https://www.arduino.cc/en/Reference/Constrain

/* Map an analog value to 8 bits (0 to 255) */void setup() {}void loop(){ int val = analogRead(0); mappedVal = map(val, 0, 1023, 0, 255); //revisar en serial print el valor mayor y menorconsVal = constrain(mappedVal, 30, 400); //revisar en serial print los valores analogWrite(9, val);Serial.println(val);
}

7) Capacitive Sensor
/*
Arduino Starter Kit example
Project 13 – Touch Sensor Lamp

This sketch is written to accompany Project 13 in the
Arduino Starter Kit

Parts required:
1 Megohm resistor
metal foil or copper mesh
220 ohm resistor
LED

Software required :
CapacitiveSensor library by Paul Badger
http://arduino.cc/playground/Main/CapacitiveSensor

Created 18 September 2012
by Scott Fitzgerald

http://arduino.cc/starterKit

This example code is part of the public domain
*/

// import the library (must be located in the
// Arduino/libraries directory)
#include

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;

void setup() {
// open a serial connection
Serial.begin(9600);
// set the LED pin as an output
pinMode(ledPin, OUTPUT);
}

void loop() {
// store the value reported by the sensor in a variable
long sensorValue = capSensor.capacitiveSensor(30);

// print out the sensor value
Serial.println(sensorValue);

// if the value is greater than the threshold
if(sensorValue > threshold) {
// turn the LED on
digitalWrite(ledPin, HIGH);
}
// if it’s lower than the threshold
else {
// turn the LED off
digitalWrite(ledPin, LOW);
}

delay(10);
}

————————————

7) Smoothing:
https://www.arduino.cc/en/Tutorial/Smoothing

/*

Smoothing

Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis <dam@mellis.org>
modified 9 Apr 2012
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Smoothing

This example code is in the public domain

*/

// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; } void loop() { // subtract the last reading: total= total – readings[index]; // read from the sensor: readings[index] = analogRead(inputPin); // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1; // if we’re at the end of the array… if (index >= numReadings)
// …wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
}

—————————————————

8) Switch Case:
https://www.arduino.cc/en/Tutorial/SwitchCase

/*
Switch statement

Demonstrates the use of a switch statement. The switch
statement allows you to choose from among a set of discrete values
of a variable. It’s like a series of if statements.

To see this sketch in action, but the board and sensor in a well-lit
room, open the serial monitor, and and move your hand gradually
down over the sensor.

The circuit:
* photoresistor from analog in 0 to +5V
* 10K resistor from analog in 0 to ground

created 1 Jul 2009
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/SwitchCase
*/
// these constants won’t change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// do something different depending on the
// range value:
switch (range) {
case 0: // your hand is on the sensor
Serial.println(“dark”);
break;
case 1: // your hand is close to the sensor
Serial.println(“dim”);
break;
case 2: // your hand is a few inches from the sensor
Serial.println(“medium”);
break;
case 3: // your hand is nowhere near the sensor
Serial.println(“bright”);
break;
}
delay(1); // delay in between reads for stability
}

—————————

Otros ejemplos sensores:
Debounce: https://www.arduino.cc/en/Tutorial/Debounce

Edge detection para push button: https://www.arduino.cc/en/Tutorial/StateChangeDetection?from=Tutorial.ButtonStateChange (Ejemplo: cambiar el color de un led rgb random-mente cada vez que se apreta el botón)
min: https://www.arduino.cc/en/Reference/Min
max: https://www.arduino.cc/en/Reference/Max

Extra – salida:
https://www.arduino.cc/en/Tutorial/ToneMultiple?from=Tutorial.Tone4

Digital pins: https://www.arduino.cc/en/Tutorial/DigitalPins
Analog pins: https://www.arduino.cc/en/Tutorial/AnalogInputPins
Sparkfun cálculo pullup: https://learn.sparkfun.com/tutorials/pull-up-resistors?_ga=1.25821428.858725962.1459691202