{"id":52223,"date":"2026-11-01T10:50:41","date_gmt":"2026-11-01T10:50:41","guid":{"rendered":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/"},"modified":"2026-11-01T10:50:41","modified_gmt":"2026-11-01T10:50:41","slug":"digital-and-analog","status":"publish","type":"siratek_tutorial","link":"https:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/","title":{"rendered":"Digital and Analog, and When Each Applies"},"content":{"rendered":"<body><p><\/p>\n<h2>What this covers<\/h2>\n<p>By now you have used all four of them: <code>digitalRead()<\/code>, <code>digitalWrite()<\/code>, <code>analogRead()<\/code> and <code>analogWrite()<\/code>. This page is about what those words actually mean on an Uno, because the naming is misleading in two places and both of them cause real confusion later.<\/p>\n<p>Specifically: why <code>analogRead()<\/code> gives you 0 to 1023, why <code>analogWrite()<\/code> gives you 0 to 255, why those two ranges have nothing to do with each other, and why only one of the four is genuinely analog.<\/p>\n<h2>Before you begin<\/h2>\n<p>Nothing to wire. A potentiometer on <code>A0<\/code> and an LED on pin 9 will let you try everything here, and both are already built if you have followed the earlier tutorials.<\/p>\n<h2>Digital: two states, and a gap between them<\/h2>\n<p>A digital pin deals in two values, and the chip decides which one it is seeing by comparing the voltage against two thresholds.<\/p>\n<p>On a 5 V ATmega328P, roughly:<\/p>\n<ul>\n<li>Below about <strong>1.5 V<\/strong>, the pin reads <code>LOW<\/code>.<\/li>\n<li>Above about <strong>3.0 V<\/strong>, the pin reads <code>HIGH<\/code>.<\/li>\n<li><strong>Between them is undefined.<\/strong> The pin reads one or the other, and which one is not something you can rely on.<\/li>\n<\/ul>\n<p>That middle band is the part worth remembering, because it explains the floating-input problem from <em>Read a Push Button<\/em>. An unconnected input is not LOW. It sits somewhere in that undefined band, picking up interference, and reads whatever it likes. This is also why a long sensor cable with a weak signal can produce readings that change when you walk past.<\/p>\n<p><code>digitalWrite()<\/code> is the other direction, and it is genuinely two-valued: the pin is connected either to 5 V or to ground, through a transistor, with nothing in between.<\/p>\n<figure class=\"siratek-tutorial-figure\"><img decoding=\"async\" src=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/digital-and-analog-ranges.svg\" alt=\"The logic threshold bands with the undefined gap between them, and the two unrelated number ranges\" loading=\"lazy\"><figcaption>0 to 1023 comes from a 10-bit converter; 0 to 255 from 8-bit timers. The two are unrelated.<\/figcaption><\/figure>\n<h2>Analog in: 1024 steps of a voltage<\/h2>\n<p><code>analogRead()<\/code> is the only one of the four that is truly analog, and it works by comparison rather than measurement.<\/p>\n<p>The ATmega328P has one analog-to-digital converter, shared between the six analog pins by a switch. It compares the pin\u2019s voltage against a reference, which by default is the board\u2019s own 5 V supply, and reports the result as a whole number from <strong>0 to 1023<\/strong>.<\/p>\n<p>That range is 1024 values because the converter is <strong>10 bits<\/strong> wide, and two to the power of ten is<\/p>\n<ol>\n<li>There is nothing special about the number beyond that.<\/li>\n<\/ol>\n<p>So each step is 5 V divided by 1023, a little under <strong>5 millivolts<\/strong>. That is the finest distinction the Uno can make, and it sets a hard floor on what any sensor reading through this converter can resolve.<\/p>\n<h3>The reference is the thing to watch<\/h3>\n<p>The reading is a <em>ratio<\/em> against the reference voltage, not a measurement in volts. The conversion is:<\/p>\n<pre><code>volts = reading * 5.0 \/ 1023.0<\/code><\/pre>\n<p>Which is only true if your board\u2019s 5 V rail is really at 5 V. On USB power it is often 4.7 or 4.8, and it sags further under load. So a reading that looks like a stable measurement can drift as a motor starts, a backlight comes on, or the USB cable changes.<\/p>\n<p>For threshold work this does not matter, because a potentiometer or an LDR divider is itself ratiometric: if the supply sags, both the divider output and the reference sag together and the reading stays put. For anything you want in real volts, it matters a great deal, and the answer is <code>analogReference(INTERNAL)<\/code>, which switches to a 1.1 V reference built into the chip. That is far more stable, at the cost of only being able to measure up to 1.1 V.<\/p>\n<h3>It is not instant<\/h3>\n<p>Each conversion takes roughly 100 microseconds. Ten thousand readings a second is the rough ceiling, and that is before your sketch does anything with them. Usually irrelevant; occasionally the reason a loop is slower than expected.<\/p>\n<h2>Analog out: a fast switch pretending<\/h2>\n<p><code>analogWrite()<\/code> is the misleading one. <strong>There is no analog output on an Uno.<\/strong> The pin is still only ever at 5 V or at 0 V.<\/p>\n<p>What <code>analogWrite()<\/code> does is switch it between the two, quickly, and vary the proportion of time spent high. An LED averages that out as brightness because your eye cannot follow the switching. A motor averages it mechanically. A capacitor would average it electrically.<\/p>\n<p>That is pulse width modulation, and <em>Dimming and Speed Control with PWM<\/em> covers it properly, including which pins can do it and which timers they share.<\/p>\n<p>The thing to take from here is why the ranges differ. <code>analogWrite()<\/code> takes <strong>0 to 255<\/strong> because the timer counters doing the switching are <strong>8 bits<\/strong> wide, and two to the eighth is 256. It is the same kind of reason as the 1024, about a different piece of hardware.<\/p>\n<p><strong>So the two ranges are unrelated, and this is the mistake to avoid:<\/strong><\/p>\n<pre><code>\/\/ Wrong. 1023 goes in, 255 is the maximum, so everything above 255 is clipped.\nint reading = analogRead(A0);\nanalogWrite(ledPin, reading);<\/code><\/pre>\n<pre><code>\/\/ Right.\nint reading = analogRead(A0);\nanalogWrite(ledPin, map(reading, 0, 1023, 0, 255));<\/code><\/pre>\n<p>Or, since the scaling is exactly four, <code>reading \/ 4<\/code> does the same thing and is cheaper. <code>map()<\/code> is clearer and survives you changing your mind about the range, which is usually worth more than the few microseconds.<\/p>\n<h2>Which pins can do what<\/h2>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>Digital pins <code>D0<\/code> to <code>D13<\/code><\/th>\n<th>Analog pins <code>A0<\/code> to <code>A5<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>digitalRead()<\/code><\/td>\n<td>Yes<\/td>\n<td><strong>Yes<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>digitalWrite()<\/code><\/td>\n<td>Yes<\/td>\n<td><strong>Yes<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>analogRead()<\/code><\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td><code>analogWrite()<\/code><\/td>\n<td>Only <code>3, 5, 6, 9, 10, 11<\/code><\/td>\n<td>No<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The two bold entries surprise people and are genuinely useful. <strong>The analog pins are ordinary digital pins as well.<\/strong> If you run out of digital pins, <code>A0<\/code> to <code>A5<\/code> will take a button or an LED exactly as <code>D2<\/code> would, using the names <code>A0<\/code> and so on:<\/p>\n<pre><code>pinMode(A0, INPUT_PULLUP);\nif (digitalRead(A0) == LOW) {\n  \/\/ button pressed\n}<\/code><\/pre>\n<p>That is six more pins than most people realise they have. The catch is that <code>A4<\/code> and <code>A5<\/code> are the I2C bus, so a display or sensor on the bus takes them.<\/p>\n<h2>Choosing between them<\/h2>\n<p>When a part gives you a choice, the question is what you are actually asking.<\/p>\n<ul>\n<li><strong>\u201cHas something happened?\u201d<\/strong> is digital. A button, a tilt sensor, a PIR module.<\/li>\n<li><strong>\u201cHow much?\u201d<\/strong> is analog. A potentiometer, an LDR, a soil probe.<\/li>\n<\/ul>\n<p>Many sensor modules offer both: an <code>AO<\/code> pin giving the raw level and a <code>DO<\/code> pin giving a yes-or-no against a threshold set by a trimmer on the board. The flame sensor, the sound sensor and the soil probe all work this way.<\/p>\n<p>Prefer the analog pin while you are developing, even if the project only needs a yes or no. The analog reading tells you what is really happening and lets you choose the threshold from measured numbers. The digital pin hides all of that behind a screw you have to adjust by trial and error.<\/p>\n<p>Switch to the digital pin at the end, if you need the Arduino pin back.<\/p>\n<h2>Check yourself<\/h2>\n<ol>\n<li>A sensor reads 512. What voltage is on the pin, and how confident should you be in that answer?<\/li>\n<li>Why does <code>analogWrite(pin, 1023)<\/code> not give you full brightness in a useful way?<\/li>\n<li>An input pin with nothing connected reads HIGH sometimes and LOW other times. What is happening?<\/li>\n<li>You have used every digital pin and need one more button. What is still available?<\/li>\n<\/ol>\n<p>Answers in order: about 2.5 V, assuming the 5 V rail really is at 5 V, which on USB it often is not. Because <code>analogWrite()<\/code> saturates at 255; the call is clipped, so 1023 and 255 do the same thing and the rest of your range does nothing. The pin is floating in the undefined band between the thresholds and picking up interference; use <code>INPUT_PULLUP<\/code>. <code>A0<\/code> to <code>A5<\/code> work as digital pins, less any taken by I2C.<\/p>\n<h2>Troubleshooting<\/h2>\n<table>\n<thead>\n<tr>\n<th>Symptom<\/th>\n<th>Likely cause<\/th>\n<th>Check or fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>LED brightness jumps straight to full<\/td>\n<td>Feeding a 0 to 1023 value into <code>analogWrite()<\/code><\/td>\n<td><code>map()<\/code> it to 0 to 255<\/td>\n<\/tr>\n<tr>\n<td><code>analogRead()<\/code> on a digital pin returns nonsense<\/td>\n<td>Only <code>A0<\/code> to <code>A5<\/code> have the converter<\/td>\n<td>Move the sensor<\/td>\n<\/tr>\n<tr>\n<td><code>analogWrite()<\/code> does nothing on pin 7<\/td>\n<td>Not a PWM pin<\/td>\n<td>Use 3, 5, 6, 9, 10 or 11<\/td>\n<\/tr>\n<tr>\n<td>Readings drift when a motor or backlight runs<\/td>\n<td>The 5 V reference is sagging<\/td>\n<td>Expect it; or use <code>analogReference(INTERNAL)<\/code><\/td>\n<\/tr>\n<tr>\n<td>Voltage calculation is consistently a few percent out<\/td>\n<td>Rail is not exactly 5 V<\/td>\n<td>Measure the rail and use the real figure<\/td>\n<\/tr>\n<tr>\n<td>Unconnected input reads randomly<\/td>\n<td>Floating, in the undefined band<\/td>\n<td><code>INPUT_PULLUP<\/code>, or an external resistor<\/td>\n<\/tr>\n<tr>\n<td>Sensor\u2019s digital output never triggers<\/td>\n<td>Threshold trimmer set wrong<\/td>\n<td>Read the analog pin and set it from real numbers<\/td>\n<\/tr>\n<tr>\n<td>Analog readings wobble by a few counts<\/td>\n<td>Normal converter noise<\/td>\n<td>Average several readings<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Next steps<\/h2>\n<ul>\n<li><strong>Easier first:<\/strong> <em>Read a Potentiometer<\/em>, which is <code>analogRead()<\/code> in its simplest form.<\/li>\n<li><strong>Related:<\/strong> <em>Dimming and Speed Control with PWM<\/em>, for what <code>analogWrite()<\/code> is really doing.<\/li>\n<li><strong>Later:<\/strong> <em>Reading a Datasheet<\/em>, for finding a part\u2019s own limits rather than the board\u2019s.<\/li>\n<\/ul>\n<h2>Sources and attribution<\/h2>\n<p>All text and code in this tutorial are original Siratek work.<\/p>\n<ul>\n<li>ATmega328P datasheet for the input threshold voltages, the 10-bit converter, conversion time and the 8-bit timer resolution behind <code>analogWrite()<\/code>.<\/li>\n<li>Arduino core documentation for <code>analogReference()<\/code> and for which pins support PWM.<\/li>\n<\/ul>\n<p>Logic threshold voltages are given as approximate figures, because the datasheet states them as fractions of the supply voltage rather than fixed values. No third-party tutorial text, diagrams, photographs or code has been reproduced here.<\/p>\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>By now you have used all four of them: digitalRead(), digitalWrite(), analogRead() and analogWrite(). This page is about what those words actually mean on an Uno, because the naming is misleading in two places and both of them cause real&#8230;<\/p>\n","protected":false},"featured_media":0,"template":"","siratek_tutorial_level":[],"class_list":["post-52223","siratek_tutorial","type-siratek_tutorial","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Digital and Analog, and When Each Applies - siratek<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Digital and Analog, and When Each Applies - siratek\" \/>\n<meta property=\"og:description\" content=\"By now you have used all four of them: digitalRead(), digitalWrite(), analogRead() and analogWrite(). This page is about what those words actually mean on an Uno, because the naming is misleading in two places and both of them cause real...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/\" \/>\n<meta property=\"og:site_name\" content=\"siratek\" \/>\n<meta property=\"og:image\" content=\"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/digital-and-analog-ranges.svg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/\",\"url\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/\",\"name\":\"Digital and Analog, and When Each Applies - siratek\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/digital-and-analog-ranges.svg\",\"datePublished\":\"2026-11-01T10:50:41+00:00\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/#primaryimage\",\"url\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/digital-and-analog-ranges.svg\",\"contentUrl\":\"https:\\\/\\\/siratek.net\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/digital-and-analog-ranges.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/digital-and-analog\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/siratek.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/siratek.net\\\/index.php\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Digital and Analog, and When Each Applies\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/siratek.net\\\/#website\",\"url\":\"https:\\\/\\\/siratek.net\\\/\",\"name\":\"siratek\",\"description\":\"Your Best Electronic Retail Shop\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/siratek.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Digital and Analog, and When Each Applies - siratek","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/","og_locale":"en_US","og_type":"article","og_title":"Digital and Analog, and When Each Applies - siratek","og_description":"By now you have used all four of them: digitalRead(), digitalWrite(), analogRead() and analogWrite(). This page is about what those words actually mean on an Uno, because the naming is misleading in two places and both of them cause real...","og_url":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/","og_site_name":"siratek","og_image":[{"url":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/digital-and-analog-ranges.svg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/","url":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/","name":"Digital and Analog, and When Each Applies - siratek","isPartOf":{"@id":"https:\/\/siratek.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/#primaryimage"},"image":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/#primaryimage"},"thumbnailUrl":"http:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/digital-and-analog-ranges.svg","datePublished":"2026-11-01T10:50:41+00:00","breadcrumb":{"@id":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/#primaryimage","url":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/digital-and-analog-ranges.svg","contentUrl":"https:\/\/siratek.net\/wp-content\/uploads\/2026\/11\/digital-and-analog-ranges.svg"},{"@type":"BreadcrumbList","@id":"http:\/\/siratek.net\/index.php\/tutorials\/digital-and-analog\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/siratek.net\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"https:\/\/siratek.net\/index.php\/tutorials\/"},{"@type":"ListItem","position":3,"name":"Digital and Analog, and When Each Applies"}]},{"@type":"WebSite","@id":"https:\/\/siratek.net\/#website","url":"https:\/\/siratek.net\/","name":"siratek","description":"Your Best Electronic Retail Shop","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/siratek.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial"}],"about":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/types\/siratek_tutorial"}],"version-history":[{"count":0,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial\/52223\/revisions"}],"wp:attachment":[{"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/media?parent=52223"}],"wp:term":[{"taxonomy":"siratek_tutorial_level","embeddable":true,"href":"https:\/\/siratek.net\/index.php\/wp-json\/wp\/v2\/siratek_tutorial_level?post=52223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}