{"id":63,"date":"2013-10-24T16:34:24","date_gmt":"2013-10-24T16:34:24","guid":{"rendered":"https:\/\/arduino-developer.com\/arduino\/?p=63"},"modified":"2023-01-18T12:25:39","modified_gmt":"2023-01-18T12:25:39","slug":"analog-inputs","status":"publish","type":"post","link":"https:\/\/arduino-developer.com\/arduino\/programming\/analog-inputs\/analog-inputs","title":{"rendered":"Analog Inputs"},"content":{"rendered":"\n<p>10 bit AtoD&nbsp;returning integers from 0 to 1023.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Read Analog Input<\/h4>\n\n\n\n<p>There is no need to set the pin up as an analog input, just start reading it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  int MyAnalogValue = analogRead(A3);    \/\/Read 10-bit (0 to 1023) analog input value (causes stall for approx 100uS)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting The Analog Reference Voltage<\/h4>\n\n\n\n<p>Use the analogReference() function;<\/p>\n\n\n\n<p>DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)<br>INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)<br>INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)<br>INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)<br>EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using Analog Pins as GPIO<\/h4>\n\n\n\n<p>The analog pins also have all the functionality of general purpose input\/output (GPIO) pins (the same as digital pins 0 &#8211; 13).<\/p>\n\n\n\n<p>Use&nbsp;the aliases A0 (for analog input 0), A1, etc. E.g this&nbsp;would set analog pin 0 to an output, and to set it HIGH:<\/p>\n\n\n\n<p>pinMode(A0, OUTPUT);<br>digitalWrite(A0, HIGH);<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Special Arduinos<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Arduino Due, Zero and MKR Family<\/h5>\n\n\n\n<p>Use analogReadResolution() to set the resolution you want to use.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example function with averaging of sampled values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/****************************************\n\/\/****************************************\n\/\/********** READ ANALOG INPUTS **********\n\/\/****************************************\n\/\/****************************************\nvoid read_analog_inputs (void)\n{\n  static uint8_t AnalogInputSampleCount = 0;\n  static uint16_t AnalogInputA2Buffer = 0;\n  \n  \n  \/\/Called every 10mS\n\n  AnalogInputA2Buffer += analogRead(A2);    \/\/Read 10-bit (0 to 1023) analog input value (causes stall for approx 100uS)\n  AnalogInputSampleCount++;\n\n  if (AnalogInputSampleCount >= 8)\n  {\n    \/\/----- SAMPLE PERIOD COMPLETE -----\n  \n    \/\/----- INPUT A2 -----\n    \/\/1V = 1A\n    \/\/0-1023 @ 5V VCC = 204.6 = 1A\n    \/\/0.2046 = 1mA\n    CurrentSense_mA = (AnalogInputA2Buffer >> 3) \/ 0.2046;    \/\/ >> 3 for \/8\n\n    \/\/Reset for next buffer sample \n    AnalogInputA2Buffer = 0;\n    AnalogInputSampleCount = 0;\n  }\n\n\n#ifdef __DEBUG\n  static uint8_t DebugOutputCount = 200;\n\n  DebugOutputCount--;\n  if (DebugOutputCount == 0)\n  {\n    DebugOutputCount = 200;\n\n    Serial.print(\"Current sense: \");\n    Serial.print(CurrentSense_mA, DEC);\n    Serial.println(\"mA\");\n  }\n#endif\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>10 bit AtoD&nbsp;returning integers from 0 to 1023. Read Analog Input There is no need to set the pin up as an analog input, just start reading it: Setting The Analog Reference Voltage Use the analogReference() function; DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-analog-inputs"],"_links":{"self":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":7,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":676,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/63\/revisions\/676"}],"wp:attachment":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}