{"id":7996,"date":"2012-10-28T22:07:37","date_gmt":"2012-10-29T02:07:37","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=7996"},"modified":"2018-11-17T09:50:29","modified_gmt":"2018-11-17T14:50:29","slug":"servo-control-from-pyfirmata-arduino","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2012\/10\/28\/servo-control-from-pyfirmata-arduino\/","title":{"rendered":"Servo Control from pyfirmata + arduino"},"content":{"rendered":"<p><em><span style=\"color: #ff0000;\"><strong>Hey! This article is really old! So old, in fact, that I clearly thought that saying (ahem) \u00e2\u20ac\u0153<\/strong><\/span><\/em><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">w00t w00t<\/span><\/span><em><span style=\"color: #ff0000;\"><strong>\u00e2\u20ac\u009d was a good idea. Information here may be misleading and possibly wrong. You probably want to be using a <a href=\"https:\/\/github.com\/firmata\/arduino#firmata-client-libraries\">newer client library<\/a> and you <span style=\"text-decoration: underline;\">definitely<\/span> want to use an <a 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><\/span><\/em><\/p>\n<p><a href=\"https:\/\/bitbucket.org\/tino\/pyfirmata\/src\">pyFirmata<\/a>&#8216;s documentation is, to be charitable, sparse. After writing <a href=\"http:\/\/scruss.com\/blog\/2012\/08\/16\/raspberry-pi-python-arduino-and-a-gui\/\">Raspberry Pi, Python &amp; Arduino *and* a GUI<\/a> (which should be making an appearance in <a href=\"http:\/\/www.themagpi.com\/\">The MagPi<\/a> soon, <del>w00t w00t<\/del> yeet!), I looked at pyFirmata again to see what it could do. That pretty much meant digging through the source.<\/p>\n<p>Firmata can drive hobby servos, and if you&#8217;re not driving too many, you can run them straight from the Arduino with no additional power. I used a standard cheapo-but-decent Futaba <a href=\"http:\/\/www.futaba-rc.com\/servos\/analog.html\">S3003<\/a>, which gives you about 180\u00c2\u00b0 of motion. The particular one I tried started to make little growly noises past 175\u00c2\u00b0, so in the example below, that&#8217;s hardcoded as the limit.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\n# -*- coding: utf-8 -*-\r\n# move a servo from a Tk slider - scruss 2012-10-28\r\n\r\nimport pyfirmata\r\nfrom Tkinter import *\r\n\r\n# don't forget to change the serial port to suit\r\nboard = pyfirmata.Arduino('\/dev\/tty.usbmodem26271')\r\n\r\n# start an iterator thread so\r\n# serial buffer doesn't overflow\r\niter8 = pyfirmata.util.Iterator(board)\r\niter8.start()\r\n\r\n# set up pin D9 as Servo Output\r\npin9 = board.get_pin('d:9:s')\r\n\r\ndef move_servo(a):\r\n    pin9.write(a)\r\n\r\n# set up GUI\r\nroot = Tk()\r\n\r\n# draw a nice big slider for servo position\r\nscale = Scale(root,\r\n    command = move_servo,\r\n    to = 175,\r\n    orient = HORIZONTAL,\r\n    length = 400,\r\n    label = 'Angle')\r\nscale.pack(anchor = CENTER)\r\n\r\n# run Tk event loop\r\nroot.mainloop()\r\n<\/pre>\n<p>The code above makes a slider (oh, okay, a Tkinter <em>Scale<\/em> widget) that moves the servo connected to Arduino pin D9 through its whole range. To set the servo position, you just need to write the angle value to the pin.<\/p>\n<p>I haven&#8217;t tried this with the Raspberry Pi yet. It wouldn&#8217;t surprise me if it needed external power to drive the Arduino and the servo. This might be a good excuse to use my <a href=\"http:\/\/omegamcu.com\/id16.html\">Omega-328U<\/a> board \u00e2\u20ac\u201d it&#8217;s Arduino code compatible, runs from an external power supply, and has Signal-Voltage-Ground (SVG) connectors that the servo cable would just plug straight into.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey! This article is really old! So old, in fact, that I clearly thought that saying (ahem) \u00e2\u20ac\u0153w00t w00t\u00e2\u20ac\u009d was a good idea. 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 Arduino IDE \u00e2\u2030\u00a5 1.6 and not the [&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,2578,2544],"class_list":["post-7996","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-arduino","tag-firmata","tag-python","tag-servo","tag-tkinter"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-24Y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7996","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=7996"}],"version-history":[{"count":9,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7996\/revisions"}],"predecessor-version":[{"id":15255,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7996\/revisions\/15255"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=7996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=7996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=7996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}