{"id":576,"date":"2014-07-07T14:18:49","date_gmt":"2014-07-07T18:18:49","guid":{"rendered":"https:\/\/itp.nyu.edu\/physicalcomputing\/?page_id=576"},"modified":"2022-06-23T07:28:28","modified_gmt":"2022-06-23T11:28:28","slug":"analog-input","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/lessons\/analog-input\/","title":{"rendered":"Analog Input"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is an introduction to basic analog&nbsp;input on a microcontroller. In order to get the most out of it, you should know something about the following concepts.&nbsp; You can check how to do so in the links below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/electronics\/electricity-the-basics\/\">Electrical circuits<\/a><\/li><li><a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/electronics\/electricity-the-basics\/\">What a microcontroller is\u00a0and what it can do<\/a><\/li><li><a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/digital-input-output\/\" target=\"_blank\" rel=\"noreferrer noopener\">Digital input and output on a microcontroller<\/a><\/li><\/ul>\n\n\n\n<p>These video links will help in understanding analog input:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/372104092\" target=\"_blank\">Analog Input 1<\/a><\/li><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/372103855\" target=\"_blank\">Analog Input 2<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Analog_Input\"><\/span>Analog Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While a digital input to a microcontroller can tell you about discrete changes in the physical world, such as whether the cat is on the mat, or the cat is off the mat, there are times when this is not enough. Sometimes you&nbsp;want to know how fat the cat on the mat is. In order to know this, you&#8217;d&nbsp;need to be able to measure the force the cat exerts on the mat as a variable quantity. When you&nbsp;want to measure variably changing conditions like this, you need analog inputs. An <strong>analog input<\/strong> to a microcontroller is an&nbsp;input that can read a variable voltage, typically from 0 volts to the maximum voltage that powers the microcontroller itself.<\/p>\n\n\n\n<p>Many transducers are available to convert various changing conditions to changing electrical quantities. There are photocells that convert the amount of light falling on them to a varying resistance; flex sensors that change resistance as they are bent; Force-sensitive resistors (FSRs) that change resistance based on a changing force applied to the surface of the sensor; thermistors that change resistance in response to changing heat; and many more.<\/p>\n\n\n\n<p><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380345304#t=0m40s\" target=\"_blank\">Related video:&nbsp;Resistors, variable resistors, and photocells<\/a><\/p>\n\n\n\n<p>In order to read these changing resistances, you put them in a circuit and pass a current through them, so that you can see the changing voltage that results. There are a few variations on this circuit. The simplest is called a&nbsp;<strong>voltage divider<\/strong>. Because the two resistors are in series voltage at the input to the microcontroller is proportional to the ratio of the resistors. If they are equal, then the input voltage is half the total voltage. So in the circuit in Figure 1, if the variable resistor changes (for example, if it&#8217;s a flex sensor being bent), then the voltage at the input changes. &nbsp;The fixed resistor&#8217;s value is generally chosen to complement the variable resistor&#8217;s range. For example, if you have a variable resistor that&#8217;s 10-20 kilohms, you might choose a 10 kilohm fixed resistor.<\/p>\n\n\n\n<figure class=\"wp-block-image alignnone wp-image-1675 size-full is-style-longdesc\"><img loading=\"lazy\" decoding=\"async\" width=\"178\" height=\"222\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/analog_in_schem.png\" alt=\"analog in schematic\" class=\"wp-image-1675\"\/><figcaption>Figure 1. voltage divider with a variable resistor and a fixed resistor<\/figcaption><\/figure>\n\n\n\n<p>In Figure 2, you use a <strong>potentiometer,&nbsp;<\/strong> which is a&nbsp;variable resistor with three connections. The center of the potentiometer, called the wiper, &nbsp;is connected to the microcontroller. The other two sides are attached to power and ground. The wiper can move from one end of the resistor to the other. In effect, it divides the resistor into two resistors and measures the resistance at the point where they meet, just like a voltage divider.<\/p>\n\n\n\n<p>Related videos:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/372104092#t=3m47s\" target=\"_blank\">Connecting the potentiometer<\/a><\/li><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380345304#t=2m23s\" target=\"_blank\">Potentiometers&#8217; Symbol<\/a><\/li><li><a rel=\"noopener noreferrer\" href=\"https:\/\/vimeo.com\/380172633#t=3m15s\" target=\"_blank\">Measure a potentiometer&#8217;s variable resistance<\/a><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image alignnone size-full wp-image-1682 is-style-longdesc\"><img loading=\"lazy\" decoding=\"async\" width=\"166\" height=\"142\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/potentiometer_schem.png\" alt=\"potentiometer schematic\" class=\"wp-image-1682\"\/><figcaption>Figure 2. potentiometer schematic<\/figcaption><\/figure>\n\n\n\n<p>Since a microcontroller&#8217;s inputs can read only two values (typically 0 volts or the controller&#8217;s supply voltage), an analog input pin\u00a0needs an extra component to read this changing, or <strong>analog<\/strong> voltage, and convert it to a digital form. An<strong> analog-to-digital converter (ADC)<\/strong> is a device that does this. It reads a changing input voltage and converts it to a binary value, which a microcontroller can then store in memory.Many microcontrollers have ADCs built in to them. Arduino\u00a0boards have an ADC attached to the <a rel=\"noopener noreferrer\" href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/AnalogInputPins\" target=\"_blank\">analog input pins<\/a>.<\/p>\n\n\n\n<p>The ADC in the Arduino can read the input voltage at a resolution of 10 bits. That&#8217;s a range of 1024 points. If the input voltage range (for example, on the Uno) is 0 to 5 volts, that means that the smallest change it can read is 5\/1024, or 0.0048 Volts. For a 3.3V board like the Nano 33 IoT, it&#8217;s 0.0029 volts. When you take a reading with the ADC using the <tt><a rel=\"noreferrer noopener\" href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/analog-io\/analogread\/\" target=\"_blank\">analogRead()<\/a><\/tt> command, the microcontroller stores the result in\u00a0memory. It takes an <tt>int<\/tt> type variable to store this, because a <tt>byte<\/tt> is not big enough to store the 10 bits of an ADC reading. A byte\u00a0can hold only 8 bits, or a range from 0 to 255.<\/p>\n\n\n\n<p>The command in Arduino\u00a0is the <tt>analogRead()<\/tt> command, and it looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nsensorReading = analogRead(pin);\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><em>Pin<\/em> is the analog input pin you are using;<\/li><li><em>sensorReading<\/em>&nbsp;is an integer variable containing the result from the ADC.<\/li><\/ul>\n\n\n\n<p>The number produced in sensorReading\u00a0is will be between 0 and 1023. Its maximum may be less, depending on the\u00a0circuit you use. A potentiometer will give the full range, but a voltage divider for a variable resistor like a force sensing resistor or flex sensor, where one of the resistors is fixed, will not.<\/p>\n\n\n\n<p>The analog inputs on an Arduino (and in fact, on most microcontrollers), are all connected to the same ADC circuit, so when the microcontroller has to switch the ADC&#8217;s input from one pin to another when you try to read two pins one after another. If you read them too fast, you can get unstable readings.\u00a0You can also get more reliable readings by introducing a small delay after you take an analog reading. This allows the ADC time to stabilize before you take your next reading.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to read three analog inputs with minimal delay and maximum stability:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n sensorOne = analogRead(A0);\n delay(1);\n sensorTwo = analogRead(A1);\n delay(1);\n sensorOne = analogRead(A2);\n delay(1);\n<\/pre><\/div>\n\n\n<p>Analog and <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/digital-input-output\/\" target=\"_blank\" rel=\"noreferrer noopener\">digital<\/a> inputs are the two simplest ways that a microcontroller reads changing sensor voltage inputs.\u00a0Once you&#8217;ve understood these two, you&#8217;re ready to use a variety of sensors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This is an introduction to basic analog&nbsp;input on a microcontroller. In order to get the most out of it, you should know something about the following concepts.&nbsp; You can check how to do so in the links below: Electrical circuits What a microcontroller is\u00a0and what it can do Digital input and output on a &hellip; <a href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/analog-input\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Analog Input&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":13,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[20,46,11,21,52,54,26,7],"tags":[],"class_list":["post-576","page","type-page","status-publish","hentry","category-analog","category-arduino-fundamentals","category-code","category-components","category-lesson","category-microcontrollers","category-programming","category-sensors"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/576"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=576"}],"version-history":[{"count":28,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/576\/revisions"}],"predecessor-version":[{"id":9840,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/576\/revisions\/9840"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}