{"id":7793,"date":"2012-08-14T23:22:40","date_gmt":"2012-08-15T03:22:40","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=7793"},"modified":"2018-11-17T09:55:15","modified_gmt":"2018-11-17T14:55:15","slug":"raspberry-pi-python-arduino","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2012\/08\/14\/raspberry-pi-python-arduino\/","title":{"rendered":"Raspberry Pi, Python &#038; Arduino"},"content":{"rendered":"<p><span style=\"color: #ff0000;\"><em><strong>Hey! This article is really old! So old, in fact, that it really only exists to track down content farms that like to knock off my articles (oh hai, <a href=\"https:\/\/circuitdigest.com\/microcontroller-projects\/controlling-arduino-with-raspberry-pi-using-pyfirmata\">CircuitDigest<\/a>!)<\/strong><\/em><em><strong>. Information here may be misleading and possibly wrong. You probably want to be using a <a style=\"color: #ff0000;\" href=\"https:\/\/github.com\/firmata\/arduino#firmata-client-libraries\">newer client library<\/a> and you definitely want to use an <a style=\"color: #ff0000;\" href=\"https:\/\/www.arduino.cc\/en\/Main\/Software\">Arduino IDE \u00e2\u2030\u00a5 1.6<\/a> and not the ancient one that comes with Raspbian.<\/strong><\/em><\/span><\/p>\n<p>After the other night&#8217;s <a href=\"http:\/\/scruss.com\/blog\/2012\/08\/12\/controlling-an-arduino-from-raspberry-pi-using-processing\/\">wonderfully slow detour into Processing<\/a>, I thought I&#8217;d try the Raspberry Pi&#8217;s &#8220;native&#8221; language of Python to control an Arduino. This worked rather well, though I don&#8217;t have a slick GUI for it yet.<\/p>\n<p><a href=\"https:\/\/bitbucket.org\/tino\/pyfirmata\/src\">pyFirmata<\/a> is the magic that allows an Arduino running Firmata to talk to Python. It&#8217;s fairly easy to install under Raspbian:<\/p>\n<ol>\n<li>Get the required packages:<br \/>\n<code>sudo apt-get install python-serial mercurial<\/code><\/li>\n<li>Download the pyFirmata code:<br \/>\n<code>hg clone https:\/\/bitbucket.org\/tino\/pyfirmata<br \/>\ncd pyfirmata<br \/>\nsudo python setup.py install<\/code><br \/>\n(If this succeeds, you can delete the pyfirmata folder.)<\/li>\n<\/ol>\n<p>Using pyFirmata is a bit different from other Arduino applications:<\/p>\n<ul>\n<li>Analogue reads and PWM writes are normalized to a 0 .. 1 range, and not the standard Arduino 0 .. 255 and 0 .. 1023.<\/li>\n<li>You really need to start a separate iterator thread to stop old readings overflowing the serial buffer<\/li>\n<li>Since the Arduino is read asynchronously, make sure that the pyFirmata connection is fully initialized before reading from ports. Otherwise, <em>None<\/em> values ensue.<\/li>\n<\/ul>\n<p>Here&#8217;s some code that uses the same hardware as before, but simply reports the temperature and ramps the brightness of the LED up in 10% steps.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\n# -*- coding: utf-8 -*-\r\n\r\n# simple test of pyfirmata and Arduino; read from an LM35 on A0,\r\n#\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 brighten an LED on D3 using PWM\r\n# scruss, 2012-08-14 - tested on Arduino Uno &amp; Raspberry Pi (Raspbian)\r\n\r\nimport pyfirmata\r\n\r\n# Create a new board, specifying serial port\r\nboard = pyfirmata.Arduino('\/dev\/ttyACM0')\r\n\r\n# start an iterator thread so that serial buffer doesn't overflow\r\nit = pyfirmata.util.Iterator(board)\r\nit.start()\r\n\r\n# set up pins\r\npin0=board.get_pin('a:0:i')\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 # A0 Input\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 (LM35)\r\npin3=board.get_pin('d:3:p')\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 # D3 PWM Output (LED)\r\n\r\n# IMPORTANT! discard first reads until A0 gets something valid\r\nwhile pin0.read() is None:\r\n    pass\r\n\r\nfor i in range(10):\r\n    pin3.write(i\/10.0)\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 # set D3 to 0, 10%, 20%, ... brightness\r\n    print &quot;PWM: %d %% Temperature %.1f \u00c2\u00b0C&quot; % (i * 10, pin0.read() * 5 * 100)\r\n    board.pass_time(1)\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 # pause 1 second\r\n\r\npin3.write(0)\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 # turn LED back off\r\nboard.exit()\r\n<\/pre>\n<p>The output from this might look like:<\/p>\n<pre>PWM: 0 % Temperature 24.9 \u00c2\u00b0C\r\nPWM: 10 % Temperature 24.9 \u00c2\u00b0C\r\nPWM: 20 % Temperature 24.9 \u00c2\u00b0C\r\nPWM: 30 % Temperature 25.9 \u00c2\u00b0C  &lt;-\r\nPWM: 40 % Temperature 26.9 \u00c2\u00b0C    |\r\nPWM: 50 % Temperature 28.3 \u00c2\u00b0C    | I was holding the LM35 here\r\nPWM: 60 % Temperature 28.8 \u00c2\u00b0C    | to make the temperature rise\r\nPWM: 70 % Temperature 29.8 \u00c2\u00b0C    |\r\nPWM: 80 % Temperature 29.8 \u00c2\u00b0C    | \r\nPWM: 90 % Temperature 29.8 \u00c2\u00b0C  &lt;-<\/pre>\n<p>If this doesn&#8217;t work, check the output of <code>dmesg<\/code> to see if you&#8217;re using the right port. You could try this little test script<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\n# -*- coding: utf-8 -*-\r\n\r\nimport pyfirmata\r\n\r\nPORT = '\/dev\/ttyACM0'\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 # change this to suit\r\nboard = pyfirmata.Arduino(PORT)\r\nprint 'pyFirmata version:\\t%s' % pyfirmata.__version__\r\nprint 'Hardware:\\t\\t%s' % board.__str__()\r\nprint 'Firmata firmware:\\t%i.%i' % (board.get_firmata_version()&#x5B;0],\r\n                                    board.get_firmata_version()&#x5B;1])\r\nboard.exit()\r\n<\/pre>\n<p>which should generate something like<\/p>\n<pre>pyFirmata version:\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a00.9.4\r\nHardware:\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a0Arduino \/dev\/ttyACM0 on \/dev\/ttyACM0\r\nFirmata firmware:\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a02.3<\/pre>\n<p>Next time, I&#8217;ll try to wrap this in a tkinter GUI. But for now, pyFirmata is a much quicker way than Processing to talk to an Arduino. But there is hope of <a href=\"http:\/\/www.oracle.com\/us\/corporate\/press\/1735645\">a faster Java for the Raspberry Pi<\/a> &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey! This article is really old! So old, in fact, that it really only exists to track down content farms that like to knock off my articles (oh hai, CircuitDigest!). Information here may be misleading and possibly wrong. You probably want to be using a newer client library and you definitely want to use an [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[2207,2543,2540,2510],"class_list":["post-7793","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-arduino","tag-firmata","tag-python","tag-raspberrypi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-21H","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=7793"}],"version-history":[{"count":11,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7793\/revisions"}],"predecessor-version":[{"id":15256,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7793\/revisions\/15256"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=7793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=7793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=7793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}