{"id":18,"date":"2011-01-20T15:30:31","date_gmt":"2011-01-20T14:30:31","guid":{"rendered":"http:\/\/www.netquote.it\/nqmain\/?p=18"},"modified":"2011-01-26T22:22:42","modified_gmt":"2011-01-26T21:22:42","slug":"maxsonar","status":"publish","type":"post","link":"https:\/\/www.netquote.it\/nqmain\/2011\/01\/maxsonar\/","title":{"rendered":"MaxSonar sensor and pySerial"},"content":{"rendered":"<p><a href=\"http:\/\/www.maxbotix.com\/uploads\/LV-MaxSonar-EZ0-Datasheet.pdf\">LV-MaxSonar-EZ0<\/a> sensor by <a href=\"http:\/\/www.maxbotix.com\/\">MaxBotix<\/a>.<\/p>\n<p>Interfacing with computer serial port and Python via pySerial module.<\/p>\n<p>Download <a href=\"http:\/\/www.python.org\/\">Python<\/a> and <a href=\"http:\/\/sourceforge.net\/projects\/pyserial\/files\/pyserial\/2.5\/\">pySerial<\/a>.<\/p>\n<p>The wiring is so trivial. Start from data sheet we need 4 pins: GND, VCC, TX, RX.<br \/>\nSensor operating voltage is from 2.5V to 5.5V and the current is about 2mA, then we can get power from serial port DTR or RTS pins. Look at this <a href=\"http:\/\/www.tkk.fi\/Misc\/Electronics\/circuits\/rspower.html\">explanation<\/a>. ATTENTION! RS-232 specification says that tension can be 25V. You can simply BURN the sonar, computer, house and more&#8230; Don&#8217;t do anything if you don&#8217;t know what are you doing.<\/p>\n<p>In my case I am using USB to RS-232 converter. Output voltage is 5V so I don&#8217;t need additional circuitry, just simple pin to pin wiring. <\/p>\n<p><a href=\"http:\/\/www.netquote.it\/nqmain\/2011\/01\/maxsonar\/rs232_maxsonar\/\" rel=\"attachment wp-att-19\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/rs232_maxsonar.jpg\" alt=\"\" title=\"rs232 maxsonar pinout\" width=\"600\" height=\"485\" class=\"alignnone size-full wp-image-19\" srcset=\"https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/rs232_maxsonar.jpg 600w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/rs232_maxsonar-150x121.jpg 150w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/rs232_maxsonar-300x242.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.netquote.it\/nqmain\/2011\/01\/maxsonar\/maxsonar_connect\/\" rel=\"attachment wp-att-20\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/maxsonar_connect.jpg\" alt=\"\" title=\"maxsonar connect\" width=\"600\" height=\"181\" class=\"alignnone size-full wp-image-20\" srcset=\"https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/maxsonar_connect.jpg 600w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/maxsonar_connect-150x45.jpg 150w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/maxsonar_connect-300x90.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>And now little coding&#8230;<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n# Import pySerial module\r\nfrom serial import *\r\n\r\n# Open serial port and setup\r\nser = Serial(&quot;COM10&quot;, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=1)\r\n\r\n# Power up MaxSonar via DTR pin\r\nser.setDTR(True)\r\n\r\n# Infinite loop\r\nwhile (True):\r\n\r\n    # Read serial data\r\n    data = ser.read(5)\r\n\r\n    # Check data length \r\n    if len(data) == 5:\r\n\r\n        # Check data packet\r\n        if data&#x5B;0] == &quot;R&quot; and data&#x5B;1:4].isdigit():\r\n\r\n            # Convert to cm and print \r\n            print int(data&#x5B;1:4]) * 2.54, &quot;cm&quot;\r\n\r\n    # Request next measurement        \r\n    ser.write(0)\r\n\r\n# Close \r\nser.close()\r\n\r\n<\/pre>\n<p>Many thanks to <strong>Antonio Bugnone<\/strong> for soldering.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LV-MaxSonar-EZ0 sensor by MaxBotix. Interfacing with computer serial port and Python via pySerial module. Download Python and pySerial. The wiring<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":15,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":56,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/18\/revisions\/56"}],"wp:attachment":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}