{"id":1594,"date":"2016-02-05T16:14:16","date_gmt":"2016-02-05T14:14:16","guid":{"rendered":"https:\/\/blog.zitnik.si\/?p=1594"},"modified":"2022-08-07T22:52:31","modified_gmt":"2022-08-07T20:52:31","slug":"part-33-reading-analog-input-using-rpi","status":"publish","type":"post","link":"https:\/\/blog.zitnik.si\/?p=1594","title":{"rendered":"Part 3\/3: Reading analog input using RPi"},"content":{"rendered":"<p>Continuing from the previous example, we would like our LED to turn on automatically, based on the light level of the environment.<\/p>\n<p><strong>Photoresistor<\/strong><\/p>\n<p>First we need to read the value of the photocell that measures the value of light. Photocells (aka. photoresistor) acts as a dynamically-adapting resistor according to the light levels. In dark areas, the resistance is high and in bright areas the resistance is low.<\/p>\n<p>As Raspberry Pi does not have an analog input, we can make a trick to read a value from an analog sensor (resistor-based sensor). We just use a small capacitor and measure time that is needed to fill the capacitor to the full level.<\/p>\n<p>On Raspberry Pi we use 3.3V pin as power supply, GND as ground and pin 18 as an input pin that will be used to measure time to fill the capacitor. In the circuit we measure voltage levels using pin 18 between photoresistor and capacitor.<\/p>\n<p>To get the light level, we need to:<\/p>\n<ol>\n<li>Set pin 18 as out and to LOW voltage.<\/li>\n<li>Set pin 18 as input to be able to read input voltage. Now we can just read if voltage is LOW or HIGH.<\/li>\n<li>Measure, how much time is needed until read voltage on pin 18 is HIGH.<\/li>\n<\/ol>\n<p>Normally you can use a 1\u03bcF but we had just a capacitor of 100\u03bcF (if it is designed to higher voltage, it is okay). The difference is only you will need more time to fill the capacitor in darker conditions. In our case, we need about\u00a0800000ms to detect dark light and about\u00a030000ms to detect twilight.<\/p>\n<p><strong>Automatic light switching<\/strong><\/p>\n<p>Above we got the information whether the light conditions are daylight, twilight or darkness. Now we set a LED that will be turned off in daylight, burn lightly in twilight and be very bright in darkness.<\/p>\n<p>For this, we use two pins to turn on a LED and for each pin, we use different resistor to set the brightness of a LED. For darkness we use a 220Ohm resistor and for twilight we use 1120Ohm (2x560Ohm) resistor.<\/p>\n<p>The script to control all of the above looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import RPi.GPIO as GPIO, time, os\nGPIO.setmode(GPIO.BCM)\nGPIO.setwarnings(False)\n\n#Variables setup\nTWILIGHT_THRESHOLD = 30000\nDARK_THRESHOLD = 800000\nLIGHT_STATUS_PIN = 18\nTWILIGHT_LIGHT_PIN = 22\nDARKNESS_LIGHT_PIN = 23\n\nclass Light(object):\n        DAYLIGHT = \"Daylight\"\n        TWILIGHT = \"Twilight\"\n        DARKNESS = \"Darkness\"\n\ndef LightInfo():\n        GPIO.setup(LIGHT_STATUS_PIN, GPIO.OUT)\n        GPIO.output(LIGHT_STATUS_PIN, GPIO.LOW)\n        time.sleep(0.1)\n        GPIO.setup(LIGHT_STATUS_PIN, GPIO.IN)\n\n        # This takes about 1 millisecond per loop cycle\n        repeats_to_full_capacitor = 0\n        while (GPIO.input(LIGHT_STATUS_PIN) == GPIO.LOW):\n                repeats_to_full_capacitor += 1\n\n        if (repeats_to_full_capacitor &amp;lt; TWILIGHT_THRESHOLD):\n                return Light.DAYLIGHT\n        elif (repeats_to_full_capacitor &amp;lt; DARK_THRESHOLD):\n                return Light.TWILIGHT\n        else:\n                return Light.DARKNESS\n\ndef TurnLEDOnOff(status):\n        #Turn all off\n        GPIO.setup(TWILIGHT_LIGHT_PIN, GPIO.OUT)\n        GPIO.setup(DARKNESS_LIGHT_PIN, GPIO.OUT)\n        GPIO.output(TWILIGHT_LIGHT_PIN, GPIO.LOW)\n        GPIO.output(DARKNESS_LIGHT_PIN, GPIO.LOW)\n        time.sleep(0.1)\n        #Turn appropriate pin on\n        if (status == Light.TWILIGHT):\n                GPIO.setup(DARKNESS_LIGHT_PIN, GPIO.IN)\n                GPIO.output(TWILIGHT_LIGHT_PIN, GPIO.HIGH)\n        elif (status == Light.DARKNESS):\n                GPIO.setup(TWILIGHT_LIGHT_PIN, GPIO.IN)\n                GPIO.output(DARKNESS_LIGHT_PIN, GPIO.HIGH)\n\n#Main program\nstatus = Light.DAYLIGHT\nwhile True:\n        newStatus = LightInfo()\n        print newStatus\n        if (newStatus != status):\n                TurnLEDOnOff(newStatus)\n                status = newStatus<\/pre>\n<p>When we\u00a0run the script as &#8220;sudo .\/LightSwitch.py&#8221; in daylight conditions, the LED is off:<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0164.jpg\" rel=\"attachment wp-att-1596\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1596\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0164-1024x768.jpg\" alt=\"IMG_0164\" width=\"500\" height=\"375\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0164-1024x768.jpg 1024w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0164-300x225.jpg 300w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0164-768x576.jpg 768w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0164.jpg 1200w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>In twilight, the LED is not that bright:<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0165.jpg\" rel=\"attachment wp-att-1597\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1597\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0165-1024x768.jpg\" alt=\"IMG_0165\" width=\"500\" height=\"375\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0165-1024x768.jpg 1024w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0165-300x225.jpg 300w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0165-768x576.jpg 768w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0165.jpg 1200w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Lastly in the darkness, the LED becomes very bright:<\/p>\n<p><a href=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0166.jpg\" rel=\"attachment wp-att-1598\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1598\" src=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0166-1024x768.jpg\" alt=\"IMG_0166\" width=\"500\" height=\"375\" srcset=\"https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0166-1024x768.jpg 1024w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0166-300x225.jpg 300w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0166-768x576.jpg 768w, https:\/\/blog.zitnik.si\/wp-content\/uploads\/2016\/02\/IMG_0166.jpg 1200w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div>","protected":false},"excerpt":{"rendered":"<p>Continuing from the previous example, we would like our LED to turn on automatically, based on the light level of the environment. Photoresistor First we need to read the value of the photocell that measures the value of light. Photocells (aka. photoresistor) acts as a dynamically-adapting resistor according to the&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/blog.zitnik.si\/?p=1594\">Continue reading<span class=\"screen-reader-text\">Part 3\/3: Reading analog input using RPi<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":1646,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,30],"tags":[],"class_list":["post-1594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-engineering","category-free-time","entry"],"_links":{"self":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1594"}],"version-history":[{"count":4,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1594\/revisions"}],"predecessor-version":[{"id":1908,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/posts\/1594\/revisions\/1908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=\/wp\/v2\/media\/1646"}],"wp:attachment":[{"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zitnik.si\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}