{"id":717,"date":"2022-10-19T20:00:00","date_gmt":"2022-10-19T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=717"},"modified":"2022-09-18T12:41:01","modified_gmt":"2022-09-18T03:41:01","slug":"raspberry-pi-photoreflector","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/raspberry-pi-photoreflector\/","title":{"rendered":"How to use a reflective optical sensor with Raspberry Pi."},"content":{"rendered":"\n<p>A reflective optical sensor is a sensor that irradiates infrared light onto an object and detects the light reflected from it.<\/p>\n\n\n\n<p>This article shows <span class=\"st-mymarker-s\">how to use a reflective optical sensor with Raspberry Pi<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>Parts required to use a reflective optical sensor<\/li><li>Calculation of resistance value<\/li><li>Connection of each part<\/li><li>Python sample code<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Parts required to use a reflective optical sensor<\/h2>\n\n\n\n<p>In the beginning, I show <span class=\"st-mymarker-s\">parts required to use a reflective optical sensor<\/span>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">reflective optical sensor<\/h3>\n\n\n\n<p><\/p>\n\n\n<p><iframe style=\"width: 120px; height: 240px;\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=katoshu0805-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B07K1HVNH7&amp;asins=B07K1HVNH7&amp;linkId=19e0ef78d00d5232edcbf545fd98a3f0&amp;show_border=true&amp;link_opens_in_new_window=true\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" sandbox=\"allow-popups allow-scripts allow-modals allow-forms allow-same-origin\"><\/iframe><\/p>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Breadboard<\/h3>\n\n\n\n<p>This breadboard is a best seller on Amazon.<\/p>\n\n\n<p><iframe style=\"width: 120px; height: 240px;\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=katoshu0805-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B07DL13RZH&amp;asins=B07DL13RZH&amp;linkId=01258b404f2d128d7166ac48534a9073&amp;show_border=true&amp;link_opens_in_new_window=true\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" sandbox=\"allow-popups allow-scripts allow-modals allow-forms allow-same-origin\"><\/iframe><\/p>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Jumper wire<\/h3>\n\n\n\n<p>This product is a set of male-male, male-female, and female-female.<\/p>\n\n\n<p><iframe style=\"width: 120px; height: 240px;\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=katoshu0805-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B07GD2BWPY&amp;asins=B07GD2BWPY&amp;linkId=b6170048a1d574627ce47a8df2dd778d&amp;show_border=true&amp;link_opens_in_new_window=true\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" sandbox=\"allow-popups allow-scripts allow-modals allow-forms allow-same-origin\"><\/iframe><\/p>\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Resistor<\/h3>\n\n\n\n<p>This product is amazon&#8217;s choice.<\/p>\n\n\n<p><iframe style=\"width: 120px; height: 240px;\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=katoshu0805-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B08FD1XVL6&amp;asins=B08FD1XVL6&amp;linkId=4db8a595fbc99afa9027dd01bd963f80&amp;show_border=true&amp;link_opens_in_new_window=true\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" sandbox=\"allow-popups allow-scripts allow-modals allow-forms allow-same-origin\"><\/iframe><\/p>\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Calculation of resistance value<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to calculate the resistance value to be connected to a reflective optical sensor<\/span>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Datasheet of a reflective optical sensor (RPR-220)<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">A datasheet of a reflective optical sensor<\/span> is required to calculate the resistance value.<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"545\" height=\"641\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RPR-220_datasheet.jpg\" alt=\"\" class=\"wp-image-1106\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RPR-220_datasheet.jpg 545w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RPR-220_datasheet-255x300.jpg 255w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The data sheet can be downloaded <a href=\"https:\/\/fscdn.rohm.com\/en\/products\/databook\/datasheet\/opto\/optical_sensor\/photosensor\/rpr-220.pdf\">here<\/a>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Circuit diagram of a reflective optical sensor<\/h3>\n\n\n\n<p>This is <span class=\"st-mymarker-s\">a schematic of the reflective optical sensor, the power supply, and the resistor.<\/span><\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/cirsuit.png\" alt=\"\" class=\"wp-image-1116\" width=\"603\" height=\"359\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/cirsuit.png 804w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/cirsuit-300x179.png 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/cirsuit-768x458.png 768w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><\/figure><\/div>\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>From this schematic and the current constraints on the GPIO pins of the Raspberry Pi, the value of the resistor is calculated.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>the current constraints on the GPIO pins of the Raspberry Pi.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-my-box st-mybox has-title\" style=\"background-color:#fff;border-color:#ffc107\"><p class=\"st-mybox-title\" style=\"color:#ffc107;font-weight:bold;text-shadow:#fff 3px 0px 0px, #fff 2.83487px .981584px 0px, #fff 2.35766px 1.85511px 0px, #fff 1.62091px 2.52441px 0px, #fff .705713px 2.91581px 0px, #fff -0.287171px 2.98622px 0px, #fff -1.24844px 2.72789px 0px, #fff -2.07227px 2.16926px 0px, #fff -2.66798px 1.37182px 0px, #fff -2.96998px .42336px 0px, #fff -2.94502px -0.571704px 0px, #fff -2.59586px -1.50383px 0px, #fff -1.96093px -2.27041px 0px, #fff -1.11013px -2.78704px 0px, #fff -0.137119px -2.99686px 0px, #fff .850987px -2.87677px 0px, #fff 1.74541px -2.43999px 0px, #fff 2.44769px -1.73459px 0px, #fff 2.88051px -0.838246px 0px;background:linear-gradient(0deg, #fff 0%, #fff 55%, rgba(0, 0, 0, 0) 55%, rgba(0, 0, 0, 0) 100%)\"><i class=\"st-fa st-svg-check-circle st-css-no\" aria-hidden=\"\"><\/i>Note<\/p><div class=\"st-in-mybox\">\n<p><\/p>\n\n\n\n<ul><li>Maximum current per GPIO 1 pin is 16mA.<\/li><li>When multiple GPIO pins are used, the total maximum current is 50 mA.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Calculation of resistance value A<\/h3>\n\n\n\n<p>From the data sheet, the VF of the LED is 1.34V.<\/p>\n\n\n\n<p>So, 1.96V ( = 3.3V &#8211; 1.34V ) is applied to resistance A.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Maximum current per GPIO 1 pin is 16mA.<\/p>\n\n\n\n<p>It is only a maximum value, so it should be 10 mA or less with a margin of error.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>From Ohm&#8217;s Law<\/p>\n\n\n\n<p>R = V \/ I = 1.96[V] \/ 0.01[A] = 196[\u03a9]<\/p>\n\n\n\n<p>When the resistance is 196 \u03a9 or higher, the current is 10 mA or less.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I used a 330 ohm resistor that I had.<\/p>\n\n\n\n<p>With a 330\u03a9 resistor, the current value is 6mA.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Calculation of resistance value B<\/h3>\n\n\n\n<p>To calculate the value of resistor B, the Low \/ High threshold voltage of the GPIO pin of the Raspberry Pi must be considered.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-my-box st-mybox has-title\" style=\"background-color:#fff;border-color:#ffc107\"><p class=\"st-mybox-title\" style=\"color:#ffc107;font-weight:bold;text-shadow:#fff 3px 0px 0px, #fff 2.83487px .981584px 0px, #fff 2.35766px 1.85511px 0px, #fff 1.62091px 2.52441px 0px, #fff .705713px 2.91581px 0px, #fff -0.287171px 2.98622px 0px, #fff -1.24844px 2.72789px 0px, #fff -2.07227px 2.16926px 0px, #fff -2.66798px 1.37182px 0px, #fff -2.96998px .42336px 0px, #fff -2.94502px -0.571704px 0px, #fff -2.59586px -1.50383px 0px, #fff -1.96093px -2.27041px 0px, #fff -1.11013px -2.78704px 0px, #fff -0.137119px -2.99686px 0px, #fff .850987px -2.87677px 0px, #fff 1.74541px -2.43999px 0px, #fff 2.44769px -1.73459px 0px, #fff 2.88051px -0.838246px 0px;background:linear-gradient(0deg, #fff 0%, #fff 55%, rgba(0, 0, 0, 0) 55%, rgba(0, 0, 0, 0) 100%)\"><i class=\"st-fa st-svg-check-circle st-css-no\" aria-hidden=\"\"><\/i>Note<\/p><div class=\"st-in-mybox\">\n<p>the Low \/ High threshold voltage of the GPIO pin of the Raspberry Pi<\/p>\n\n\n\n<ul><li>High : 1.3V or higher<\/li><li>Low : 0.8V or less<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>From the Low \/ High threshold voltages of the GPIO pins, two equations can be established.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>1.3[V] &lt; Ic min \u00d7 R          \u30fb\u30fb\u30fbEquation 1<\/p>\n\n\n\n<p>0.8[V] &gt; Iceo max \u00d7 R     \u30fb\u30fb\u30fbEquation 2<\/p>\n\n\n\n<p>Ic : current of collector, Iceo : dark current, R : resistor value<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Equation 1 :   This condition is recognized as High on Raspberry Pi even when the current of collector &#8220;Ic&#8221; is minimum.<\/p>\n\n\n\n<p>Equation 2 :   This condition is recognized as Low on Raspberry Pi even when the dark current &#8220;Iceo&#8221; is maximum.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>From these two equations, the resistance R is calculated.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>According to the data sheet, Ic min is 0.08mA.<\/p>\n\n\n\n<p>However, since the value of  &#8220;Ic min&#8221; are based on the IF=10mA condition, it is necessary to recalculate &#8220;Ic min&#8221; to match the current conditions.<\/p>\n\n\n\n<p>Under the present condition, IF is 6mA.<\/p>\n\n\n\n<p>Ic min = 0.08[mA] \u00d7 (6[mA] \/ 10[mA]) = 0.048mA<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>According to the data sheet, Iceo max is 0.5uA.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Based on the above conditions, R is as follows.<\/p>\n\n\n\n<p>27[k\u03a9] &lt; R &lt; 1.6 [M\u03a9]<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I used a 47k\u03a9 resistor that I had.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Connection of each part<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to connect each part.<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">GPIO and the 40-pin Header on Raspberry Pi 4<\/h3>\n\n\n\n<p>Here is <span class=\"st-mymarker-s\">the pinout of the Raspberry Pi<\/span>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center\"><p><img decoding=\"async\" width=\"789\" height=\"445\" class=\"wp-image-518\" style=\"width: 600px;\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/pinout_raspberrypi.jpg\" alt=\"\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/pinout_raspberrypi.jpg 789w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/pinout_raspberrypi-300x169.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/pinout_raspberrypi-768x433.jpg 768w\" sizes=\"(max-width: 789px) 100vw, 789px\" \/><\/p><cite>quotation : <a href=\"https:\/\/www.raspberrypi.com\/documentation\/computers\/os.html\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi Documentation<\/a><\/cite><\/blockquote>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Connection of each part<\/h3>\n\n\n\n<p>This is <span class=\"st-mymarker-s\">a schematic of the reflective optical sensor and Raspberry Pi<\/span>.<\/p>\n\n\n\n<p>A reflective optical sensor on the schematic is replaced by two LEDs.<br>( There is no part of reflective optical sensor on &#8220;Fritzing&#8221;. )<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>On this schematic, white LED is &#8220;LED&#8221; and gray LED is &#8220;phototransistor&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Phototransistor is input to GPIO 17 (pin 11).<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/photoreflector_raspberrypi-1024x959.png\" alt=\"\" class=\"wp-image-1126\" width=\"768\" height=\"719\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/photoreflector_raspberrypi-1024x959.png 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/photoreflector_raspberrypi-300x281.png 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/photoreflector_raspberrypi-768x719.png 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/photoreflector_raspberrypi-1536x1439.png 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/photoreflector_raspberrypi.png 1755w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Picture of breadboard after wiring<\/p>\n\n\n<div class=\"wp-block-image is-style-st-photo-shadow\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_photoreflector_1-1024x684.jpg\" alt=\"\" class=\"wp-image-1128\" width=\"768\" height=\"513\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_photoreflector_1-1024x684.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_photoreflector_1-300x200.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_photoreflector_1-768x513.jpg 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_photoreflector_1-1536x1026.jpg 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_photoreflector_1-2048x1367.jpg 2048w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Python sample code<\/h2>\n\n\n\n<p>Every second, the output of reflective optical sensor is read at the GPIO pin.<\/p>\n\n\n\n<p>Press &#8220;ctrl&#8221; + &#8220;C&#8221; simultaneously to stop.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>### sample code\n\nimport RPi.GPIO as GPIO\nimport time\n\nGPIO.setmode(GPIO.BCM) #Specified by GPIO number\n#GPIO.setmode(GPIO.BOARD) #Specified by Pin number\n\ngpio_sensor = 17 #Specified by GPIO number\nGPIO.setup(gpio_sensor,GPIO.IN) #Specified by \"in mode\"\n\ntime_start = time.time()\n\ntry :\n    while True :\n\n        ### ------------------- get_time ----------------- ###\n        time_end = time.time()\n        time_elapsed = time_end - time_start\n\n        ### ------------------- GPIO input ----------------- ###\n        input_sensor = GPIO.input(gpio_sensor)\n\n        print('Time : {:.1f}s, GPIO_input : {}'.format(time_elapsed, input_sensor))\n        print(\"\")\n\n        time.sleep(1)\nexcept KeyboardInterrupt:\n    print(\"\\nCtrl+C\")\n    GPIO.cleanup(gpio_sensor)<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This video shows the output of reflective optical sensor is acquired by Raspberry Pi.<\/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 title=\"\u30d5\u30a9\u30c8\u30ea\u30d5\u30ec\u30af\u30bf\u51fa\u529b\u3092\u30e9\u30ba\u30d1\u30a4(raspberry pi)\u306eGPIO\u5165\u529b\u3067\u691c\u51fa\u3002\" width=\"974\" height=\"548\" src=\"https:\/\/www.youtube.com\/embed\/PSFyGbgolCw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>A reflective optical sensor is a sensor that irradiates infrared light onto an object and detects th &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[24],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/717"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=717"}],"version-history":[{"count":132,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/717\/revisions"}],"predecessor-version":[{"id":1215,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/717\/revisions\/1215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/1140"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}