{"id":1495,"date":"2022-10-13T22:18:06","date_gmt":"2022-10-13T21:18:06","guid":{"rendered":"https:\/\/theroamingworkshop.cloud\/b\/?p=1495"},"modified":"2022-10-24T19:51:47","modified_gmt":"2022-10-24T18:51:47","slug":"analogtouch-touch-detection-with-any-analog-pin-on-arduino","status":"publish","type":"post","link":"https:\/\/theroamingworkshop.cloud\/b\/en\/1495\/analogtouch-touch-detection-with-any-analog-pin-on-arduino\/","title":{"rendered":"AnalogTouch: touch detection with any analog pin on Arduino"},"content":{"rendered":"\n<p>One of the ways to avoid roomy buttons in your Arduino projects is using boards or modules with tactile pins, like some ESP32 modules.<\/p>\n\n\n\n<p>Despite this, the majority of the boards do not include this feature, but reading this entry in esp32.com forum y found something interesting:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.esp32.com\/viewtopic.php?t=23066\">https:\/\/www.esp32.com\/viewtopic.php?t=23066<\/a><\/p>\n\n\n\n<p>With a quick calibration you can use <strong>any analog pin<\/strong> as a tactile sensor. Despite there are a few libraries about this, you really just need a couple of function, so let&#8217;s get on with it!<\/p>\n\n\n\n<div id=\"menu\" style=\"padding:20px 20px 20px 20px; border-left:2px solid darkgrey;\">\n<p style=\"font-weight:bold;\">Content<\/p>\n<\/div>\n<script>\nvar text;\nvar element;\n\nfunction fillmenu(){\n\nfor (let i=0; i<window.document.getElementsByTagName(\"h2\").length; i++){\nelement = window.document.getElementsByTagName(\"h2\")[i];\ntext = \"\u25b9 \"+element.innerHTML;\nvar newelement = document.createElement(\"a\");\nnewelement.innerHTML=text;\nvar postid=window.document.getElementsByTagName(\"article\")[0].id;\nvar url = \"https:\/\/theroamingworkshop.cloud\/b\/?p=\"+postid.substr(5,postid.length)+\"#\"+element.id;\nnewelement.setAttribute(\"href\", url);\nnewelement.appendChild(document.createElement(\"br\"));\nwindow.document.getElementById(\"menu\").appendChild(newelement);\nconsole.log(text);\n}\n}\nif(window.location.href.includes(\"tag\")){\n\n}else{\nwindow.setTimeout(fillmenu,2000);\n}\n\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"components\">Components<\/h2>\n\n\n\n<p>For this project I am using these simple components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1x SparkFun Pro Micro board with ATmega32u4<br>In fact, it's a cheap clone from Aliexpress.<br><a rel=\"noreferrer noopener\" href=\"https:\/\/learn.sparkfun.com\/tutorials\/pro-micro--fio-v3-hookup-guide\/hardware-overview-pro-micro\" target=\"_blank\">https:\/\/learn.sparkfun.com\/tutorials\/pro-micro--fio-v3-hookup-guide\/hardware-overview-pro-micro<\/a><\/li><li>1x Micro USB data cable.<\/li><li>1x LED diode.<\/li><li>1x 1K or 220 Ohm resistance. <\/li><li>3x Jumper cables<\/li><li>1x Protoboard<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wiring\">Wiring<\/h2>\n\n\n\n<p>If using the Pro Micro, this is the pin assignment:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Pro Micro<\/th><th>Componente<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">Puerto Micro USB<\/td><td>Cable USB<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Pin 21 (A3)<\/td><td>Cable puente desconectado<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Pin 20<\/td><td>LED+<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">GND<\/td><td>Resistencia<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>All you need to take into account is to connect the juper wire, that we will use as tactile sensor, into an <strong>analog pin<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/esquema-1.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"572\" src=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/esquema-1.png\" alt=\"\" class=\"wp-image-1489\" srcset=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/esquema-1.png 668w, https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/esquema-1-300x257.png 300w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/a><figcaption>(the board shown is not exactly a Pro Micro)<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"sketch-de-arduino\">Arduino sketch<\/h2>\n\n\n\n<p>You'll find a ready to use Arduino sketch in my github:<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/TheRoam\/TRW_AnalogTouch\/blob\/main\/ProMicro_Blink_AnalogTouch.ino\">https:\/\/github.com\/TheRoam\/TRW_AnalogTouch\/blob\/main\/ProMicro_Blink_AnalogTouch.ino<\/a><\/p>\n\n\n\n<p>It states \"ProMicro\" but it can really be used with any compatible microcontroller board. You just need to select your board in the board manager.<\/p>\n\n\n\n<p>The Pro Micro runs a ATMega32u4, which is compatible with the <strong>Arduino Leonardo<\/strong> board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/at2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"709\" src=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/at2.png\" alt=\"\" class=\"wp-image-1490\" srcset=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/at2.png 848w, https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/at2-300x251.png 300w, https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/at2-768x642.png 768w, https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/at2-676x565.png 676w\" sizes=\"auto, (max-width: 848px) 100vw, 848px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>With this config, compile the script and upload to the board.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"result\">Result<\/h2>\n\n\n\n<p>Open the <strong>Serial Monitor<\/strong>, which should show blank as the debugging text is set to minimum by default.<\/p>\n\n\n\n<p>Now we <strong>touch<\/strong> the free pin of the jumper wire and see how the led lights, and how the \"touch\" is detected in the Serial Monitor.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"240\" style=\"aspect-ratio: 320 \/ 240;\" width=\"320\" autoplay controls loop muted src=\"https:\/\/theroamingworkshop.cloud\/b\/wp-content\/uploads\/2022\/10\/touchled.mp4\" playsinline><\/video><\/figure>\n\n\n\n<p>You'll also see that the script accumulates \"touches\" so this can be used in other programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-code\">The code<\/h2>\n\n\n\n<p>Briefly this is what the program does:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>During <code>setup()<\/code>, start serial communication, activate led pin and fill-up the analog readings array with <code>setTouch<\/code> (Reads 20 values by default).<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>void setup() {\n  \/\/ initialize serial communication at 115200 bits per second:\n  Serial.begin(115200);\n  \/\/enable LED pin\n  pinMode(LP,OUTPUT);\n  setTouch(TP);\n}<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Next we run the <code>loop()<\/code> function, where the readings are updated at the start of each loop.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>setTouch(TP);\n\/\/------loop continues------\/\/\nvoid setTouch(int tp){\n  \/\/fill up the array\n  for(int a=0;a&lt;VALUES;a++){\n    mean&#91;a]=analogRead(tp);\n    delay(10);\n  }\n}<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Check if debugging is minimal or extended, and run the main function (the only difference is the amount of debugging messages).<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/run check\n  if(out==1){\n    extended(TP);\n  }else if(out==0){\n    minimal(TP);\n  }<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Then, the main function:<ol><li>Runs through the reading array,<\/li><li>Obtains the mean value,<\/li><li>Gets a new reading,<\/li><li>Calculates the difference between new reading and average,<\/li><li>If the difference is bigger than the REFV voltage drop value: \"touch\" is identified, it's printed in Serial Monitor, touch counter is increased and led is lit.<\/li><li>Restore average to 0, turn led off and return \"touch\" variable.<\/li><\/ol><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>int minimal(int tp){\n  \/\/start reading values\n  for(int i=0;i&lt;VALUES;i++){\n    \/\/get average of array\n    for(int j=0;j&lt;VALUES;j++){\n      avg+=mean&#91;j];\n    }\n    \n    avg=avg\/VALUES;\n\n    \/\/read current value\n    fin=analogRead(tp);\n    \n    \/\/compare average with current value\n    if((avg-fin)&gt;REFV){\n      Serial.println(\"----------  T   O    U    C    H  ----------\");\n      \/\/increase counter\n      touch++;\n      \/\/print touch count\n      Serial.print(\"T: \");\n      Serial.println(touch);\n      \/\/light led\n      ledON(LP);\n      \/\/wait 1 second after touch detected\n      delay(1000);\n    }\n    \/\/restart counter\n    avg=0;\n    \/\/restart led\n    ledOFF(LP);\n    delay(VALINTERVAL);\n  }\n  return touch;\n}<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>After this the loop is restarted, going back to point 2.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"other-configurations\">Other configurations<\/h2>\n\n\n\n<p>The script includes different configuration variables that we find under the '#define's:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>OUT<\/code>: defines the amount of debugging text. Value 0: minimal; Value 1: extended.<\/li><li><code>VALUES<\/code>: analog readings array size.<\/li><li><code>TOUCHPIN<\/code>: define the analog pin number used for touch.<\/li><li><code>LEDPIN<\/code>: define the pin number used for led.<\/li><li><code>VALINTERVAL<\/code>: evaluation interval in milliseconds.<\/li><li><code>REFV<\/code>: voltage drop value (in millivolts) which identifies a \"touch\".<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ OUTPUT verbosity\n\/\/ 1: extended --&gt; Prints ADC array, mean value, touch count and touch message with touch count\n\/\/ 0: minimal  --&gt; Prints touch message with touch count\n\n#define OUT 0\n\n\/\/ comparison variables\n#define VALUES 20       \/\/values for mean array\n#define TOUCHPIN 3      \/\/touch pin\n#define LEDPIN 20       \/\/led pin\n#define VALINTERVAL 100 \/\/evaluation interval\n#define REFV 80         \/\/reference voltage drop in mV<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"calibration\">Calibration<\/h2>\n\n\n\n<p>Changing the configuration you can calibrate the script for different boards. From what I've tested, this is a good sequence:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Set a high <strong>REFV<\/strong>, for example 100.<\/li><li>Run the extended script with <strong>OUT 1<\/strong><\/li><li>Open <strong>Serial Monitor<\/strong> and have a look at the average values for a while (e.g.: 230)<\/li><li>Now <strong>touch the jumper wire<\/strong> and see what the values are now (e.g.: 170)<\/li><li>Make the <strong>difference between the average at 3. and the average at 4<\/strong> (230-170=60).<\/li><li><strong>Set your REFV<\/strong> to this value (60) and play around it.<ul><li>For more <strong>sensitivity<\/strong>, set it a bit lower (50)<\/li><li>If there is <strong>detection with no touch<\/strong>, increase it (80)<\/li><li>Because of CPU load, <strong>REFV value can vary<\/strong> if OUT 1 or OUT 0<\/li><\/ul><\/li><\/ol>\n\n\n\n<p>Found it useful? Any doubts? Leave your comment on \ud83d\udc26 Twitter!<\/p>\n\n\n\n<p>\ud83d\udc26 <code><a rel=\"noreferrer noopener\" href=\"https:\/\/twitter.com\/roamingworkshop\" target=\"_blank\">@RoamingWorkshop<\/a><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the ways to avoid roomy buttons in your Arduino projects is using boards or modules with tactile pins, like some ESP32 modules. Despite this, the majority of the boards do not include this feature, but reading this entry in esp32.com forum y found something interesting: https:\/\/www.esp32.com\/viewtopic.php?t=23066 With a quick calibration you can use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1488,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-1495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","post-preview"],"_links":{"self":[{"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/posts\/1495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/comments?post=1495"}],"version-history":[{"count":3,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/posts\/1495\/revisions"}],"predecessor-version":[{"id":1575,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/posts\/1495\/revisions\/1575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/media\/1488"}],"wp:attachment":[{"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/media?parent=1495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/categories?post=1495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroamingworkshop.cloud\/b\/wp-json\/wp\/v2\/tags?post=1495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}