{"id":408,"date":"2015-03-22T22:55:00","date_gmt":"2015-03-22T21:55:00","guid":{"rendered":"http:\/\/heppg.de\/ikg\/wordpress\/?p=408"},"modified":"2015-07-03T15:13:06","modified_gmt":"2015-07-03T13:13:06","slug":"timer-for-hc-sr04-ultrasonic-sensor-and-scratch","status":"publish","type":"post","link":"https:\/\/heppg.de\/ikg\/wordpress\/?p=408","title":{"rendered":"Timer for HC-SR04 ultrasonic sensor and scratch"},"content":{"rendered":"<p>The ultrasonic sensor HC-SR04 is a quite inexpensive ultrasonic sensor, well suited for microcontroller applications.<br \/>\nIt produces pulse width modulation directly giving the signal run time.<\/p>\n<p>For 1.7m, at sound speed in air of 340m\/s, you get 10ms pulse width.<br \/>\nUp to now, my scratchClient software had no ability to support this device. In order to evaluate different approaches to measure pulse width timings in this range, I have set up an arduino board simulating this device with 10ms timing.<br \/>\nThe goal was to have around 1000 measurements and look for the distribution of values.<\/p>\n<p>Approach A was to take python code as found on the net. Two loops, first one looking for raising pulse and second one looking for falling pulse.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/python.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-409\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/python.png\" alt=\"python\" width=\"400\" height=\"376\" \/><\/a><br \/>\nResults are not very precise.<br \/>\nThere are quite a lot of measurements at 10ms, but 2\/3 of all results are up to 40% higher; few also till 16,5 ms. This is as expected, as the IO system is slow, and a lot of other things are running in the system in parallel.<br \/>\nAdvantage is that there are no additional cost, except some voltage dividers needed.<\/p>\n<p>Approach B is a microcontroller subsystem using the atmel 328 chip, sitting on a breadboard and connected with SPI to RPI. The internal oscillator is used for simplicity and reduced cost. This setup is similiar to the adc-setup described in <a title=\"atmel328 as adc\" href=\"http:\/\/heppg.de\/ikg\/wordpress\/?p=209\">another post<\/a>.<br \/>\nTime measurement is using the 16 bit timer in 1\/64 resolution, yielding 8us values.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/ultrasonic_328.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-410\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/03\/ultrasonic_328.png\" alt=\"ultrasonic_328\" width=\"332\" height=\"326\" \/><\/a><br \/>\nResults are pretty nice, although the deviation is 3% from 10.0ms. Most possible this is caused by the free running oscillator running a little bit too fast; some more investigation is needed here to eliminate software errors.<\/p>\n<p>The code is polling the timer, which is fast enough for this application. SPI is handled with an interrupt coded in assembler. This allows up to 240kHz SPI frequency.<br \/>\nThe software is completed and supports up to 4 sonic sensors.<br \/>\nCost is prox 3.5$ per unit, some soldering and some voltage dividers. No difficult software setup needed.<\/p>\n<p>I found other approaches on the net. DMA code, C-Code or alike. Problem here is the complicated software setup (no ready to use solutions) and the difficult integration into the python environment with RPI.GPIO.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/04\/atmel_ultrasonic_Steckplatine.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-426 size-full\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2015\/04\/atmel_ultrasonic_Steckplatine.png\" alt=\"\" width=\"2562\" height=\"2175\" \/><\/a><\/p>\n<p>The hardware setup is straightforward. Note the 5V on the supply voltage of the HC-SR04. Doublecheck also the ground connection from HCSR04 to breadboard and to Raspberry. This is important to avoid to get 5V into the raspberry inputs.<\/p>\n<p>For the scratch setup, there is a sample configuration with device(0) connected.<\/p>\n<pre>cd ~\/scatchClient\r\nsudo python src\/scratchClient.py -c config\/config_hcsr04_atmel328.xml<\/pre>\n<p>In scratch, enable remote sensor connections. You receive &#8216;distance_A&#8217;.<\/p>\n<p>For detailed instructions on how to program the atmel processor, see the documentation in the download file on <a title=\"scratchClient Download\" href=\"http:\/\/heppg.de\/ikg\/wordpress\/?page_id=6\">download page<\/a>. The programming is performed by the raspberry pi with the SPI cabling.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ultrasonic sensor HC-SR04 is a quite inexpensive ultrasonic sensor, well suited for microcontroller applications. It produces pulse width modulation directly giving the signal run time. For 1.7m, at sound speed in air of 340m\/s, you get 10ms pulse width. Up to now, my scratchClient software had no ability to support this device. In order [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,3],"tags":[24,25,23],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-atmel328-coprocessor","category-scratchclient","tag-hc-sr04","tag-range-finder","tag-sonar"],"_links":{"self":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=408"}],"version-history":[{"count":10,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions\/427"}],"wp:attachment":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}