{"id":140,"date":"2008-01-31T14:10:30","date_gmt":"2008-01-31T19:10:30","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/arduinowiring\/140"},"modified":"2008-03-29T17:38:43","modified_gmt":"2008-03-29T22:38:43","slug":"sensor-graphing-with-3-lines-of-code","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/140\/","title":{"rendered":"Sensor graphing with 3 lines of code!"},"content":{"rendered":"<p>I&#8217;ve been looking for simple ways to graph the data from a sensor attached to a microcontroller lately, because it&#8217;s such a necessary activity if you want to look at sensor data over time. Using Apples Grapher program, which comes with OSX, I found a simple way that involves only four lines of code on an Arduino or Wiring microcontroller, and produces graphs like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2008\/01\/graph.png\" alt=\"graph.png\" style=\"border: 3px dotted #000000; margin-top: 3px; margin-right: 3px; margin-left: 3px\" height=\"266\" width=\"480\" \/><\/p>\n<p><!--more--><\/p>\n<p>Grapher lets you import text files with data point values. So here&#8217;s the procedure:<\/p>\n<ol>\n<li style=\"list-style: none\"><\/li>\n<li>Program microcontroller to send out comma-separated time and sensor values.<\/li>\n<li>Use screen program to save serial values to a file<\/li>\n<li>convert file to a comma-separated value file (.csv)<\/li>\n<li>open file in Grapher<\/li>\n<\/ol>\n<p>\n<strong>1. Program the microcontroller<\/strong> Here&#8217;s the code for a Wiring or Arduino microcontroller. It prints the time in 100ths of a second, and the sensor reading at that time:<\/p>\n<pre>\n\nvoid setup() {\n\t\/\/ initialize the serial port:\n\tSerial.begin(9600);\n}\n\nvoid loop() {\n\t\/\/ print the time, in 100ths of a second:\n\tSerial.print(millis()\/10);\n\t\/\/ print a comma:\n\tSerial.print(\",\");\n\t\/\/ print the sensor value (connected to analog pin 0):\n\tSerial.println(analogRead(0));\n}\n\n<\/pre>\n<p>Note that the last line is a println(). That makes sure that each sensor reading gets its own line.<strong>2. Use screen to save the values<\/strong><br \/>\nThe screen program in OSX and other Unix and Linux operating systems is a command-line program that allows you to see data coming in the serial port. You can learn more about using it here. It can also save data to a file, using the -L option. To use it, open a Terminal window. Change directories to the Desktop by typing:<\/p>\n<pre>\ncd Desktop\n<\/pre>\n<p>\nNow open the screen program by typing:<\/p>\n<pre>\nscreen -L serialportname\n<\/pre>\n<p>\nwhere serialportname is the name of your serial port. For example, a typical Arduino module (or FTDI USB-to-serial adaptor) might show up as \/dev\/tty.usb-A400133P. So you&#8217;d type <tt>screen -L \/dev\/tty.usb-A400133P<\/tt>.<br \/>\nAs you hit the enter key to start the screen program, hit the rest button on your microcontroller. This will reset the time. A few seconds later, data will flow across the screen. When you&#8217;ve got enough data, type control-C to stop it, then <tt>control-A<\/tt> followed by <tt>control-&#92;<\/tt> to close the screen program.<br \/>\n<strong>3. Convert the file to a .csv file<\/strong><br \/>\nIf you look on the Desktop, you&#8217;ll see a file called screenlog.0. Open it in a text editor and save it as a text-only file called <tt>screenlog.csv<\/tt>.<br \/>\n<strong>4. Graph the data<\/strong> Now open the Grapher application. It lives in the Applications\/Utilities directory. Open a new default graph. From the Equation menu, choose &#8220;New Point Set&#8221;. A few new points will appear on the screen:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2008\/01\/new-points.png\" alt=\"new_points.png\" style=\"margin: 3px\" height=\"359\" width=\"480\" \/> Click &#8220;Edit Points&#8221; and you&#8217;ll get a list of the points and their values. Select all the points and delete them.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2008\/01\/select-points.png\" alt=\"select_points.png\" style=\"margin: 3px\" height=\"356\" width=\"480\" \/> Then click &#8220;Import &#8221; and import your screenlog.csv file. Zoom out until you see the data points, and that&#8217;s it! For two sensor values at a time, you can send three values and view the graph in the 3D view as well.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2008\/01\/graphed-points.png\" alt=\"graphed_points.png\" style=\"margin: 3px\" height=\"340\" width=\"480\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been looking for simple ways to graph the data from a sensor attached to a microcontroller lately, because it&#8217;s such a necessary activity if you want to look at sensor data over time. Using Apples Grapher program, which comes with OSX, I found a simple way that involves only four lines of code on &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/140\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sensor graphing with 3 lines of code!&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}