{"id":22923,"date":"2021-07-31T10:25:22","date_gmt":"2021-07-31T01:25:22","guid":{"rendered":"https:\/\/www.souichi.club\/?p=22923"},"modified":"2022-03-19T17:22:43","modified_gmt":"2022-03-19T08:22:43","slug":"m5stack-optical-sensor","status":"publish","type":"post","link":"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/","title":{"rendered":"M5Stack&#8217;s light sensor informs the Raspberry Pi when a rotary beacon light is on"},"content":{"rendered":"<ul class=\"bogo-language-switcher list-view\"><li class=\"en-US en current first\"><span class=\"bogoflags bogoflags-us\"><\/span> <span class=\"bogo-language-name\"><a rel=\"alternate\" hreflang=\"en-US\" href=\"https:\/\/www.souichi.club\/en\/wp-json\/wp\/v2\/posts\/22923\/\" title=\"English\" class=\"current\" aria-current=\"page\">English<\/a><\/span><\/li>\n<li class=\"ja last\"><span class=\"bogoflags bogoflags-jp\"><\/span> <span class=\"bogo-language-name\"><a rel=\"alternate\" hreflang=\"ja\" href=\"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/22923\/\" title=\"Japanese\">\u65e5\u672c\u8a9e<\/a><\/span><\/li>\n<\/ul>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f33d312059e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #454e5c;color:#454e5c\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #454e5c;color:#454e5c\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f33d312059e\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#M5Stacks_light_sensor\" >M5Stack&#8217;s light sensor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Overall_configuration_diagram\" >Overall configuration diagram<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Prepared_equipment_parts_and_tools\" >Prepared equipment, parts, and tools<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Optical_Sensor_Unit_for_M5Stack\" >Optical Sensor Unit for M5Stack<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#M5Stack_ATOM_Lite\" >M5Stack ATOM Lite<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Pins\" >Pins<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Left_side\" >Left side<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#right_side\" >right side<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Grove_compatible_terminals\" >Grove compatible terminals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#front_side\" >front side<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Raspberry_Pi_4B\" >Raspberry Pi 4B<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Wiring_Diagram\" >Wiring Diagram<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Program\" >Program<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#ATOM_Lite\" >ATOM Lite<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Building_the_Environment\" >Building the Environment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Installing_the_library\" >Installing the library<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Tool_Settings\" >Tool Settings<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Board_management\" >Board management<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Checking_the_COM_port\" >Checking the COM port<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Source_Code\" >Source Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Supplementary_explanation\" >Supplementary explanation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Writing_the_program\" >Writing the program<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Raspberry_Pi\" >Raspberry Pi<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Directory_structure\" >Directory structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Source_Code-2\" >Source Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Supplementary_explanation-2\" >Supplementary explanation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#Execution\" >Execution<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stack-optical-sensor\/#At_the_end\" >At the end.<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"M5Stacks_light_sensor\"><\/span>M5Stack&#8217;s light sensor<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>I created a program that detects the lighting of the rotary beacon light with M5Stack&#8217;s light sensor and sends the lighting information via BLE (Bluetooth Low Energy).<\/p>\n\n\n\n<p>On the Raspberry Pi side, it displays on the console which  rotary beacon light has been turned on based on the received information.<\/p>\n\n\n\n<p>Since BLE is a 4.2 standard, it has a communication speed of 1Mbps and can fly up to 100m in a place with good visibility.<\/p>\n\n\n\n<p>Since the Wi-Fi of the Raspberry Pi is not used, this configuration can be used in places where LAN (wired or wireless) is not installed, thus expanding the range of use.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?resize=512%2C431&#038;ssl=1\" alt=\"BLE communication between M5Stack light sensor, ATOM Lite and Raspberry Pi 4B\" class=\"wp-image-20590\" width=\"512\" height=\"431\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?resize=1024%2C861&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?resize=300%2C252&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?resize=768%2C646&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?resize=1536%2C1291&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-22.jpg?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Overall_configuration_diagram\"><\/span>Overall configuration diagram<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The overall configuration diagram is as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?resize=768%2C432&#038;ssl=1\" alt=\"Overall configuration of detecting the lighting of rotary beacon light by optical sensor\" class=\"wp-image-22927\" width=\"768\" height=\"432\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-01.png?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Detect the lighting of the rotary beacon light with the photoresistor part of the light sensor for the M5Stack<\/li><li>Receive the detection result (digital or analog) by M5Stack ATOM Lite<\/li><li>Advertise (broadcast) the detection information via BLE (Bluetooth Low Energy) for 10 seconds<\/li><li>Raspberry Pi 4B receives the BLE and displays it on the console<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prepared_equipment_parts_and_tools\"><\/span>Prepared equipment, parts, and tools<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Optical_Sensor_Unit_for_M5Stack\"><\/span>Optical Sensor Unit for M5Stack<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The photoresistor section detects light and outputs both digital and analog signals.<\/p>\n\n\n\n<p>The digital value indicates whether the light is on (0) or off (1), and the threshold value can be adjusted with a variable resistor.<\/p>\n\n\n\n<p>The analog value represents the intensity of the light as a number from 0 (light) to 4095 (dark).<\/p>\n\n\n\n<p>It can also be connected to the M5Stack series via Grove compatible terminals.<\/p>\n\n\n\n<p>\u5149\u30bb\u30f3\u30b5\u30fc\u306e\u5199\u771f\u3092\u3053\u3053\u306b\u8f09\u305b\u308b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"M5Stack_ATOM_Lite\"><\/span>M5Stack ATOM Lite<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This time, I used M5Stack ATOM Lite to connect to the optical sensor unit.<\/p>\n\n\n\n<p>The ATOM Lite is the smallest in size among the M5Stack series and the least expensive at the moment (May 2021).<\/p>\n\n\n\n<p>Compared to the M5Stick-C, the ATOM Lite does not have a built-in battery, so it is necessary to secure a power supply (supplied via USB-Type-C).<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Size: 24 x 24mm<\/li><li>Chip: ESP32-PICO-D4<\/li><li>Wi-Fi, Bluetooth 4.2<\/li><li>4MB flash memory<\/li><li>RGB LED<\/li><li>Infrared LED<\/li><li>Button x 2<\/li><li>GPIO pins x 6<\/li><li>Grove compatible interface<\/li><li>Power input: 5V\/500mA (USB Type-C)<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?resize=256%2C227&#038;ssl=1\" alt=\"Behind the scenes of M5Stack ATOM Lite\" class=\"wp-image-20522\" width=\"256\" height=\"227\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?resize=1024%2C908&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?resize=300%2C266&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?resize=768%2C681&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?resize=1536%2C1361&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-02.jpg?w=1635&amp;ssl=1 1635w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/a><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pins\"><\/span>Pins<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>On the back, there is a 5-pin connector on the left, a 4-pin connector on the right, and a Grove compatible connector in the center.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Left_side\"><\/span>Left side<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>From the top.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>3V3: Out<\/li><li>G22: In\/Out<\/li><li>G19: In\/Out<\/li><li>G23: In\/Out<\/li><li>G33: In\/Out(Analog\uff09<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"right_side\"><\/span>right side<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>From the top.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>G21: In\/Out<\/li><li>G25: In\/Out<\/li><li>\uff15V: Out<\/li><li>GND<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Grove_compatible_terminals\"><\/span>Grove compatible terminals<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>From left to right.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GND<\/li><li>\uff15V: Out<\/li><li>G26: In\/Out<\/li><li>G32: In\/Out(Analog)<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"front_side\"><\/span>front side<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>On the front, there is a USB Type-C connector for power supply, button A and LED in the center, and a reset button on the left.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?resize=256%2C227&#038;ssl=1\" alt=\"Front side of the M5Stack ATOM Lite\" class=\"wp-image-22986\" width=\"256\" height=\"227\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?resize=1024%2C907&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?resize=300%2C266&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?resize=768%2C680&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?resize=1536%2C1360&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-03.jpg?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/a><\/figure>\n\n\n\n<p>This time I connected it to ATOM Lite, but it can also be connected to other M5Stack series and M5Stick-C.<\/p>\n\n\n\n<p>I also connected it to my M5Stick-C, and it worked fine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Raspberry_Pi_4B\"><\/span>Raspberry Pi 4B<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In order to receive the BLE signal from ATOM Lite and display the information on the console, I prepared a Raspberry Pi 4B.<\/p>\n\n\n\n<p>The Raspberry Pi 4B supports Bluetooth Ver. 5.0 (125 kbps and a distance of up to 400 m), but since the Bluetooth version on the ATOM Lite side is 4.2, communication is based on 4.2.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?resize=512%2C280&#038;ssl=1\" alt=\"Raspberry Pi 4B\" class=\"wp-image-22988\" width=\"512\" height=\"280\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?resize=1024%2C559&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?resize=300%2C164&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?resize=768%2C419&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?resize=1536%2C838&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-04.jpg?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Wiring_Diagram\"><\/span>Wiring Diagram<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The wiring diagram is shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?resize=768%2C401&#038;ssl=1\" alt=\"Wiring the optical sensor to the M5Stack ATOM Lite\" class=\"wp-image-22934\" width=\"768\" height=\"401\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?resize=1024%2C535&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?resize=768%2C401&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?resize=1536%2C802&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-02.png?w=1574&amp;ssl=1 1574w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Program\"><\/span>Program<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The development environment for ATOM Lite, which acquires the values of the light sensor and advertises them via BLE, was created in C++ with the Arduino IDE installed on Windows 10.<\/p>\n\n\n\n<p>On the other hand, the Raspberry Pi, which receives the data and displays it on the console, was created in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ATOM_Lite\"><\/span>ATOM Lite<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Building_the_Environment\"><\/span>Building the Environment<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>For details on downloading and installing the Arduino IDE, please <a href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stickc\/#arduinoidedownload\" target=\"_blank\" rel=\"noreferrer noopener\">refer to the previous article<\/a>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Arduino IDE Download<\/li><li>Installation<\/li><li>Installing Additional Software<\/li><li>Adding a board manager<\/li><li>The following steps are done.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installing_the_library\"><\/span>Installing the library<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Install the library for the M5Atom we will be using.<\/p>\n\n\n\n<p>From the Arduino IDE menu, go to Sketch -&gt; Include Library -&gt; Manage Libraries, search for &#8220;M5Atom&#8221; in the search field, and install the library that appears.<\/p>\n\n\n\n<p>I installed 0.0.2, which is the latest version at the moment (May 2021).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-05.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-05.png?resize=738%2C416&#038;ssl=1\" alt=\"M5Atom library installation\" class=\"wp-image-22989\" width=\"738\" height=\"416\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-05.png?w=984&amp;ssl=1 984w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-05.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-05.png?resize=768%2C432&amp;ssl=1 768w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/a><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Tool_Settings\"><\/span>Tool Settings<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The settings for the tools in the Arduino IDE menu are as follows<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Board: ESP32 Arduino -&gt; Select M5Stack-ATOM (<a href=\"#Board-management\">see below for details<\/a>)<\/li><li>Upload Speed: 1500000 (reduce the speed such that a communication error occurs)<\/li><li>Partition Schema: Initial value<\/li><li>Core Debug Level: None<\/li><li>Serial port: COMn (n is a number, <a href=\"#comport\">see below for details<\/a>)<\/li><li>Specify the following.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?resize=768%2C411&#038;ssl=1\" alt=\"Tool setting\" class=\"wp-image-23007\" width=\"768\" height=\"411\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?resize=1024%2C548&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?resize=300%2C161&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?resize=768%2C411&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?resize=1536%2C822&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-13.png?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Board_management\"><\/span>Board management<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>In the Arduino IDE menu, go to Tools -&gt; Board -&gt; ESP32 Arduino to see the list.<\/p>\n\n\n\n<p>If you don&#8217;t see M5Stack-ATOM in the list, update the esp32 from the Boards Manager.<\/p>\n\n\n\n<p>Select Tools -&gt; Board -&gt; Boards Manager.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?resize=768%2C412&#038;ssl=1\" alt=\"Boards Manager\" class=\"wp-image-23009\" width=\"768\" height=\"412\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?resize=1024%2C549&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?resize=300%2C161&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?resize=768%2C412&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?resize=1536%2C824&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-14.png?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<p>Selecting &#8220;Updateable&#8221; for the type will narrow down the list to only those boards that can be updated.<\/p>\n\n\n\n<p>The esp32 version 1.0.4 did not show M5Stack-ATOM in the list, so I changed the &#8220;Select Version&#8221; field to the latest (as of May 2021) 1.0.6 and clicked the &#8220;Update&#8221; button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-08.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-08.png?resize=738%2C416&#038;ssl=1\" alt=\"Board Manager esp32\" class=\"wp-image-22996\" width=\"738\" height=\"416\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-08.png?w=984&amp;ssl=1 984w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-08.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-08.png?resize=768%2C432&amp;ssl=1 768w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/a><\/figure>\n\n\n\n<p>After updating the board esp32 version to 1.0.6, the list now shows M5Stack-Core2 and others in addition to M5Stack-ATOM.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?resize=768%2C413&#038;ssl=1\" alt=\"M5Stack ATOM\" class=\"wp-image-23010\" width=\"768\" height=\"413\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?resize=1024%2C551&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?resize=300%2C162&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?resize=768%2C414&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?resize=1536%2C827&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-15.png?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Checking_the_COM_port\"><\/span>Checking the COM port<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>Connect the PC (Type-A) and ATOM Lite (Type-C) with a USB cable.<\/p>\n\n\n\n<p>A COM port will be assigned automatically, so specify it from the Arduino IDE menu.<\/p>\n\n\n\n<p>If you have only one device connected to the serial port, select Tools -&gt; Serial Port from the Arduino IDE menu, and only one COM port will be displayed.<\/p>\n\n\n\n<p>When multiple COM ports are displayed, check the COM port (in Windows 10) as follows<\/p>\n\n\n\n<p>Right-click on the Windows Start menu and select Device Manager.<\/p>\n\n\n\n<p>The Device Manager will appear, and while displaying the ports (COM and LPT), plug and unplug the USB cable to find out that the newly added COM port is the ATOM Lite COM port.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-12.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-12.png?resize=732%2C536&#038;ssl=1\" alt=\"Check the OCM port in Device Manager\" class=\"wp-image-23005\" width=\"732\" height=\"536\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-12.png?w=976&amp;ssl=1 976w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-12.png?resize=300%2C220&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-12.png?resize=768%2C563&amp;ssl=1 768w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/a><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Source_Code\"><\/span>Source Code<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>optical-sensor-atom-en.ino<\/p>\n\n\n<pre class=\"line-numbers\"><code class=\"lang-cpp\">\/**\n *  Created on 2021-04-27\n *  \n *  Light sensor unit detects the lighting of rotary beacon light.\n *  When detected, the LED changes to red and then advertises for a certain number of seconds.\n * \n *  @author: Souichirou Kukuchi\n *\/\n#include \"M5Atom.h\"\n#include &lt;BLEDevice.h&gt; \/\/ Bluetooth Low Energy \n#include &lt;BLEServer.h&gt; \/\/ Bluetooth Low Energy\n#include &lt;BLEUtils.h&gt; \/\/ Bluetooth Low Energy\n\n#define T_ID 3101 \/\/  terminal ID\n#define T_PERIOD 10 \/\/ Number of seconds to send advertizing packets\n#define A_PIN 32 \/\/ Analog Pin number\n#define D_PIN 26 \/\/ digital Pin nunber\n#define LED_GREEN 0xf00000\n#define LED_RED 0x00f000\n#define LED_BLUE 0x0000f0\n#define LED_WHITE 0x707070\n\nRTC_DATA_ATTR static uint8_t seq;\n\nuint16_t analogRead_value = 0; \/\/ Small: bright, Large: dark 0\uff5e4095\nuint16_t digitalRead_value = 0; \/\/ 0: bright. 1: dark\nuint16_t terminalid = T_ID; \/\/ Terminal ID\n\nvoid setAdvData(BLEAdvertising *pAdvertising) { \/\/ Formatting Advertising Packets\n  BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();\n  oAdvertisementData.setFlags(0x06); \/\/ BR_EDR_NOT_SUPPORTED | General Discoverable Mode\n\n  std::string strServiceData = \"\";\n  strServiceData += (char)0x0a; \/\/ Length(10Byte)\n  strServiceData += (char)0xff; \/\/ AD Type 0xFF: Manufacturer specific data\n  strServiceData += (char)0xff; \/\/ Test manufacture ID low byte\n  strServiceData += (char)0xff; \/\/ Test manufacture ID high byte\n  strServiceData += (char)seq; \/\/ sequence\n  strServiceData += (char)(analogRead_value &amp; 0xff); \/\/ Lower byte of analog\n  strServiceData += (char)((analogRead_value &gt;&gt; 8) &amp; 0xff); \/\/ High byte of analog\n  strServiceData += (char)(digitalRead_value &amp; 0xff);  \/\/ Lower byte of digital\n  strServiceData += (char)((digitalRead_value &gt;&gt; 8) &amp; 0xff); \/\/ High byte of digital\n  strServiceData += (char)(terminalid &amp; 0xff); \/\/ Lower byte of terminal ID\n  strServiceData += (char)((terminalid &gt;&gt; 8) &amp; 0xff); \/\/ High byte of terminal ID\n\n  oAdvertisementData.addData(strServiceData);\n  pAdvertising-&gt;setAdvertisementData(oAdvertisementData);\n}\n\nvoid setup() {\n  M5.begin(true, false, true); \/\/ initialize UART, I2C, LED \n  delay(50);\n  M5.dis.drawpix(0, LED_GREEN); \/\/ LED color change\n  pinMode(D_PIN, INPUT_PULLUP); \/\/ Set to digital input\n}\n\nvoid loop() {\n  analogRead_value = analogRead(A_PIN); \/\/ Analog\n  digitalRead_value = digitalRead(D_PIN); \/\/ Digital\n \n  if (digitalRead_value == 0){ \/\/ When the rotary beacon light glows\n    M5.dis.drawpix(0, LED_RED); \/\/ LED color change\n    BLEDevice::init(\"blepub-01\"); \/\/ Initialize the device\n    BLEServer *pServer = BLEDevice::createServer();  \/\/ Create a server\n\n    BLEAdvertising *pAdvertising = pServer-&gt;getAdvertising(); \/\/ Get the advertised object.\n    setAdvData(pAdvertising); \/\/ Set the advertizing data.\n\n    pAdvertising-&gt;start(); \/\/ advertisement launch\n    delay(T_PERIOD * 1000); \/\/ Advertise T_PERIOD seconds\n    pAdvertising-&gt;stop(); \/\/ Stop advertisement\n\n    seq++; \/\/ Update sequence number\n    M5.dis.drawpix(0, LED_GREEN); \/\/ LED color change\n  }\n  delay(100);\n}<\/code><\/pre>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Supplementary_explanation\"><\/span>Supplementary explanation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Please refer to the comments in the source code for details, and I would like to make a few additions.<\/p>\n\n\n\n<p>The lighting of the rotary beacon light is judged by the digital value (digitalRead_value) of the light sensor, but it can also be judged by the analog value (analogRead_value).<\/p>\n\n\n\n<p>In this case, I used the digital value and adjusted it with the variable resistance of the light sensor itself.<\/p>\n\n\n\n<p>Also, the data for advertising is assembled with setAdvData in line 29.<\/p>\n\n\n\n<p>For details on the data layout, please refer to the <a href=\"https:\/\/www.souichi.club\/en\/m5stack\/m5stickc-ble\/#setAdvData\" target=\"_blank\" rel=\"noreferrer noopener\">setAdvData chapter of the previous article<\/a>.<\/p>\n\n\n\n<p>The company ID is 0xff * 2 for testing.<\/p>\n\n\n\n<p>The upper limit of the message size is 31Byte, so it is not possible to send such a large size of data.<\/p>\n\n\n\n<p>The items that are being advertised are<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Analog value<\/li><li>Digital value<\/li><li>Terminal ID (unique value for each optical sensor)<\/li><\/ul>\n\n\n\n<p>The following three items are being sent.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Writing_the_program\"><\/span>Writing the program<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><br>Select Sketch -&gt; Upload from the Arduino IDE menu, or press Ctrl+U to compile and write the program (sketch) executable to the ATOM Lite.<\/p>\n\n\n\n<p>If the writing is successful, the message &#8220;Done uploading&#8221; will be displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?resize=768%2C412&#038;ssl=1\" alt=\"Sketch Upload\" class=\"wp-image-23004\" width=\"768\" height=\"412\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?resize=1024%2C549&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?resize=300%2C161&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?resize=768%2C412&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?resize=1536%2C824&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-11.png?w=1920&amp;ssl=1 1920w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Raspberry_Pi\"><\/span>Raspberry Pi<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The program structure of the Raspberry Pi on the side receiving the data is as follows.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Directory_structure\"><\/span>Directory structure<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The following directory structure is used.<\/p>\n\n\n<pre><code class=\"lang-shell\">\u251c\u2500$HOME\/.local\/\n\u2502  \u2502      \n\u2502  \u251c\u2500optical-sensor\n\u2502  \u2502  \u2502  optical_sensor_catch.py\n\u2502  \u2502  \u2502\n\u2502  \u2502  \u251c\u2500\u2500log\n\u2502  \u2502  \u2502  error.log\n\u2502  \u2502  \u2502<\/code><\/pre>\n\n\n<p>The programs were saved in a directory under $HOME\/.local\/<\/p>\n\n\n\n<p>error.log is a file that stores the error log when an exception occurs for some reason.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Source_Code-2\"><\/span>Source Code<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>optical_sensor_catch_en.py<\/p>\n\n\n<pre class=\"line-numbers\"><code class=\"lang-python\"># -*- coding: utf-8 -*-\n\"\"\"\nCreated on Wed Apr 28 16:20:42 2021\n\n\u30fbDisplay the value of the data sent by BLE from M5Stack ATOM Lite.\n\n@author: Souichirou Kikuchi\n\"\"\"\n\nfrom bluepy.btle import DefaultDelegate, Scanner, BTLEException\nimport sys\nimport struct\nimport os\nimport csv\nfrom datetime import datetime as dt\n\nclass ScanDelegate(DefaultDelegate):\n    def __init__(self): # constructor\n        try:\n            DefaultDelegate.__init__(self)\n            self.lastseq = None\n            self.lasttime = dt.fromtimestamp(0)\n        except:\n            ex, ms, tb = sys.exc_info()\n            self.put_error_log(type(ms))\n\n    def handleDiscovery(self, dev, isNewDev, isNewData):\n        try:\n            if isNewDev or isNewData: # New device or new data\n                for (adtype, desc, value) in dev.getScanData(): # Repeat for as many data as you need.\n                    if desc == 'Manufacturer' and value[0:4] == 'ffff': # companyID for testing\n                        delta = dt.now() - self.lasttime\n                        # Only use the first one retrieved.\n                        if value[4:6] != self.lastseq and delta.total_seconds() &gt; 11:\n                            self.lastseq = value[4:6] # Save Seq and time\n                            self.lasttime = dt.now()\n                            (analog, digital, terminal_id) = struct.unpack('&lt;hhh', bytes.fromhex(value[6:])) # h is a 2Byte integer (take out 3)\n                            print('Analog: {0}, Digital: {1}, Terminal ID: {2} '.format( analog, digital, terminal_id))\n        except:\n            ex, ms, tb = sys.exc_info()\n            self.put_error_log(type(ms))\n\n    def put_error_log(self, message): # Output the error log file.\n        ERR_LOG_FILE = '.\/log\/error.log'\n        if (os.path.isfile(ERR_LOG_FILE)): # When the file exists\n            f = open(ERR_LOG_FILE, 'a') # Loading in append mode\n        else: # If there is no file, create a header.\n            f = open(ERR_LOG_FILE, 'w') # Read in write mode\n        writer = csv.writer(f)\n        writer.writerow(['{0:%Y-%m-%d %H:%M:%S.%f}'.format(dt.now()), message])\n        f.close()\n\nif __name__ == \"__main__\":\n    scanner = Scanner().withDelegate(ScanDelegate())\n    try:\n        print('Optical Sensor Catch Start') # Program start\n        while True:\n            scanner.scan(5.0) # Scan. Let ScanDelegate take care of the rest after finding the device.\n    except BTLEException:\n        ex, ms, tb = sys.exc_info()\n        print('BLE exception '+str(type(ms)) + ' at ' + sys._getframe().f_code.co_name)\n        pass\n    finally:\n        print('Optical Sensor Catch End')<\/code><\/pre>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Supplementary_explanation-2\"><\/span>Supplementary explanation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Please refer to the comments in the program for details.<\/p>\n\n\n\n<p>When a BLE signal is found by repeated scanning, the handleDiscovery method of the ScanDelegate class is executed.<\/p>\n\n\n\n<p>In the handleDiscovery method, in addition to checking the company ID, only the first data received is targeted since it will be advertised continuously for 10 seconds.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Execution\"><\/span>Execution<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Since the program accesses the BLE module, it needs to be run with sudo and with administrative privileges.<\/p>\n\n\n<pre><code class=\"lang-shell\">cd ~\/.local\/optical-sensor\nsudo python optical_sensor_catch_en.py<\/code><\/pre>\n\n\n<p>The execution result is as follows.<\/p>\n\n\n\n<p>When you shine light on the light sensor, the value of the light sensor is displayed on the console of the Raspberry Pi.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-16.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-16.png?resize=768%2C479&#038;ssl=1\" alt=\"Execution result (Raspy console)\" class=\"wp-image-23014\" width=\"768\" height=\"479\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-16.png?resize=1024%2C638&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-16.png?resize=300%2C187&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-16.png?resize=768%2C478&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-en-16.png?w=1026&amp;ssl=1 1026w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"At_the_end\"><\/span>At the end.<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a class=\"r_image\" href=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?resize=194%2C256&#038;ssl=1\" alt=\"rotary beacon light\" class=\"wp-image-20572\" width=\"194\" height=\"256\" srcset=\"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?resize=774%2C1024&amp;ssl=1 774w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?resize=227%2C300&amp;ssl=1 227w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?resize=768%2C1016&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?resize=1161%2C1536&amp;ssl=1 1161w, https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-19.jpg?w=1329&amp;ssl=1 1329w\" sizes=\"auto, (max-width: 194px) 100vw, 194px\" \/><\/a><\/figure>\n\n\n\n<p>The position of the rotary beacon light and the light sensor needs to be adjusted at the factory with a variable resistor.<\/p>\n\n\n\n<p>The line at the factory is about 20 meters long, but the signal was received even with BLE4.2 (Bluetooth Low Energy).<\/p>\n\n\n\n<p>Also, the Raspberry Pi program in this article only displays information on the console.<\/p>\n\n\n\n<p>In reality, we are building a system that uses the information received from each sensor to inform the user of error conditions using voice.<\/p>\n\n\n\n<p>This concludes this article.<\/p>\n\n\n<div class=\"postscript_balloon\">Finally.<\/div>\n\n\n<p>I hope this article will be useful to someone somewhere.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>M5Stack&#8217;s light sensor detects the lighting of the rotary beacon light and sends the lighting information to the Raspberry Pi via BLE (Bluetooth Low Enegy).<\/p>\n","protected":false},"author":5,"featured_media":20567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/www.souichi.club\/?p=20502","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[238],"tags":[138,11,166,223],"class_list":["post-22923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-m5stack","tag-python","tag-programming","tag-166","tag-223","en-US"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.souichi.club\/wp-content\/uploads\/m5stack-optical-sensor-18.jpg?fit=1920%2C1564&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/22923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/comments?post=22923"}],"version-history":[{"count":1,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/22923\/revisions"}],"predecessor-version":[{"id":24943,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/posts\/22923\/revisions\/24943"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/media\/20567"}],"wp:attachment":[{"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/media?parent=22923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/categories?post=22923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.souichi.club\/wp-json\/wp\/v2\/tags?post=22923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}