{"id":714,"date":"2022-10-12T20:00:00","date_gmt":"2022-10-12T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=714"},"modified":"2022-09-17T09:35:27","modified_gmt":"2022-09-17T00:35:27","slug":"raspberry-pi-tof","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/raspberry-pi-tof\/","title":{"rendered":"How to use a ToF sensor with Raspberry Pi."},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to use a ToF sensor with Raspberry Pi.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>ToF sensor (VL53L1X) was controlled by <span class=\"st-mymarker-s\">I2C communication<\/span> from Raspberry Pi.<\/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 ToF sensor<\/li><li>Connection of each parts<\/li><li>Python sample code<\/li><li>Operation check of ToF sensor<\/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 ToF sensor<\/h2>\n\n\n\n<p>In the beginning, I show <span class=\"st-mymarker-s\">parts required to use a ToF 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\">ToF sensor<\/h3>\n\n\n\n<p>I bought a module with VL53L1X at <a rel=\"noreferrer noopener\" href=\"https:\/\/www.switch-science.com\/catalog\/3938\/\" target=\"_blank\">Switch Science<\/a>.<\/p>\n\n\n\n<p>( Sorry, link to Japanese shop )<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Specifications of ToF sensor.<\/h4>\n\n\n\n<ul><li>Size : 13 \u00d7 18 \u00d7 2 mm<\/li><li>Weight : 0.5 g<\/li><li>Resolution : 1mm<\/li><li>Max range : 400cm<\/li><li>Min range : 4cm<\/li><li>Interface : I2C<\/li><li>Min operating power : 2.6V<\/li><li>Max operating power : 5.5V<\/li><li>Power supply current : 15mA<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Alternatives on Amazon US.<\/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=B08J1K9T5P&amp;asins=B08J1K9T5P&amp;linkId=cea05e7567005694e1c438225d6a714f&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<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\">Connection of each parts<\/h2>\n\n\n\n<p>From here, 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<p>The pinouts of the Raspberry Pi can also be viewed by using the terminal.<\/p>\n\n\n\n<p>Enter the following command in the terminal and the pinouts of the Raspberry Pi will be displayed.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ pinout<\/code><\/pre>\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\/08\/raspi-pinout1-2.png\" alt=\"\" class=\"wp-image-523\" width=\"389\" height=\"381\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-pinout1-2.png 518w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-pinout1-2-300x294.png 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-pinout1-2-60x60.png 60w\" sizes=\"(max-width: 389px) 100vw, 389px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\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\/08\/raspi-pinout2.png\" alt=\"\" class=\"wp-image-525\" width=\"222\" height=\"384\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-pinout2.png 296w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-pinout2-173x300.png 173w\" sizes=\"(max-width: 222px) 100vw, 222px\" \/><\/figure><\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Since I2C communication is used, PIN numbers 3(SDA) and 5(SCL) are used.<\/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\">Connection of each part<\/h3>\n\n\n\n<p>I show the wiring diagram of the ToF sensor and the Raspberry Pi.<\/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\/RaspberryPi-ToFsensor-915x1024.png\" alt=\"\" class=\"wp-image-1023\" width=\"686\" height=\"768\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RaspberryPi-ToFsensor-915x1024.png 915w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RaspberryPi-ToFsensor-268x300.png 268w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RaspberryPi-ToFsensor-768x860.png 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/RaspberryPi-ToFsensor.png 1005w\" sizes=\"(max-width: 686px) 100vw, 686px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Connect SDA and SCL between the ToF sensor and the Raspberry Pi.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The 5V output of the Raspberry Pi is used to power the ToF sensor.<\/p>\n\n\n\n<p>Do not forget to connect GND as well.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is a picture of the 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_ToF1-1024x684.jpg\" alt=\"\" class=\"wp-image-1029\" width=\"768\" height=\"513\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF1-1024x684.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF1-300x200.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF1-768x513.jpg 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF1-1536x1025.jpg 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF1-2048x1367.jpg 2048w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is a picture of the front of the ToF sensor.<\/p>\n\n\n\n<p>Note that a ToF sensor may have a protective sticker on it if you just bought it.<\/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_ToF2-1024x684.jpg\" alt=\"\" class=\"wp-image-1033\" width=\"768\" height=\"513\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF2-1024x684.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF2-300x200.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF2-768x513.jpg 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF2-1536x1025.jpg 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF2-2048x1367.jpg 2048w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is a picture of the ToF sensor after the protective sticker has been removed.<\/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_ToF3-1024x683.jpg\" alt=\"\" class=\"wp-image-1037\" width=\"768\" height=\"512\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF3-1024x683.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF3-300x200.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF3-768x513.jpg 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF3-1536x1025.jpg 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/raspberry_pi_ToF3-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<h3 class=\"wp-block-heading\">Enable I2C communication on the Raspberry Pi.<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">Enable I2C communication<\/span> on the Raspberry Pi.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Go to &#8220;Preferences&#8221; &gt; &#8220;Raspberry Pi Configuration&#8221;.<\/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\/08\/raspi-servo1-1-1024x576.jpg\" alt=\"\" class=\"wp-image-535\" width=\"768\" height=\"432\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo1-1-1024x576.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo1-1-300x169.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo1-1-768x432.jpg 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo1-1-1536x864.jpg 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo1-1.jpg 1920w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<p>( Sorry, Japanese is included. )<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>On the Interface tab, enable &#8220;I2C&#8221;.<\/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\/08\/raspi-servo2.png\" alt=\"\" class=\"wp-image-538\" width=\"674\" height=\"348\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo2.png 898w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo2-300x155.png 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/08\/raspi-servo2-768x397.png 768w\" sizes=\"(max-width: 674px) 100vw, 674px\" \/><\/figure><\/div>\n\n\n<p>( Sorry, Japanese is included. )<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I2C communication is available on Raspberry Pi.<\/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\">Check the I2C address of the ToF sensor<\/h3>\n\n\n\n<p>Check the I2C address of the ToF sensor.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Enter the following command in the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo i2cdetect -y 1<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If the following image is displayed, the I2C communication is established.<\/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\/i2c_channel_2.jpg\" alt=\"\" class=\"wp-image-1048\" width=\"400\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/i2c_channel_2.jpg 316w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/i2c_channel_2-300x143.jpg 300w\" sizes=\"(max-width: 316px) 100vw, 316px\" \/><\/figure><\/div>\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Channel 0x29 can be used for communication with a ToF sensor.<\/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\">Python sample code for using a ToF sensor<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">Python sample code for using a ToF sensor<\/span>.<\/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\">Install the library for VL53L1X<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The library for VL53L1X must be installed<\/span> in advance.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Enter the following command in the terminal to install the library.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>$ sudo pip3 install vl53l1x<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/install_vl53l1x_2.jpg\" alt=\"\" class=\"wp-image-1060\" width=\"500\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/install_vl53l1x_2.jpg 457w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/install_vl53l1x_2-300x89.jpg 300w\" sizes=\"(max-width: 457px) 100vw, 457px\" \/><\/figure><\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Python sample code<\/h3>\n\n\n\n<p>Here is a sample code to use a ToF sensor with Raspberry Pi.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>### Sample code\n\nimport VL53L1X\nimport time\n\ntof = VL53L1X.VL53L1X(i2c_bus=1, i2c_address=0x29)\nprint(\"Python: Initialized\")\ntof.open()\nprint(\"Python: Opened\")\n\n\ntime_start = time.time()\n\ntry :\n    while True:\n\n\n        ### ------------------- get_time ----------------- ###\n\n        time_end = time.time()\n        time_elapsed = time_end - time_start\n\n        print(\"\")\n        print(\"--- Time : {:.1f}s ---\".format(time_elapsed))\n\n\n\n        ### ------------------- get_distance ----------------- ###\n\n        ### --- Short Range --- ###\n        tof.start_ranging(1)    # 1 = Short Range\n        distance_mm = tof.get_distance()\n        print(\"Short Range Distance: {}mm\".format(distance_mm))\n        tof.stop_ranging()\n\n        ### --- Medium Range --- ###\n        tof.start_ranging(2)    # 2 = Medium Range\n        distance_mm = tof.get_distance()\n        print(\"Medium Range Distance: {}mm\".format(distance_mm))\n        tof.stop_ranging()\n\n        ### --- Long Range --- ###\n        tof.start_ranging(3)    # 3 = Long Range\n        distance_mm = tof.get_distance()\n        print(\"Long Range Distance: {}mm\".format(distance_mm))\n        tof.stop_ranging()\n\n\n        time.sleep(1)\n\nexcept KeyboardInterrupt:\n        print(\"\\nCtrl+C\")<\/code><\/pre>\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\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\">Operation check of ToF sensor<\/h2>\n\n\n\n<p>This video shows a ToF sensor being used to measure distance.<\/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=\"ToF\u30bb\u30f3\u30b5\u30fc\u3068\u30e9\u30ba\u30d1\u30a4(raspberry pi)\u3067\u6e2c\u8ddd\u3002\" width=\"974\" height=\"548\" src=\"https:\/\/www.youtube.com\/embed\/yst_ZrAA3VQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>VL53L1X has three measurement modes.<\/p>\n\n\n\n<p>To confirm the characteristics of each ranging mode, I conducted a simple experiment.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is a picture of the experimental environment.<\/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\/test-of-tof-1024x684.jpg\" alt=\"\" class=\"wp-image-1069\" width=\"768\" height=\"513\" srcset=\"https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/test-of-tof-1024x684.jpg 1024w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/test-of-tof-300x200.jpg 300w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/test-of-tof-768x513.jpg 768w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/test-of-tof-1536x1026.jpg 1536w, https:\/\/python-academia.com\/en\/wp-content\/uploads\/sites\/2\/2022\/09\/test-of-tof.jpg 1678w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here are the measurement results.<\/p>\n\n\n\n<table id=\"tablepress-2\" class=\"tablepress tablepress-id-2\">\n<thead>\n<tr class=\"row-1 odd\">\n\t<th class=\"column-1\"><\/th><th class=\"column-2\">Short Range<\/th><th class=\"column-3\">Medium Range<\/th><th class=\"column-4\">Long Range<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n\t<td class=\"column-1\">50cm<\/td><td class=\"column-2\">47.6cm<\/td><td class=\"column-3\">47.5cm<\/td><td class=\"column-4\">46.2cm<\/td>\n<\/tr>\n<tr class=\"row-3 odd\">\n\t<td class=\"column-1\">100cm<\/td><td class=\"column-2\">96.9cm<\/td><td class=\"column-3\">99.2cm<\/td><td class=\"column-4\">99.8cm<\/td>\n<\/tr>\n<tr class=\"row-4 even\">\n\t<td class=\"column-1\">150cm<\/td><td class=\"column-2\">154.9cm<\/td><td class=\"column-3\">156.1cm<\/td><td class=\"column-4\">157.1cm<\/td>\n<\/tr>\n<tr class=\"row-5 odd\">\n\t<td class=\"column-1\">200cm<\/td><td class=\"column-2\">184.1cm<\/td><td class=\"column-3\">189.5cm<\/td><td class=\"column-4\">192.2cm<\/td>\n<\/tr>\n<tr class=\"row-6 even\">\n\t<td class=\"column-1\">250cm<\/td><td class=\"column-2\">204.5cm<\/td><td class=\"column-3\">217.8cm<\/td><td class=\"column-4\">235.1cm<\/td>\n<\/tr>\n<tr class=\"row-7 odd\">\n\t<td class=\"column-1\">300cm<\/td><td class=\"column-2\">166.9cm<\/td><td class=\"column-3\">205.2cm<\/td><td class=\"column-4\">255.4cm<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-2 from cache -->\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When the object distance was close, there was no difference between modes, but when the object distance exceeded 250 cm, an error became larger by using the Short Range mode.<\/p>\n\n\n\n<p>This result is expected to vary depending on the reflectivity of the subject and its shape.<\/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>This article shows how to use a ToF sensor with Raspberry Pi. ToF sensor (VL53L1X) was controlled by &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1009,"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\/714"}],"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=714"}],"version-history":[{"count":95,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/714\/revisions"}],"predecessor-version":[{"id":1123,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/714\/revisions\/1123"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/1009"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}