{"id":1600,"date":"2016-05-17T00:32:17","date_gmt":"2016-05-17T00:32:17","guid":{"rendered":"http:\/\/etab.cl\/clases\/uchile\/obj-tec\/?page_id=1600"},"modified":"2016-06-28T14:25:06","modified_gmt":"2016-06-28T14:25:06","slug":"at2016-c7","status":"publish","type":"page","link":"https:\/\/clases.etab.cl\/?page_id=1600","title":{"rendered":"AT2016: C7"},"content":{"rendered":"<p><strong>Sensores<\/strong><\/p>\n<p><strong>Resistencias pull-up y pull down<\/strong><br \/>\n<iframe loading=\"lazy\" width=\"300\" height=\"169\" src=\"https:\/\/www.youtube.com\/embed\/bKv0nj4gkFk\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p><strong>1) Revisi\u00f3n sensores DIY<\/strong><br \/>\n<strong>2) Conexi\u00f3n de sensores a Arduino<\/strong><br \/>\n<strong>2.1) Conexi\u00f3n de sensores digitales: resistencia pull-up y pull-down<\/strong><br \/>\nhttps:\/\/www.youtube.com\/watch?v=bKv0nj4gkFk (espa\u00f1ol)<\/p>\n<p>https:\/\/www.youtube.com\/watch?v=wxjerCHCEMg (ingl\u00e9s, m\u00e1s completo)<\/p>\n<p>https:\/\/www.youtube.com\/watch?v=BxA7qwmY9mg (ingl\u00e9s completo ++)<\/p>\n<p><strong>2.1.1) Pull-up: <\/strong><br \/>\nEjercicio:<br \/>\nhttps:\/\/www.arduino.cc\/en\/Tutorial\/InputPullupSerial<br \/>\n&#8211; uso de INPUT_PULLUP<\/p>\n<p>void setup() {<br \/>\n\/\/start serial connection<br \/>\nSerial.begin(9600);<br \/>\n\/\/configure pin2 as an input and enable the internal pull-up resistor<br \/>\npinMode(2, INPUT_PULLUP);<br \/>\npinMode(13, OUTPUT);<\/p>\n<p>}<\/p>\n<p>void loop() {<br \/>\n\/\/read the pushbutton value into a variable<br \/>\nint sensorVal = digitalRead(2);<br \/>\n\/\/print out the value of the pushbutton<br \/>\nSerial.println(sensorVal);<\/p>\n<p>\/\/ Keep in mind the pullup means the pushbutton&#8217;s<br \/>\n\/\/ logic is inverted. It goes HIGH when it&#8217;s open,<br \/>\n\/\/ and LOW when it&#8217;s pressed. Turn on pin 13 when the<br \/>\n\/\/ button&#8217;s pressed, and off when it&#8217;s not:<br \/>\nif (sensorVal == HIGH) {<br \/>\ndigitalWrite(13, LOW);<br \/>\n} else {<br \/>\ndigitalWrite(13, HIGH);<br \/>\n}<br \/>\n}<\/p>\n<p><strong>3) Conexi\u00f3n de sensores anal\u00f3gicos: divisor de corriente<\/strong><br \/>\nhttps:\/\/learn.sparkfun.com\/tutorials\/voltage-dividers<\/p>\n<p><strong>4) Calibraci\u00f3n autom\u00e1tica<\/strong><br \/>\n\/*<br \/>\nCalibration<\/p>\n<p>Demonstrates one technique for calibrating sensor input. The<br \/>\nsensor readings during the first five seconds of the sketch<br \/>\nexecution define the minimum and maximum of expected values<br \/>\nattached to the sensor pin.<\/p>\n<p>The sensor minimum and maximum initial values may seem backwards.<br \/>\nInitially, you set the minimum high and listen for anything<br \/>\nlower, saving it as the new minimum. Likewise, you set the<br \/>\nmaximum low and listen for anything higher as the new maximum.<\/p>\n<p>The circuit:<br \/>\n* Analog sensor (potentiometer will do) attached to analog input 0<br \/>\n* LED attached from digital pin 9 to ground<\/p>\n<p>created 29 Oct 2008<br \/>\nBy David A Mellis<br \/>\nmodified 30 Aug 2011<br \/>\nBy Tom Igoe<\/p>\n<p>http:\/\/arduino.cc\/en\/Tutorial\/Calibration<\/p>\n<p>This example code is in the public domain.<\/p>\n<p>*\/<\/p>\n<p>\/\/ These constants won&#8217;t change:<br \/>\nconst int sensorPin = A0; \/\/ pin that the sensor is attached to<br \/>\nconst int ledPin = 9; \/\/ pin that the LED is attached to<\/p>\n<p>\/\/ variables:<br \/>\nint sensorValue = 0; \/\/ the sensor value<br \/>\nint sensorMin = 1023; \/\/ minimum sensor value<br \/>\nint sensorMax = 0; \/\/ maximum sensor value<\/p>\n<p>void setup() {<br \/>\n\/\/ turn on LED to signal the start of the calibration period:<br \/>\npinMode(13, OUTPUT);<br \/>\ndigitalWrite(13, HIGH);<\/p>\n<p>\/\/ calibrate during the first five seconds<br \/>\nwhile (millis() &lt; 5000) { sensorValue = analogRead(sensorPin); \/\/ record the maximum sensor value if (sensorValue &gt; sensorMax) {<br \/>\nsensorMax = sensorValue;<br \/>\n}<\/p>\n<p>\/\/ record the minimum sensor value<br \/>\nif (sensorValue &lt; sensorMin) {<br \/>\nsensorMin = sensorValue;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ signal the end of the calibration period<br \/>\ndigitalWrite(13, LOW);<br \/>\n}<\/p>\n<p>void loop() {<br \/>\n\/\/ read the sensor:<br \/>\nsensorValue = analogRead(sensorPin);<\/p>\n<p>\/\/ apply the calibration to the sensor reading<br \/>\nsensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);<\/p>\n<p>\/\/ in case the sensor value is outside the range seen during calibration<br \/>\nsensorValue = constrain(sensorValue, 0, 255);<\/p>\n<p>\/\/ fade the LED using the calibrated value:<br \/>\nanalogWrite(ledPin, sensorValue);<br \/>\n}<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p><strong>5) Funci\u00f3n Map<\/strong><br \/>\nReferencia: https:\/\/www.arduino.cc\/en\/Reference\/Map<\/p>\n<p>\/* 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);<br \/>\n}<\/p>\n<p><strong>6) Funci\u00f3n Constrain<\/strong><br \/>\nhttps:\/\/www.arduino.cc\/en\/Reference\/Constrain<\/p>\n<p>\/* 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);<br \/>\n}<\/p>\n<p><strong>7) Capacitive Sensor<\/strong><br \/>\n\/*<br \/>\nArduino Starter Kit example<br \/>\nProject 13 &#8211; Touch Sensor Lamp<\/p>\n<p>This sketch is written to accompany Project 13 in the<br \/>\nArduino Starter Kit<\/p>\n<p>Parts required:<br \/>\n1 Megohm resistor<br \/>\nmetal foil or copper mesh<br \/>\n220 ohm resistor<br \/>\nLED<\/p>\n<p>Software required :<br \/>\nCapacitiveSensor library by Paul Badger<br \/>\nhttp:\/\/arduino.cc\/playground\/Main\/CapacitiveSensor<\/p>\n<p>Created 18 September 2012<br \/>\nby Scott Fitzgerald<\/p>\n<p>http:\/\/arduino.cc\/starterKit<\/p>\n<p>This example code is part of the public domain<br \/>\n*\/<\/p>\n<p>\/\/ import the library (must be located in the<br \/>\n\/\/ Arduino\/libraries directory)<br \/>\n#include<\/p>\n<p>\/\/ create an instance of the library<br \/>\n\/\/ pin 4 sends electrical energy<br \/>\n\/\/ pin 2 senses senses a change<br \/>\nCapacitiveSensor capSensor = CapacitiveSensor(4,2);<\/p>\n<p>\/\/ threshold for turning the lamp on<br \/>\nint threshold = 1000;<\/p>\n<p>\/\/ pin the LED is connected to<br \/>\nconst int ledPin = 12;<\/p>\n<p>void setup() {<br \/>\n\/\/ open a serial connection<br \/>\nSerial.begin(9600);<br \/>\n\/\/ set the LED pin as an output<br \/>\npinMode(ledPin, OUTPUT);<br \/>\n}<\/p>\n<p>void loop() {<br \/>\n\/\/ store the value reported by the sensor in a variable<br \/>\nlong sensorValue = capSensor.capacitiveSensor(30);<\/p>\n<p>\/\/ print out the sensor value<br \/>\nSerial.println(sensorValue);<\/p>\n<p>\/\/ if the value is greater than the threshold<br \/>\nif(sensorValue &gt; threshold) {<br \/>\n\/\/ turn the LED on<br \/>\ndigitalWrite(ledPin, HIGH);<br \/>\n}<br \/>\n\/\/ if it&#8217;s lower than the threshold<br \/>\nelse {<br \/>\n\/\/ turn the LED off<br \/>\ndigitalWrite(ledPin, LOW);<br \/>\n}<\/p>\n<p>delay(10);<br \/>\n}<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p><strong>7) Smoothing:<\/strong><br \/>\nhttps:\/\/www.arduino.cc\/en\/Tutorial\/Smoothing<\/p>\n<p>\/*<\/p>\n<p>Smoothing<\/p>\n<p>Reads repeatedly from an analog input, calculating a running average<br \/>\nand printing it to the computer. Keeps ten readings in an array and<br \/>\ncontinually averages them.<\/p>\n<p>The circuit:<br \/>\n* Analog sensor (potentiometer will do) attached to analog input 0<\/p>\n<p>Created 22 April 2007<br \/>\nBy David A. Mellis &lt;dam@mellis.org&gt;<br \/>\nmodified 9 Apr 2012<br \/>\nby Tom Igoe<br \/>\nhttp:\/\/www.arduino.cc\/en\/Tutorial\/Smoothing<\/p>\n<p>This example code is in the public domain<\/p>\n<p>*\/<\/p>\n<p>\/\/ Define the number of samples to keep track of. The higher the number,<br \/>\n\/\/ the more the readings will be smoothed, but the slower the output will<br \/>\n\/\/ respond to the input. Using a constant rather than a normal variable lets<br \/>\n\/\/ use this value to determine the size of the readings array.<br \/>\nconst int numReadings = 10;<\/p>\n<p>int readings[numReadings]; \/\/ the readings from the analog input<br \/>\nint index = 0; \/\/ the index of the current reading<br \/>\nint total = 0; \/\/ the running total<br \/>\nint average = 0; \/\/ the average<\/p>\n<p>int inputPin = A0;<\/p>\n<p>void setup()<br \/>\n{<br \/>\n\/\/ initialize serial communication with computer:<br \/>\nSerial.begin(9600);<br \/>\n\/\/ initialize all the readings to 0:<br \/>\nfor (int thisReading = 0; thisReading &lt; numReadings; thisReading++) readings[thisReading] = 0; } void loop() { \/\/ subtract the last reading: total= total &#8211; 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&#8217;re at the end of the array&#8230; if (index &gt;= numReadings)<br \/>\n\/\/ &#8230;wrap around to the beginning:<br \/>\nindex = 0;<\/p>\n<p>\/\/ calculate the average:<br \/>\naverage = total \/ numReadings;<br \/>\n\/\/ send it to the computer as ASCII digits<br \/>\nSerial.println(average);<br \/>\ndelay(1); \/\/ delay in between reads for stability<br \/>\n}<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p><strong>8) Switch Case:<\/strong><br \/>\nhttps:\/\/www.arduino.cc\/en\/Tutorial\/SwitchCase<\/p>\n<p>\/*<br \/>\nSwitch statement<\/p>\n<p>Demonstrates the use of a switch statement. The switch<br \/>\nstatement allows you to choose from among a set of discrete values<br \/>\nof a variable. It&#8217;s like a series of if statements.<\/p>\n<p>To see this sketch in action, but the board and sensor in a well-lit<br \/>\nroom, open the serial monitor, and and move your hand gradually<br \/>\ndown over the sensor.<\/p>\n<p>The circuit:<br \/>\n* photoresistor from analog in 0 to +5V<br \/>\n* 10K resistor from analog in 0 to ground<\/p>\n<p>created 1 Jul 2009<br \/>\nmodified 9 Apr 2012<br \/>\nby Tom Igoe<\/p>\n<p>This example code is in the public domain.<\/p>\n<p>http:\/\/www.arduino.cc\/en\/Tutorial\/SwitchCase<br \/>\n*\/<br \/>\n\/\/ these constants won&#8217;t change. They are the<br \/>\n\/\/ lowest and highest readings you get from your sensor:<br \/>\nconst int sensorMin = 0; \/\/ sensor minimum, discovered through experiment<br \/>\nconst int sensorMax = 600; \/\/ sensor maximum, discovered through experiment<\/p>\n<p>void setup() {<br \/>\n\/\/ initialize serial communication:<br \/>\nSerial.begin(9600);<br \/>\n}<\/p>\n<p>void loop() {<br \/>\n\/\/ read the sensor:<br \/>\nint sensorReading = analogRead(A0);<br \/>\n\/\/ map the sensor range to a range of four options:<br \/>\nint range = map(sensorReading, sensorMin, sensorMax, 0, 3);<\/p>\n<p>\/\/ do something different depending on the<br \/>\n\/\/ range value:<br \/>\nswitch (range) {<br \/>\ncase 0: \/\/ your hand is on the sensor<br \/>\nSerial.println(&#8220;dark&#8221;);<br \/>\nbreak;<br \/>\ncase 1: \/\/ your hand is close to the sensor<br \/>\nSerial.println(&#8220;dim&#8221;);<br \/>\nbreak;<br \/>\ncase 2: \/\/ your hand is a few inches from the sensor<br \/>\nSerial.println(&#8220;medium&#8221;);<br \/>\nbreak;<br \/>\ncase 3: \/\/ your hand is nowhere near the sensor<br \/>\nSerial.println(&#8220;bright&#8221;);<br \/>\nbreak;<br \/>\n}<br \/>\ndelay(1); \/\/ delay in between reads for stability<br \/>\n}<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p><strong>Otros ejemplos sensores:<\/strong><br \/>\nDebounce: https:\/\/www.arduino.cc\/en\/Tutorial\/Debounce<\/p>\n<p>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\u00f3n)<br \/>\nmin: https:\/\/www.arduino.cc\/en\/Reference\/Min<br \/>\nmax: https:\/\/www.arduino.cc\/en\/Reference\/Max<\/p>\n<p><strong>Extra &#8211; salida:<\/strong><br \/>\nhttps:\/\/www.arduino.cc\/en\/Tutorial\/ToneMultiple?from=Tutorial.Tone4<\/p>\n<p>Digital pins: https:\/\/www.arduino.cc\/en\/Tutorial\/DigitalPins<br \/>\nAnalog pins: https:\/\/www.arduino.cc\/en\/Tutorial\/AnalogInputPins<br \/>\nSparkfun c\u00e1lculo pullup: https:\/\/learn.sparkfun.com\/tutorials\/pull-up-resistors?_ga=1.25821428.858725962.1459691202<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sensores Resistencias pull-up y pull down 1) Revisi\u00f3n sensores DIY 2) Conexi\u00f3n de sensores a Arduino 2.1) Conexi\u00f3n de sensores digitales: resistencia pull-up y pull-down https:\/\/www.youtube.com\/watch?v=bKv0nj4gkFk (espa\u00f1ol) https:\/\/www.youtube.com\/watch?v=wxjerCHCEMg (ingl\u00e9s, m\u00e1s completo) https:\/\/www.youtube.com\/watch?v=BxA7qwmY9mg (ingl\u00e9s completo ++) 2.1.1) Pull-up: Ejercicio: https:\/\/www.arduino.cc\/en\/Tutorial\/InputPullupSerial &#8211; uso de INPUT_PULLUP void setup() { \/\/start serial connection Serial.begin(9600); \/\/configure pin2 as an input [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1496,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1600","page","type-page","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/pages\/1600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/clases.etab.cl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1600"}],"version-history":[{"count":5,"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/pages\/1600\/revisions"}],"predecessor-version":[{"id":1613,"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/pages\/1600\/revisions\/1613"}],"up":[{"embeddable":true,"href":"https:\/\/clases.etab.cl\/index.php?rest_route=\/wp\/v2\/pages\/1496"}],"wp:attachment":[{"href":"https:\/\/clases.etab.cl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}