<?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>Brett&#039;s Build Log &#187; relay</title>
	<atom:link href="http://brettinman.com/tag/relay/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettinman.com</link>
	<description>Hardware tinkering with the occasional bit of software</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:24:26 +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>Sketching with the USB Power Shield</title>
		<link>http://brettinman.com/2009/07/16/sketching-with-the-usb-power-shield/</link>
		<comments>http://brettinman.com/2009/07/16/sketching-with-the-usb-power-shield/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 05:04:25 +0000</pubDate>
		<dc:creator>brettinman</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[USB Power Shield]]></category>
		<category><![CDATA[Woot-Off Lights Project]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[relay]]></category>
		<category><![CDATA[sketch]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://brettinman.com/?p=132</guid>
		<description><![CDATA[So you have a USB Power Shield (or anything that uses a latching relay). How do you control it in an Arduino Sketch? Normal relays are pretty simple &#8211; supply voltage/current to the coil and it generates a magnetic field, which pushes the switch to the ON position. You only need one pin to run [...]]]></description>
			<content:encoded><![CDATA[<p>So you have a USB Power Shield (or anything that uses a latching relay). How do you control it in an Arduino Sketch?</p>
<p>Normal relays are pretty simple &#8211; supply voltage/current to the coil and it generates a magnetic field, which pushes the switch to the ON position. You only need one pin to run it, although you may need a transistor to supply more current because most microcontrollers can only supply 50mA, which often isn&#8217;t enough current to create a magnetic field to move the switch. You must maintain the current to keep the switch on.</p>
<p>Latching relays are similar to this, but once put into position, it &#8220;latches&#8221; there and requires no further current to stay in that position. This uses less power and makes the switching more reliable &#8211; if there is a decrease in current supplied, the switch will still stay in position, unlike the regular relay which could flicker. The downside is that the common types require two pins. Sending current in one direction switches on, and reversing the current switches it off.</p>
<p>So how do you control it? Set two pins to be outputs (the USB Power Shield uses 12/11 and 10/9 for the two relays). Write one pin LOW and the other pin HIGH. After a sufficient time to make the switching happen (50ms or less, check the datasheet for the relay), switch the HIGH pin back to low. This gives the magnetic field a place to sink, as well as making sure no more current is supplied to the coil, preventing overheating and wasting power. </p>
<p>Ideally, with a normal relay you&#8217;d like to force the stored current from the magnetic field back through the coil by using a flyback diode across it, but with a latched relay and current running in both directions, a diode would be needed in each direction. And that would simply cause a short circuit both ways. I&#8217;ve tested the relays used in the USB Power Shield and they don&#8217;t harm the Arduino (even over thousands of cycles) so this isn&#8217;t a huge worry here.</p>
<p>Below is some sample code for using the USB Power Shield with an Arduino. <span id="more-132"></span></p>
<p>Simply set up the pins for relay1 and relay2:<br />
<code>//give the pins names<br />
int relay11 = 12;<br />
int relay12 = 11;<br />
int relay21 = 10;<br />
int relay22 = 9;<br />
void setup()<br />
{<br />
//set the pins to outputs<br />
  pinMode(relay11, OUTPUT);<br />
  pinMode(relay12, OUTPUT);<br />
  pinMode(relay21, OUTPUT);<br />
  pinMode(relay22, OUTPUT);<br />
//make sure they start in the off position<br />
  relay1(false);<br />
  relay2(false);<br />
  // do other stuff<br />
}</code></p>
<p>Then add these functions to turn the relays on and off:<br />
<code>void relay1 (bool state)<br />
{<br />
  if (state)<br />
  {<br />
    digitalWrite(relay12,LOW); //set the current in one direction<br />
    digitalWrite(relay11,HIGH);<br />
    delay(50); //hold long enough to switch<br />
    digitalWrite(relay11,LOW);   //set both to ground to stop current<br />
  }<br />
  else<br />
  {<br />
    digitalWrite(relay11,LOW);<br />
    digitalWrite(relay12,HIGH);<br />
    delay(50);<br />
    digitalWrite(relay12,LOW);<br />
  }<br />
}<br />
void relay2 (bool state)<br />
{<br />
  if (state)<br />
  {<br />
    digitalWrite(relay22,LOW);<br />
    digitalWrite(relay21,HIGH);<br />
    delay(50);<br />
    digitalWrite(relay21,LOW);<br />
  }<br />
  else<br />
  {<br />
    digitalWrite(relay21,LOW);<br />
    digitalWrite(relay22,HIGH);<br />
    delay(50);<br />
    digitalWrite(relay22,LOW);<br />
  }<br />
}</code></p>
<p>Then simply write your main loop to do whatever you wish. Call relay1(true) to turn relay1 on, call relay2(false) to turn relay2 off, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettinman.com/2009/07/16/sketching-with-the-usb-power-shield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
