AT2016: C7


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 (español) (inglés, más completo) (inglés completo ++)

2.1.1) Pull-up:

void setup() {
//start serial connection
//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

// 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

4) Calibración automática

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

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

/* 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

/* 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

Software required :
CapacitiveSensor library by Paul Badger

Created 18 September 2012
by Scott Fitzgerald

This example code is part of the public domain

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

// 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
// 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

// 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);



7) 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 <>
modified 9 Apr 2012
by Tom Igoe

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:
// 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
delay(1); // delay in between reads for stability


8) Switch Case:

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.
// 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:

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
case 1: // your hand is close to the sensor
case 2: // your hand is a few inches from the sensor
case 3: // your hand is nowhere near the sensor
delay(1); // delay in between reads for stability


Otros ejemplos sensores:

Edge detection para push button: (Ejemplo: cambiar el color de un led rgb random-mente cada vez que se apreta el botón)

Extra – salida:

Digital pins:
Analog pins:
Sparkfun cálculo pullup: