{"id":8035,"date":"2012-11-13T17:00:05","date_gmt":"2012-11-13T22:00:05","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=8035"},"modified":"2020-05-07T08:29:02","modified_gmt":"2020-05-07T12:29:02","slug":"python-arduino-tk-toggling-an-led","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2012\/11\/13\/python-arduino-tk-toggling-an-led\/","title":{"rendered":"python + Arduino + Tk: Toggling an LED"},"content":{"rendered":"<p><em><strong><span style=\"color: #ff0000;\">Whoa! This is so old I don&#8217;t even know where to start!<\/span><br \/>\n<\/strong><\/em><\/p>\n<ul>\n<li>It&#8217;s using Python 2, so if it works at all it probably won&#8217;t for much longer, and Tkinter is something completely different under Python 3<br \/>\n(<em>grrreat<\/em> planning there, Python guys \u00e2\u20ac\u00a6)<\/li>\n<li><em>pyfirmata<\/em> is likely ancient history too.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8036\" title=\"Screen Shot 2012-11-13 at 16.32.13\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/11\/Screen-Shot-2012-11-13-at-16.32.13-.png\" alt=\"\" width=\"164\" height=\"160\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/11\/Screen-Shot-2012-11-13-at-16.32.13-.png 164w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/11\/Screen-Shot-2012-11-13-at-16.32.13--160x156.png 160w\" sizes=\"auto, (max-width: 164px) 100vw, 164px\" \/>Phil sent me a note last week asking how to turn an LED on or off using Python talking through Firmata to an Arduino. This was harder than it looked.<\/p>\n<p>It turns out the hard part is getting the value from the Tkinter Checkbutton itself. It seems that some widgets don&#8217;t return values directly, so you must read the widget&#8217;s value with a <code>get()<\/code> method. This appears to work:<\/p>\n<pre><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/python\n# turn an LED on\/off with a Tk Checkbutton - scruss 2012\/11\/13\n# Connection:\n# - small LED connected from D3, through a resistor, to GND\n\nimport pyfirmata\nfrom Tkinter import *\n\n# Create a new board, specifying serial port\n# board = pyfirmata.Arduino('\/dev\/ttyACM0') # Raspberry Pi\nboard = pyfirmata.Arduino('\/dev\/tty.usbmodem411') # Mac\n\nroot = Tk()\nvar = BooleanVar()\n\n# set up pins\npin3 = board.get_pin('d:3:o') # D3 On\/Off Output (LED)\n\ndef set_led():  # set LED on\/off\n    ledval = var.get()\n    print &quot;Toggled&quot;, ledval\n    pin3.write(ledval)\n\n# now set up GUI\nb = Checkbutton(root, text = &quot;LED&quot;, command = set_led,\n                variable = var)\nb.pack(anchor = CENTER)\n\nroot.mainloop()\n<\/pre>\n<p>This is explained quite well here: <a href=\"http:\/\/stackoverflow.com\/questions\/6469254\/tkinter-checkbutton-doesnt-change-my-variable\">Tkinter Checkbutton doesn&#8217;t change my variable &#8211; Stack Overflow<\/a>. I also learnt a couple of things about my previous programs:<\/p>\n<ul>\n<li>You don&#8217;t really need to set up an Iterator unless you&#8217;re reading analogue inputs<\/li>\n<li>My &#8220;clever&#8221; cleanup-on-exit code actually made the script hang on Mac OS.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Whoa! This is so old I don&#8217;t even know where to start! It&#8217;s using Python 2, so if it works at all it probably won&#8217;t for much longer, and Tkinter is something completely different under Python 3 (grrreat planning there, Python guys \u00e2\u20ac\u00a6) pyfirmata is likely ancient history too. Phil sent me a note last [&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,150,2540,2544],"class_list":["post-8035","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-arduino","tag-firmata","tag-led","tag-python","tag-tkinter"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-25B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/8035","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=8035"}],"version-history":[{"count":11,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/8035\/revisions"}],"predecessor-version":[{"id":16248,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/8035\/revisions\/16248"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=8035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=8035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=8035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}