{"id":29242,"date":"2022-07-21T09:29:26","date_gmt":"2022-07-21T16:29:26","guid":{"rendered":"https:\/\/digilent.com\/blog\/?p=29242"},"modified":"2022-07-21T09:29:26","modified_gmt":"2022-07-21T16:29:26","slug":"build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-2","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-2\/","title":{"rendered":"Build a Smart Lamp Using an Analog Discovery Pro ADP3450 &#8211; Part 2"},"content":{"rendered":"<p>This is the second installment of a series of four blog posts explaining how to design a smart RGB-lamp. If you are interested, you can <a href=\"https:\/\/digilent.com\/blog\/build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-1\/\">find Part 1 here<\/a>. By running an app on your smartphone, you can set the brightness and the color of the LED, or turn it off. The core of the system is an <a href=\"https:\/\/digilent.com\/shop\/analog-discovery-pro-3000-series-portable-high-resolution-mixed-signal-oscilloscopes\/\">Analog Discovery Pro ADP3450<\/a>, working in Linux Mode. It receives the settings from the smartphone\u2019s app through a Pmod <a href=\"https:\/\/digilent.com\/shop\/pmod-ble-bluetooth-low-energy-interface\/\">BLE<\/a>, as well as the intensity of the ambient light, which is measured by a Pmod <a href=\"https:\/\/digilent.com\/shop\/pmod-als-ambient-light-sensor\/\">ALS<\/a>\u00a0light sensor.<\/p>\n<p>In the previous post, we developed the hardware abstraction layer (HAL) functions for the <a href=\"https:\/\/digilent.com\/reference\/software\/waveforms\/waveforms-sdk\/start\">Waveforms SDK<\/a>. Now we design the software modules for the Pmods. We will do this similarly as before, but this time the code we create will two Pmods we use for our project and not ADP3450.<\/p>\n<p>The Pmod BLE uses an UART for communication, and as we want to avoid an interruption of the PWM connected to the LED, we are going to implement it in two ways: the first one uses the UART instrument of Waveforms for sending and receiving data, the other one employs the logic analyzer and the pattern generator.<\/p>\n<p>SPI is used to receive data from the Pmod ALS, and we use the Protocol Analyzer in WaveForms for that. As we will also need some I\/O functionality, the Static I\/O instrument is helpful.<\/p>\n<p>Of course, you could write all the functions yourself, but it might be easier to download the module for the <a href=\"https:\/\/digilent.com\/reference\/_media\/test-and-measurement\/analog-discovery-pro-3x50\/pmod_ble.zip\">Pmod BLE from here<\/a>\u00a0and the one for the <a href=\"https:\/\/digilent.com\/reference\/_media\/test-and-measurement\/analog-discovery-pro-3x50\/pmod_als.zip#-1\">Pmod ALS from here<\/a>. These Python scripts contain all the routines for our project, like open(), read(), or close() routines. It is worth noting that not all the functionality in these programs were tested, so there might be errors. Use them on your own responsibility.<\/p>\n<h2>Making Sure the Pmods Work<\/h2>\n<p class=\"iE-Fliesstext\" style=\"text-indent: 0in;\">Before we can test the functionality of our Pmod, we need to connect them to the digital lines of the ADP3450. First connect the Pmod ALS. Have a look at the test-code to the right: it lists pin assignments. Link the CS-pin of the Pmod to the digital line 8 of the ADP 3450, the SDO pin to line 9, and the SCK pin to line 10. Moreover, connect VCC to VIO and the ground lines. <o:p><\/o:p><\/p>\n<p class=\"iE-Fliesstext\">Finally, copy the code to a file in your working directory. Run the script and point a flashlight to the sensor on the Pmod. The intensity level should change.<o:p><\/o:p><\/p>\n<p><strong><em>To test the Pmod ALS, the ambient light intensity is read and displayed continuously.\u00a0<\/em><\/strong><\/p>\n<blockquote><p><em># import modules<\/em><\/p>\n<p>import Pmod_ALS as als<\/p>\n<p>import WF_SDK as wf <em># import WaveForms instruments<\/em><\/p>\n<p>from time import sleep<\/p>\n<p>&nbsp;<\/p>\n<p><em># define pins<\/em><\/p>\n<p>als.pins.cs = 8<\/p>\n<p>als.pins.sdo = 9<\/p>\n<p>als.pins.sck = 10<\/p>\n<p>&nbsp;<\/p>\n<p>try:<\/p>\n<p><em># initialize the interface<\/em><\/p>\n<p>device_data = wf.device.<strong>open<\/strong>()<\/p>\n<p><em># check for connection errors<\/em><\/p>\n<p>wf.device.check_error(device_data)<\/p>\n<p>als.<strong>open<\/strong>()<\/p>\n<p>&nbsp;<\/p>\n<p>while <strong>True<\/strong>:<\/p>\n<p><em># display measurements<\/em><\/p>\n<p>light = als.read_percent(rx_mode=&#8221;static&#8221;, reopen=<strong>True<\/strong>)<\/p>\n<p>print(&#8220;static: &#8221; + <strong>str<\/strong>(light) + &#8220;%&#8221;)<\/p>\n<p>light = als.read_percent(rx_mode=&#8221;spi&#8221;, reopen=<strong>True<\/strong>)<\/p>\n<p>print(&#8220;spi: &#8221; + <strong>str<\/strong>(light) + &#8220;%&#8221;)<\/p>\n<p>sleep(0.5)<\/p>\n<p>&nbsp;<\/p>\n<p>except <strong>KeyboardInterrupt<\/strong>:<\/p>\n<p>pass<\/p>\n<p>finally:<\/p>\n<p><em># close the device<\/em><\/p>\n<p>als.close(reset=<strong>True<\/strong>)<\/p>\n<p>wf.device.close(device_data)<\/p><\/blockquote>\n<p>Repeat the same exercise for the Pmod BLE. Again, the test script lists the connections. Copy the file to your hard drive as well and run it.\u00a0<em><strong>To test the Pmod BLE, all messages received on Bluetooth are displayed, the string &#8220;ok&#8221; is sent as a response.\u00a0<\/strong><\/em><\/p>\n<blockquote><p><em># import modules<\/em><\/p>\n<p>import Pmod_BLE as ble<\/p>\n<p>import WF_SDK as wf <em># import WaveForms instruments<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><em># define pins<\/em><\/p>\n<p>ble.pins.tx = 4<\/p>\n<p>ble.pins.rx = 3<\/p>\n<p>ble.pins.rst = 5<\/p>\n<p>ble.pins.status = 6<\/p>\n<p>&nbsp;<\/p>\n<p><em># turn on messages<\/em><\/p>\n<p>ble.settings.DEBUG = <strong>True<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>try:<\/p>\n<p><em># initialize the interface<\/em><\/p>\n<p>device_data = wf.device.<strong>open<\/strong>()<\/p>\n<p><em># check for connection errors<\/em><\/p>\n<p>wf.device.check_error(device_data)<\/p>\n<p>ble.<strong>open<\/strong>()<\/p>\n<p>ble.reset(rx_mode=&#8221;uart&#8221;, tx_mode=&#8221;uart&#8221;, reopen=<strong>True<\/strong>)<\/p>\n<p>ble.reboot()<\/p>\n<p>&nbsp;<\/p>\n<p>while <strong>True<\/strong>:<\/p>\n<p><em># check connection status<\/em><\/p>\n<p>if ble.get_status():<\/p>\n<p><em># receive data<\/em><\/p>\n<p>data, sys_msg, error = ble.read(blocking=<strong>True<\/strong>, rx_mode=&#8221;logic&#8221;, reopen=<strong>False<\/strong>)<\/p>\n<p><em># display data and system messages<\/em><\/p>\n<p>if data != &#8220;&#8221;:<\/p>\n<p>print(&#8220;data: &#8221; + data)\u00a0 <em># display it<\/em><\/p>\n<p>ble.write_data(&#8220;ok&#8221;, tx_mode=&#8221;pattern&#8221;, reopen=<strong>False<\/strong>)\u00a0 <em># and send response<\/em><\/p>\n<p>elif sys_msg != &#8220;&#8221;:<\/p>\n<p>print(&#8220;system: &#8221; + sys_msg)<\/p>\n<p>elif error != &#8220;&#8221;:<\/p>\n<p>print(&#8220;error: &#8221; + error)\u00a0\u00a0\u00a0 <em># display the error<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>except <strong>KeyboardInterrupt<\/strong>:<\/p>\n<p>pass<\/p>\n<p>finally:<\/p>\n<p><em># close the device<\/em><\/p>\n<p>ble.close(reset=<strong>True<\/strong>)<\/p>\n<p>wf.device.close(device_data)<\/p><\/blockquote>\n<p>In addition, download the <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.macdom.ble.blescanner&amp;_ga=2.112624192.574416802.1658216205-1945391217.1658216205\">BLE Scanner<\/a>\u00a0application to your phone and start it. Turn on Bluetooth, if it is not already running, as well as the location. The app should display the Pmod BLE after some time. Connect to it and jot down the long code (MAC address) below the device\u2019s name. You will need it later.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/07\/Figure-1-Connecting-the-BLE-Scanner-337x600.jpg\" alt=\"\" width=\"337\" height=\"600\" class=\"aligncenter size-medium wp-image-29245\" data-wp-pid=\"29245\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/07\/Figure-1-Connecting-the-BLE-Scanner-337x600.jpg 337w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/07\/Figure-1-Connecting-the-BLE-Scanner.jpg 393w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/p>\n<p>Open the Custom Service dialog and hit the \u201cN\u201c (notify) icon and some system messages should show in the terminal of the test script. The \u201cGot it\u201c string should appear on the custom characteristic screen of the app. Now tap \u201cW\u201c and type in your text. Write down the value for the UUID and the characteristic. As before, you will need them later.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/07\/Figure-2-Configuring-the-BLE-Scanner-337x600.jpg\" alt=\"\" width=\"337\" height=\"600\" class=\"aligncenter size-medium wp-image-29246\" data-wp-pid=\"29246\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/07\/Figure-2-Configuring-the-BLE-Scanner-337x600.jpg 337w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/07\/Figure-2-Configuring-the-BLE-Scanner.jpg 393w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/p>\n<p>This concludes the second installment of this blog-post series. In the next edition, we will create the controlling Android app and the LED and charger circuits.<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-29242 jlk' data-task='like' data-post_id='29242' data-nonce='f013a4e301' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-29242 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-29242 jlk' data-task='unlike' data-post_id='29242' data-nonce='f013a4e301' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-29242 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-29242 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>This is the second installment of a series of four blog posts explaining how to design a smart RGB-lamp. If you are interested, you can find Part 1 here. By &hellip; <\/p>\n","protected":false},"author":61,"featured_media":29229,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4322,4312,1563],"tags":[4352,4353,4368],"ppma_author":[4506],"class_list":["post-29242","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-accessories","category-usb-scopes-analyzers-generators","category-guide","tag-adp3450","tag-linux","tag-pmod"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/06\/smartlamp1.jpg","jetpack_sharing_enabled":true,"authors":[{"term_id":4506,"user_id":61,"is_guest":0,"slug":"richardoed","display_name":"Richard Oed","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/12de1ad12c6f52d1668873102baa76018ec3d4f2078318739209f4b75051c424?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=29242"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29242\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/29229"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=29242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=29242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=29242"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=29242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}