{"id":250,"date":"2024-10-20T17:55:25","date_gmt":"2024-10-20T17:55:25","guid":{"rendered":"https:\/\/cascajolabs.es\/?p=250"},"modified":"2024-10-20T17:57:57","modified_gmt":"2024-10-20T17:57:57","slug":"lcd-7-segments-3-digits-with-an-esp32","status":"publish","type":"post","link":"https:\/\/cascajolabs.es\/?p=250","title":{"rendered":"LCD 7 segments 3 digits with an ESP32"},"content":{"rendered":"\n<p>I recently got some tiny LCD screens that have 3 digits with 7 segments, and I&#8217;ve been playing around with them to understand how they work better.<\/p>\n\n\n\n<p>The first thing I noticed compared to 7-segment LED displays is that to show a number, you just need to &#8216;turn on&#8217; the segment you want and keep it lit. But with an LCD screen, it\u2019s a bit different. You first have to create a voltage difference with the COM and then &#8216;flip it&#8217;.<\/p>\n\n\n\n<p>I found a pretty good explanation of how this works here:  <\/p>\n\n\n\n<p><a href=\"https:\/\/forum.arduino.cc\/t\/driving-a-salvaged-lcd-directly-from-arduino\/51558\">https:\/\/forum.arduino.cc\/t\/driving-a-salvaged-lcd-directly-from-arduino\/51558<\/a><\/p>\n\n\n\n<p>Even though my LCD screen didn\u2019t come with any specs or part numbers, I managed to find some info online for a similar LCD from a different seller, and it matched mine.<\/p>\n\n\n\n<p>As you can see in the code, I have connected the pins to the same Dx on my Arduino: P1-D1, P2-D2\u2026<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"707\" src=\"https:\/\/cascajolabs.es\/wp-content\/uploads\/2024\/10\/lcd_3d-1024x707.jpg\" alt=\"\" class=\"wp-image-251\" style=\"width:737px;height:auto\" srcset=\"https:\/\/cascajolabs.es\/wp-content\/uploads\/2024\/10\/lcd_3d-1024x707.jpg 1024w, https:\/\/cascajolabs.es\/wp-content\/uploads\/2024\/10\/lcd_3d-300x207.jpg 300w, https:\/\/cascajolabs.es\/wp-content\/uploads\/2024\/10\/lcd_3d-768x530.jpg 768w, https:\/\/cascajolabs.es\/wp-content\/uploads\/2024\/10\/lcd_3d.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>I used an Arduino Nano ESP32 because it\u2019s convenient, and here\u2019s the sample code I wrote to test it out and a short video:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LCD 3 digits 7 segments small | Arduino Nano ESP32\" width=\"750\" height=\"422\" src=\"https:\/\/www.youtube.com\/embed\/6N3aqJwLt20?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h>\nint counter = 0;\nint number = 1;\nunsigned long lasttime = 0; \nconst unsigned long interval =200; \/\/ 200ms\n\nint digit1&#91;10]&#91;4]&#91;6] = { \/\/COM1-COM2.COM3-COM4 1-6\n    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}}, \/\/ 0\n    {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1}}, \/\/ 1\n    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 1}}, \/\/ 2\n    {{1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 0, 1}}, \/\/ 3\n    {{1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 0}}, \/\/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 1, 1, 1}, {0, 1, 1, 1, 1, 1}}, \/\/ 7\n    {{1, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}, \/\/ 8\n    {{1, 0, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1}}  \/\/ 9\n};\n\nvoid activateCOM(int index);\nvoid deactivateCOM(int index);\n\n\nconst int COM1 = 10, COM2 = 9, COM3 = 8, COM4 = 7; \/\/ COM pins\n\nvoid setup() {\n    \/\/ Pin config\n    for (int i = 0; i &lt; 11; i++) { \n        pinMode(i, OUTPUT);\n        digitalWrite(i, HIGH);\n    }\n    delay(1000);\n}\n\nvoid loop() {\n    if (millis() - lasttime >= interval) {\n        lasttime = millis();\n        counter++;\n        if (counter > 999) counter = 0;\n    }\n\n    int hundreds = counter \/ 100;\n    int tens = (counter \/ 10) % 10;\n    int units = counter % 10;\n    for (int i = 0; i &lt; 4; i++) {\n        activateCOM(i); \n        for (int j=0;j&lt;6;j++)\n        {\n            if(number==1)\n            {\n                digitalWrite(j+1,digit1&#91;hundreds]&#91;i]&#91;j]);\n            }\n            else if(number==2)\n            {\n                digitalWrite(j+1,digit2&#91;tens]&#91;i]&#91;j]);\n            }\n            else if(number==3)\n            {\n                digitalWrite(j+1,digit3&#91;units]&#91;i]&#91;j]);\n            }\n        }\n        delay(1);\n        deactivateCOM(i);\n    for (int j=0;j&lt;6;j++)\n        {\n            if(number==1)\n            {\n                digitalWrite(j+1,!digit1&#91;hundreds]&#91;i]&#91;j]);\n            }\n            else if(number==2)\n            {\n                digitalWrite(j+1,!digit2&#91;tens]&#91;i]&#91;j]);\n            }\n            else if(number==3)\n            {\n                digitalWrite(j+1,!digit3&#91;units]&#91;i]&#91;j]);\n            }\n        }\n        \n    }\n    number++;\n    if (number>3){number = 1;}\n\n}\n\nvoid activateCOM(int index) {\n      if (index == 0) { \/\/ COM1\n        pinMode(COM1, OUTPUT); \n        digitalWrite(COM1, HIGH);\n        pinMode(COM2, INPUT);\n        pinMode(COM3, INPUT);\n        pinMode(COM4, INPUT);\n    }\n    else if (index == 1) { \/\/ COM2\n        pinMode(COM2, OUTPUT); \n        digitalWrite(COM2, HIGH); \n        pinMode(COM1, INPUT);\n        pinMode(COM3, INPUT);\n        pinMode(COM4, INPUT);\n    }\n    else if (index == 2) { \/\/ COM3\n        pinMode(COM3, OUTPUT); \n        digitalWrite(COM3, HIGH);  \n        pinMode(COM1, INPUT);\n        pinMode(COM2, INPUT);\n        pinMode(COM4, INPUT);\n    }\n    else if (index == 3) { \/\/ COM4\n        pinMode(COM4, OUTPUT); \n        digitalWrite(COM4, HIGH);\n        pinMode(COM1, INPUT);\n        pinMode(COM2, INPUT);\n        pinMode(COM3, INPUT);\n    }\n}\n\nvoid deactivateCOM(int index) {\n     if (index == 0) {\n        digitalWrite(COM1, LOW);\n    }\n    else if (index == 1) {\n        digitalWrite(COM2, LOW);\n    }\n    else if (index == 2) {\n        digitalWrite(COM3, LOW);\n    }\n    else if (index == 3) {\n        digitalWrite(COM4, LOW);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently got some tiny LCD screens that have 3 digits with 7 segments, and I&#8217;ve been playing around with them to understand how they work better. The first thing I noticed compared to 7-segment LED displays is that to show a number, you just need to &#8216;turn on&#8217; the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":254,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[60,66,68],"class_list":["post-250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-arduino","tag-esp32","tag-lcd"],"_links":{"self":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":4,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/257"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}