{"id":744,"date":"2015-02-26T03:40:12","date_gmt":"2015-02-26T03:40:12","guid":{"rendered":"https:\/\/www.courses.tegabrain.com\/SS15\/?page_id=744"},"modified":"2015-03-05T17:06:31","modified_gmt":"2015-03-05T17:06:31","slug":"arduino-2","status":"publish","type":"page","link":"https:\/\/www.courses.tegabrain.com\/SS15\/?page_id=744","title":{"rendered":"ARDUINO 2: Sensors"},"content":{"rendered":"<p>INPUTS AND OUTPUTS<br \/>\nRevision &#8211; functions from last week:<\/p>\n<ul>\n<li><a class=\"wikilink\" href=\"http:\/\/arduino.cc\/en\/Reference\/Setup\">setup()<\/a><\/li>\n<li><a class=\"wikilink\" href=\"http:\/\/arduino.cc\/en\/Reference\/Loop\">loop()<\/a><\/li>\n<li><a class=\"wikilink\" href=\"http:\/\/arduino.cc\/en\/Reference\/PinMode\">pinMode()<\/a><\/li>\n<li><a class=\"wikilink\" href=\"http:\/\/arduino.cc\/en\/Reference\/DigitalWrite\">digitalWrite()<\/a><\/li>\n<li><a class=\"wikilink\" href=\"http:\/\/arduino.cc\/en\/Reference\/Delay\">delay()<\/a><\/li>\n<\/ul>\n<p><strong>DIGITAL SENSORS<\/strong><\/p>\n<p>Microcontrollers are powerful ways to connect inputs and outputs. Incoming\u00a0voltage can be read from sensors and interpreted as data. And just like with when we use actuators (outputs), sensors\u00a0can also be digital or analog. A button, or a switch which is on or off, are both examples of a digital sensors because they\u00a0only two states. A volume knob, also known as a potentiometer, is an example of a analog sensor. It produces a range of values.\u00a0In your kits you have\u00a0a light sensor and a temperature sensor both of which are analog sensors.<\/p>\n<p>Digital sensors can be controlled on the digital pins (1-13), simply be setting them to an input pin. This means they are listening for voltage changes instead of sending them out. We do this in code as follows:<\/p>\n<pre lang=\"java\">int inPin = 7;   \/\/ pushbutton connected to digital pin 7 \r\n\r\nvoid setup(){\r\n  pinMode(inPin, INPUT);      \/\/ sets the digital pin 7 as input\r\n}\r\nvoid loop(){\r\n  val = digitalRead(inPin);   \/\/ read the input pin\r\n}\r\n<\/pre>\n<p>This sets pin 7 as inPin. And then sets it as an INPUT pin (meaning it will take a sensor). We then read from the pin by using the function digitalRead();<\/p>\n<p><strong>02A\u00a0CONNECTING A BUTTON<\/strong><\/p>\n<p>See this guide and make a circuit that connects a digital sensor, a button to an LED.\u00a0<a href=\"http:\/\/arduino.cc\/en\/tutorial\/button\">http:\/\/arduino.cc\/en\/tutorial\/button<\/a><\/p>\n<p>This example uses a pull down resistor. Buttons often require what are called pull down or pull up resistors. This is a way of controlling their voltage when their circuit is open. Open circuits can have a voltage that floats around making it difficult for the Arduino to read. So we put a resistor in to make sure the voltage doesn&#8217;t float when the button is open.<\/p>\n<p><strong>02B\u00a0ANALOG SENSORS<\/strong><\/p>\n<p>Analog sensors must be connected to analog pins. (A0-A5). They are set using very similar code except they use the function analogRead(); instead of digitalRead();<\/p>\n<p>By using the analogRead() function, we can read the voltage applied to one of the pins. This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512.<\/p>\n<p>Set up you board with the potentiometer as follows:<\/p>\n<p><a href=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/potenitometer.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-753\" src=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/potenitometer.png\" alt=\"potenitometer\" width=\"806\" height=\"502\" srcset=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/potenitometer.png 806w, https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/potenitometer-300x187.png 300w, https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/potenitometer-535x333.png 535w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/a><\/p>\n<pre lang=\"java\"><\/pre>\n<p>&nbsp;<\/p>\n<pre lang=\"java\">\/\/Example 02B - Potentiometer<\/pre>\n<pre lang=\"java\">int analogPin = 3; \/\/ potentiometer wiper (middle terminal) connected to analog pin 3. Outside leads to ground and +5V \r\n\r\nint val = 0; \/\/ variable to store the value read\r\n\r\nvoid setup(){ \r\n  Serial.begin(9600); \/\/ setup serial\r\n } \r\nvoid loop(){ \r\n  val = analogRead(analogPin); \/\/ read the input piN\r\n  Serial.println(val); \/\/ debug value \r\n}<\/pre>\n<p>Notice we don&#8217;t have to set the pin in void setup to INPUT. This is because A0-A5 are always input pins.<\/p>\n<p>Open up you serial monitor and have a look at the values coming from your potentiometer.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>02C LIGHT\u00a0SENSOR EXAMPLE<\/strong><\/p>\n<p>If you now build the circuit below, using560 Ohm Resistor (Green-Blue-Brown) and run the code listed in Example 02B, you\u2019ll see the onboard LED<\/p>\n<div class=\"page\" title=\"Page 76\">\n<div class=\"section\">\n<div class=\"layoutArea\">\n<div class=\"column\">\n<p>(you could also insert your own LED into pins 13 and GND as shown in \u201cBlinking an LED\u201d in Chapter 4) blinking at a rate that\u2019s dependent upon the amount of light that hits the sensor.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><a href=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/LDR1-1-620x436.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-748\" src=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/LDR1-1-620x436.png\" alt=\"LDR1-1-620x436\" width=\"620\" height=\"436\" srcset=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/LDR1-1-620x436.png 620w, https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/LDR1-1-620x436-300x211.png 300w, https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/02\/LDR1-1-620x436-535x376.png 535w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"java\">\/\/ Example 02C: Blink LED at a rate specified by the\r\n\/\/ value of the analogue input\r\n\r\nint LED = 13; \/\/ the pin for the LED\r\nint val = 0;   \/\/ variable used to store the value\r\n               \/\/ coming from the sensor\r\nvoid setup() {\r\n  pinMode(LED, OUTPUT); \/\/ LED is as an OUTPUT\r\n  \/\/ Note: Analogue pins are\r\n  \/\/ automatically set as inputs\r\n}\r\nvoid loop() {\r\n  val = analogRead(0); \/\/ read the value from\r\n                       \/\/ the sensor\r\n  digitalWrite(13, HIGH); \/\/ turn the LED on\r\n  delay(val); \/\/ stop the program for\r\n              \/\/ some time\r\n  digitalWrite(13, LOW); \/\/ turn the LED off\r\n  delay(val); \/\/ stop the program for\r\n              \/\/ some time\r\n}\r\n<\/pre>\n<p><strong>02D &#8211; REACTIVE LIGHT<\/strong><\/p>\n<p>Now, try Example 02D: but before you do, you\u2019ll need to modify your circuit. Take a look at the next figure again and hook the LED up to pin 9 as shown. Because you\u2019ve already got some stuff on the breadboard, you\u2019ll need to find a spot on the breadboard where the LED, wires, and resistor won\u2019t overlap with the LDR circuit.<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"java\">\/\/ Example 02D: Set the brightness of LED to\r\n\/\/ a brightness specified by the\r\n\/\/ value of the analogue input\r\nint LED =9;  \/\/ the pin for the LED\r\nint val = 0;   \/\/ variable used to store the value\r\n               \/\/ coming from the sensor\r\nvoid setup() {\r\n  pinMode(LED, OUTPUT); \/\/ LED is as an OUTPUT\r\n  \/\/ Note: Analogue pins are\r\n  \/\/ automatically set as inputs\r\n}\r\nvoid loop() {\r\n  val = analogRead(0); \/\/ read the value from\r\n                       \/\/ the sensor\r\n  analogWrite(LED, val\/4); \/\/ turn the LED on at\r\n                           \/\/ the brightness set\r\n                           \/\/ by the sensor\r\n  delay(10); \/\/ stop the program for\r\n             \/\/ some time\r\n}\r\n<\/pre>\n<p>NoTe: we specify the brightness by dividing val by 4, because analogRead() returns a number up to 1023, and analogWrite() accepts a maximum of 255.<\/p>\n<p><strong>Try Other Analogue Sensors<\/strong><br \/>\nUsing the same circuit that you have seen in the previous section, you can connect a lot of other resistive sensors that work in more or less the same way. For examples, see the booklet included in your kits and try\u00a0the force sensor example on page 32.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>INPUTS AND OUTPUTS Revision &#8211; functions from last week: setup() loop() pinMode() digitalWrite() delay() DIGITAL SENSORS Microcontrollers are powerful ways to connect inputs and outputs. Incoming\u00a0voltage can be read from sensors and interpreted as data. And just like with when we use actuators (outputs), sensors\u00a0can also be digital or analog. A button, or a switch [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/744"}],"collection":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=744"}],"version-history":[{"count":10,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/744\/revisions"}],"predecessor-version":[{"id":808,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/744\/revisions\/808"}],"wp:attachment":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}