{"id":7774,"date":"2012-08-12T22:48:50","date_gmt":"2012-08-13T02:48:50","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=7774"},"modified":"2014-01-07T21:40:20","modified_gmt":"2014-01-08T02:40:20","slug":"controlling-an-arduino-from-raspberry-pi-using-processing","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2012\/08\/12\/controlling-an-arduino-from-raspberry-pi-using-processing\/","title":{"rendered":"Controlling an Arduino from Raspberry Pi using Processing"},"content":{"rendered":"<p><span style=\"color: #ff0000;\"><strong>Hey<\/strong>! This article is <em>really<\/em> old! The code might still work, but I&#8217;ve updated the installation instructions for Processing 2.1 and <del>Sun<\/del> Oracle Java here: <a href=\"http:\/\/scruss.com\/blog\/2014\/01\/07\/processing-2-1-oracle-java-raspberry-pi-serial-arduino-%e2%98%ba\/\">Processing 2.1 + Oracle Java + Raspberry Pi + Serial + Arduino = \u00e2\u02dc\u00ba<\/a>.<\/span><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7775\" title=\"arduino IO on Raspberry Pi from Processing\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-201002_254x153_scrot.png\" width=\"254\" height=\"153\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-201002_254x153_scrot.png 254w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-201002_254x153_scrot-160x96.png 160w\" sizes=\"auto, (max-width: 254px) 100vw, 254px\" \/><\/p>\n<p>This might not look like much, but it was a lot of work to get here. It&#8217;s the display from a small <a href=\"http:\/\/processing.org\/\">Processing<\/a> sketch, running on a <a href=\"http:\/\/www.raspberrypi.org\/\">Raspberry Pi<\/a>, talking to an <a href=\"http:\/\/arduino.cc\">Arduino<\/a> controlling the brightness of an LED with the slider, and reading from an <a href=\"http:\/\/www.ti.com\/product\/lm35\">LM35<\/a> temperature sensor.<\/p>\n<p>I wanted to see if I could get graphical control of an Arduino on the Raspberry Pi. I wrote about the simplest sketch in Processing that combined output (to control a small green LED through a resistor) and input (from an LM35, that simplest of sensors). This is how it looks running on a slightly faster machine than the Raspberry Pi:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0382.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-7782\" title=\"LED half bright\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0382-320x320.jpg\" width=\"320\" height=\"320\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0382-320x320.jpg 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0382-160x160.jpg 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0382-300x300.jpg 300w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0382.jpg 640w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">LED at half brightness, LM35 showing 25\u00c2\u00b0C<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0384.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-7783\" title=\"LED off\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0384-320x320.jpg\" width=\"320\" height=\"320\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0384-320x320.jpg 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0384-160x160.jpg 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0384-300x300.jpg 300w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0384.jpg 640w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">LED off, sensor at 26\u00c2\u00b0C<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0385.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-7784\" title=\"LED full on, LM35 warmed up\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0385-320x320.jpg\" width=\"320\" height=\"320\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0385-320x320.jpg 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0385-160x160.jpg 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0385-300x300.jpg 300w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/IMG_0385.jpg 640w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a>LED full on, LM35 warmed up<\/p>\n<p>I had the same results on the Raspberry Pi; just much, much slower. The sketch is below the fold.<\/p>\n<h2>Running Processing on Raspberry Pi<\/h2>\n<p>Processing is both written in and generates <a href=\"https:\/\/www.java.com\/en\/\">Java<\/a>, so there&#8217;s some hope that it can run on most platforms. <a href=\"http:\/\/scruss.com\/blog\/2014\/01\/07\/processing-2-1-oracle-java-raspberry-pi-serial-arduino-%E2%98%BA\/\"><strong>Up-to-date installation instructions<\/strong><\/a>. <del>These instructions are modified from\u00c2\u00a0<a href=\"http:\/\/www.pobot.org\/Processing-sur-Raspberry-Pi.html\">Processing sur Raspberry Pi<\/a>, for which thanks are given to the original author:<\/del><\/p>\n<ol>\n<li><del>Install the JDK and Java serial library: <code>sudo apt-get install librxtx-java openjdk-6-jdk<\/code><\/del><\/li>\n<li><del>Download the Linux version of <a href=\"http:\/\/processing.org\/download\/\">Processing<\/a>, and unpack it to somewhere permanent in your home directory<\/del><\/li>\n<li><del>Delete the java folder in the Processing directory; for me, that was <code>processing-1.5.1\/java<\/code><\/del><\/li>\n<li><del>Replace that java folder with a link to your system&#8217;s installation: <code>ln -s \/usr\/lib\/jvm\/java-6-openjdk-armhf java<\/code><\/del><\/li>\n<li><del>In the Processing folder, remove or rename <code>modes\/java\/libraries\/serial\/library\/linux32\/librxtxSerial.so<\/code>; it&#8217;s an x86 binary, and will fail<\/del><\/li>\n<li><del>In the Processing folder, also remove <code>modes\/java\/libraries\/serial\/library\/RXTXcomm.jar<\/code>, and replace it with a copy of <code>\/usr\/share\/java\/RXTXcomm.jar<\/code><\/del><br \/>\n<del> (If you don&#8217;t do this, you&#8217;ll get a warning: &#8220;WARNING:\u00c2\u00a0 RXTX Version mismatch&#8221;, and any serial comms will fail.)<\/del><\/li>\n<li>Download and install the <a href=\"http:\/\/www.sojamo.de\/libraries\/controlP5\/\">controlP5<\/a> library for Processing<\/li>\n<li>Download and install the <a href=\"http:\/\/www.arduino.cc\/playground\/interfacing\/processing\">Arduino<\/a> library for Processing<\/li>\n<li>Program your Arduino with <a href=\"https:\/\/github.com\/firmata\/arduino\">Firmata<\/a>; the version that comes with the Arduino software is <del>fine<\/del> a bit old.<\/li>\n<li>Connect your Arduino to the Raspberry Pi with a USB cable; it may require external power.<\/li>\n<\/ol>\n<p>Now fire up Processing. It <del>will<\/del> used to take a while to start up<del>, and will throw the following warning:<\/del><\/p>\n<p><del><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7777\" title=\"2012-08-12-192031_483x204_scrot\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192031_483x204_scrot.png\" width=\"483\" height=\"204\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192031_483x204_scrot.png 483w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192031_483x204_scrot-160x67.png 160w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192031_483x204_scrot-320x135.png 320w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/del><\/p>\n<p>Despite this, it should eventually should start up fine:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7776\" title=\"2012-08-12-192145_500x559_scrot\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192145_500x559_scrot.png\" width=\"500\" height=\"559\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192145_500x559_scrot.png 500w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192145_500x559_scrot-143x160.png 143w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192145_500x559_scrot-286x320.png 286w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2012\/08\/2012-08-12-192145_500x559_scrot-268x300.png 268w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p><del>Now, this is <em>slow<\/em>. It takes tens of seconds to start up. It might not be the most practical development tool, but<\/del> Processing sketches are very portable, so you can develop on one machine, and then run on the Raspberry Pi.<\/p>\n<p>The code at the end of this article expects:<\/p>\n<ul>\n<li>an Arduino running the Firmata DAQ sketch attached to a USB port;<\/li>\n<li>a small LED connected from digital pin 3, through a 1k\u00ce\u00a9 resistor to ground;<\/li>\n<li>an LM35 with the following connections: +Vs \u00e2\u2020\u2019 +5V, Vout \u00e2\u2020\u2019 analogue pin 0, and GND \u00e2\u2020\u2019 GND.<\/li>\n<\/ul>\n<p>If you run this, <del>after about half a minute<\/del>, the blank sketch window appears, <del>and about half a minute later,<\/del> the slider and temperature reading appears. If it doesn&#8217;t, there&#8217;s a good chance that the serial libraries are wrong. Try this sketch:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nimport processing.serial.*;\r\nimport cc.arduino.*;\r\n\r\nArduino arduino;\r\nprintln(Arduino.list());\r\n<\/pre>\n<p>This should return a number and a serial port where the Arduino was found; something like &#8216;[0] \/dev\/ttyACM0&#8217;.<\/p>\n<p>What I really want to do now is get this same hardware running with Python and tkinter. It&#8217;s not that Python&#8217;s my favourite language; it&#8217;s just that the Raspberry Pi Foundation chose Python as the official language for the board. I&#8217;d rather work to further the aims of this educational foundation rather than work against it. <del>Processing&#8217;s pretty much unworkably slow on the Raspberry Pi \u00e2\u20ac\u201d but it does work!<\/del><\/p>\n<p><!--more--><br \/>\nHere&#8217;s the example code:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ arduino_firmata_io_example\r\n\/\/ control an LED's brightness on pin 3\r\n\/\/ read an LM35 on analogue 0\r\n\/\/ scruss - 2012-08-12\r\n\r\nimport processing.serial.*;\r\nimport cc.arduino.*;\r\nimport controlP5.*;\r\nControlP5 controlP5;\r\nArduino arduino;\r\n\r\nint ledPin = 3;       \/\/ digital 3; PWM capable\r\nint lm35pin = 0;      \/\/ analogue 0\r\nint brightness=128;   \/\/ LED brightness, 0-255\r\nint lastbrightness=0; \/\/ store brightness to stop flicker\r\n\r\nvoid setup()\r\n{\r\n  size(250, 50);\r\n  controlP5 = new ControlP5(this);\r\n  \/\/ horizontal controlP5 slider bound to brightness variable\r\n  controlP5.addSlider(&quot;brightness&quot;,0,255,128,90,12,128,16);\r\n  \/\/ set up first Arduino we find\r\n  arduino = new Arduino(this, Arduino.list()&#x5B;0]);\r\n  arduino.pinMode(ledPin, Arduino.OUTPUT);\r\n}\r\n\r\nvoid draw() {\r\n  background(240);             \/\/ blank screen\r\n  fill(0,0,0);                 \/\/ black text\r\n  text(&quot;Brightness:&quot;, 15, 25); \/\/ slider label\r\n  \/\/ read LM35 and print\r\n  text(&quot;Temperature: &quot; + str(int(read_lm35(lm35pin))) + &quot; \u00c2\u00b0C&quot;, 15, 48);\r\n  if (brightness != lastbrightness) {\r\n    \/\/ LED flickers if we keep doing analogWrite() with same value\r\n    \/\/  so only write if slider brightness value has changed\r\n    arduino.analogWrite(ledPin, brightness);\r\n  }\r\n  lastbrightness=brightness;\r\n}\r\n\r\nfloat read_lm35(int pin) {\r\n  \/\/ read lm35 on pin, and return float \u00c2\u00b0C\r\n  \/\/ assumes 5V supply, and 0-100\u00c2\u00b0C range\r\n  return (5.0 * arduino.analogRead(pin) * 100.0)\/1024.0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hey! This article is really old! The code might still work, but I&#8217;ve updated the installation instructions for Processing 2.1 and Sun Oracle Java here: Processing 2.1 + Oracle Java + Raspberry Pi + Serial + Arduino = \u00e2\u02dc\u00ba. This might not look like much, but it was a lot of work to get here. [&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,1334,2541,2510,2542],"class_list":["post-7774","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-arduino","tag-firmata","tag-java","tag-processing","tag-raspberrypi","tag-serial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-21o","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7774","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=7774"}],"version-history":[{"count":12,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7774\/revisions"}],"predecessor-version":[{"id":10167,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7774\/revisions\/10167"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=7774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=7774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=7774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}