<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stealthcopter.com &#187; arduino</title>
	<atom:link href="http://www.stealthcopter.com/blog/category/arduino/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stealthcopter.com/blog</link>
	<description>Android, Linux, Python and stealthcopters</description>
	<lastBuildDate>Sat, 24 Jul 2010 00:01:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>DIY soil moisture sensors</title>
		<link>http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/</link>
		<comments>http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 12:26:29 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[microscope]]></category>
		<category><![CDATA[sensor]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=836</guid>
		<description><![CDATA[I&#8217;ve been looking into creating an automated herbarium of some sort for a while, and I came across the brilliant post about creating some DIY soil moisture sensors using nails and plaster of paris. cheapvegetablegardener from hackaday. I wont explain all of the theory or background as it is already explained on cheapvegetablegardener. I measured [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been looking into creating an automated herbarium of some sort for a while, and I came across the brilliant post about creating some DIY soil moisture sensors using nails and plaster of paris. <a href="http://www.cheapvegetablegardener.com/2009/11/how-to-make-cheap-soil-moisture-sensor-2.html">cheapvegetablegardener</a> from <a href="http://hackaday.com/2010/03/15/soil-moisture-sensing/">hackaday</a>.</p>
<div id="attachment_837" class="wp-caption aligncenter" style="width: 512px"><a href="http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/cimg0411/" rel="attachment wp-att-837"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/03/CIMG0411-502x799.jpg" alt="Plaster of paris humidity sensor" title="Plaster of paris humidity sensor" width="502" height="799" class="size-medium wp-image-837" /></a><p class="wp-caption-text">Plaster of paris humidity sensor</p></div>
<div id="attachment_838" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/cimg0412/" rel="attachment wp-att-838"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/03/CIMG0412-600x450.jpg" alt="Plaster of paris humidity sensor with wires attached " title="Plaster of paris humidity sensor with wires attached " width="600" height="450" class="size-medium wp-image-838" /></a><p class="wp-caption-text">Plaster of paris humidity sensor with wires attached </p></div>
<div id="attachment_839" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/cimg0409/" rel="attachment wp-att-839"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/03/CIMG0409-600x450.jpg" alt="Both of the sensors" title="Both of the sensors" width="600" height="450" class="size-medium wp-image-839" /></a><p class="wp-caption-text">Both of the sensors</p></div>
<p>I wont explain all of the theory or background as it is already explained on <a href="http://www.cheapvegetablegardener.com/2009/11/how-to-make-cheap-soil-moisture-sensor-2.html">cheapvegetablegardener</a>.</p>
<div id="attachment_841" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/cimg0413/" rel="attachment wp-att-841"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/03/CIMG0413-600x450.jpg" alt="Resistance experiment on moisture sensor" title="Resistance experiment on moisture sensor" width="600" height="450" class="size-medium wp-image-841" /></a><p class="wp-caption-text">Resistance experiment on moisture sensor</p></div>
<p>I measured the resistance of the sensor in air to be 12Kohms, I then placed the sensor into water (keeping the nail heads above the surface). The resistance dropped to 4Kohms whilst in the water, and then over 20 mins of back in air the resistance rose to 5.6Kohms. The sample still looks and feels quite wet, so I imagine it is going to take some time to dry, but from these prelimary results we can see it appears to work.</p>
<p>Soldering the wires to the nails before creating the plaster of paris along with covering the end sensor with hot glue would improve its longevity. I will post again once I find a better mould to make the plaster in as it was quite difficult to get them out of the cuvettes  (I had about a 50% success rate). </p>
<p>Here is a microscope image of the surface of the plaster:</p>
<div id="attachment_840" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/2010-03-28-123608/" rel="attachment wp-att-840"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/03/2010-03-28-123608-600x450.jpg" alt="Microscope image of the surface of the plaster of paris" title="Microscope image of the surface of the plaster of paris" width="600" height="450" class="size-medium wp-image-840" /></a><p class="wp-caption-text">Microscope image of the surface of the plaster of paris</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/03/diy-soil-moisture-sensors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: interfacing with an arduino</title>
		<link>http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 11:42:56 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[arduino]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Electronics]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=204</guid>
		<description><![CDATA[So what is an arduino? An arduino is an open source open hardware programmable controller with several inputs and outputs. The image below shows an Ardunio Dicemella. It (Arduino Dicemella) has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>So what is an arduino?</strong><br />
An <a href="http://www.arduino.cc/">arduino</a> is an open source open hardware programmable controller with several inputs and outputs. The image below shows an <a href="http://www.arduino.cc/en/Main/ArduinoBoardDiecimila">Ardunio Dicemella</a>.</p>
<div id="attachment_472" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/arduino.jpg"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/arduino-300x172.jpg" alt="Ardunio Dicemella Annotated Photo" title="Ardunio Dicemella Annotated Photo" width="300" height="172" class="size-medium wp-image-472" /></a><p class="wp-caption-text">Ardunio Dicemella Annotated Photo</p></div>
<blockquote><p>It (Arduino Dicemella) has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.</p></blockquote>
<p>They are very useful for people who know how to program but have little experience with hardware interaction.</p>
<p><strong>Programming the arduino</strong><br />
This post will not contain in-depth detail on how to program the arduino, instead focussing briefly on setting up serial (over serial or usb cable) communications in order to talk to a python script. The arduino can be programmed via a IDE provided by the creators in a C-style hardware language.</p>
<p><strong>Code example</strong></p>
<pre name="code" class="c">
int ledPin = 13;            // choose the pin for the LED
int inputPin = 2;          // choose the input pin (for a pushbutton)
int val = 0;                // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}
</pre>
<div id="attachment_596" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino1.jpg"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino1-300x170.jpg" alt="Arduino LED switch circuit off" title="Arduino LED switch circuit off" width="300" height="170" class="size-medium wp-image-596" /></a><p class="wp-caption-text">Arduino LED switch circuit off</p></div>
<div id="attachment_598" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino2.jpg"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino2-300x170.jpg" alt="Arduino LED switch circuit on" title="Arduino LED switch circuit on" width="300" height="170" class="size-medium wp-image-598" /></a><p class="wp-caption-text">Arduino LED switch circuit on</p></div>
<p>Now we add a few lines to enable the writing of information from our arduino over the serial connection. We first need to set up the transfer speed in our setup (<em>Serial.begin(9600);</em>). Then we can simply send messages over serial using <em>Serial.print(&#8220;message\n&#8221;);</em>. You can choose between print and println with the difference been that the latter automatically appends the newline char, so we would use the former to write multiple things to the same line. Below is our modified code:</p>
<p><strong>Serial write example</strong></p>
<pre name="code" class="c">
int ledPin = 13;           // choose the pin for the LED
int inputPin = 2;         // choose the input pin (for a pushbutton)
int val = 0;               // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
  Serial.begin(9600);
  Serial.print("Program Initiated\n");
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}
</pre>
<p>We now add into this code the ability to receive information via serial. Below is the modified example which removes the action of the button and replaces it by activating the LED when &#8216;Y&#8217; is sent via serial. </p>
<p><strong>Serial read example</strong></p>
<pre name="code" class="c">
int ledPin = 13;  // choose the pin for the LED
int val = 0;      // variable for reading the pin status
char msg = '  ';   // variable to hold data from serial

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  Serial.begin(9600);
  Serial.print("Program Initiated\n");
}

void loop(){
        // While data is sent over serial assign it to the msg
	while (Serial.available()>0){
		msg=Serial.read();
	}

  // Turn LED on/off if we recieve 'Y'/'N' over serial
  if (msg=='Y') {
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
    msg=' ';
  } else if (msg=='N') {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}
</pre>
<p><strong>Interaction with python</strong></p>
<p>First we import the serial library to python in order to communicate with the arduino (this includes talking over usb).</p>
<pre name="code" class="python">import serial</pre>
<p>We then attempt to connect to our arduino on /dev/ttyUSB0, using try and except to catch an exception if we are unable to find the arduino on USB0. The 9600 corresponds to the baud rate (speed of communication) that we are using with the arduino and should be the same as set in the program on the arduino otherwise your communication may appear garbled.</p>
<pre name="code" class="python">try:
	arduino = serial.Serial('/dev/ttyUSB0', 9600)
except:
	print "Failed to connect on /dev/ttyUSB0"
</pre>
<p>The address will be /dev/ttyUSB# where # is replaced by a number for arduinos connected via usb and /dev/ttyS# where # is replaced by a number for arduinos connected via serial. If you are not sure of the location of your arduino, it can be found in the arduino IDE or you can write some python to scroll through possible locations until a response is found</p>
<pre name="code" class="python">
locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']

for device in locations:
	try:
		arduino = serial.Serial(device, 9600)
	except:
		print "Failed to connect on",device
</pre>
<p>You may need to be careful as other devices can be connected. For example if I try to connect to /dev/ttyS0 I will connect to the wacom tablet on my laptop.</p>
<p>Once you have connected to your arduino successfully you can write information to it using write and read information sent from it using read (you will need to import time to use the sleep function). If your arduino does not send any messages via serial then attempting to readline will result in your program hanging until it receives a message.</p>
<pre name="code" class="python">
try:
	arduino.write('Y')
	time.sleep(1)
	print arduino.readline()
except:
	print "Failed to send!"
</pre>
<p>So the python code should now look like the following and we should be able to control the LED over serial.</p>
<pre name="code" class="python">
import serial
import time

locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']  

for device in locations:
	try:
		print "Trying...",device
		arduino = serial.Serial(device, 9600)
		break
	except:
		print "Failed to connect on",device   

try:
    arduino.write('Y')
    time.sleep(1)
    print arduino.readline()
except:
    print "Failed to send!"
</pre>
<p>The above will send the character &#8216;Y&#8217; (Y for Yes please turn on the LED) to the arduino wait for 1 second and then read from the arduino which will have hopefully posted a response to our &#8216;Y&#8217;. Using the program on this should turn the LED on, and report <em>LED Activated</em> back via serial to our python program. This should be enough for people to get started with ardunios and communicating with them in python.</p>
<p><strong>References</strong></p>
<ul>
<li><a href="http://www.arduino.cc/">Arduino</a> &#8211; The arduino website with everything you are likely to need (programming examples and reference guide, and hardware information)</li>
<li>
<a href="http://www.ladyada.net/learn/arduino/">Arduino tutorial </a> &#8211;  a basic and easy to understand tutorial on programming the arduino
</li>
<li><a href="http://lemonodor.com/archives/2008/02/arduino_serial.html/">Python port of arduino-serial.c</a> &#8211; By John Wiseman from which I based my program.</li>
<li><a href="http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/">original arduino-serial.c</a> &#8211; by Tod E. Kurt.</li>
<li><a href="http://www.sparkfun.com">Sparkfun</a> &#8211; Here is a good place to purchase ardunio and other electronics parts. Try <a href="http://www.coolcomponents.co.uk">coolcomponents</a> if your from the uk like me</li>
<li><a href="http://www.dealextreme.com/default.dx/r.27613104">Dealextreme</a> &#8211; Hong Kong based retailer that sells a lot of cheap DIY electronics and also has worldwide free delivery with no min spend (crazy). Does take about two weeks to arrive though (uk).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.022 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-30 10:13:09 -->
