<?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>AlferSoft Blog &#187; Electronic Projects</title>
	<atom:link href="http://www.alfersoft.com.ar/blog/category/elec/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alfersoft.com.ar/blog</link>
	<description>Explaining this blog in a few words since 1999</description>
	<lastBuildDate>Sun, 29 Aug 2010 13:55:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>USocket &#8211; USB controlled Socket with PIC18F4550</title>
		<link>http://www.alfersoft.com.ar/blog/2008/07/08/usocket-usb-controlled-socket-with-pic18f4550/</link>
		<comments>http://www.alfersoft.com.ar/blog/2008/07/08/usocket-usb-controlled-socket-with-pic18f4550/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 04:06:25 +0000</pubDate>
		<dc:creator>fvicente</dc:creator>
				<category><![CDATA[Electronic Projects]]></category>
		<category><![CDATA[CDC]]></category>
		<category><![CDATA[circuit]]></category>
		<category><![CDATA[electronic]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[PIC16F4550]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[USB]]></category>

		<guid isPermaLink="false">http://www.alfersoft.com.ar/blog/?p=9</guid>
		<description><![CDATA[Introduction The idea of this project is to control (switch off/on) two power sockets with a computer by using its USB port. I&#8217;ve chosen USB in first place because I wanted to experiment with the PIC18F4550 microchip&#8217;s microcontroller, and secondly because the power supplied by this port (500mA) is enough to activate a relay without [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>The idea of this project is to control (switch off/on) two power sockets with a computer by using its USB port. I&#8217;ve chosen USB in first place because I wanted to experiment with the PIC18F4550 microchip&#8217;s microcontroller, and secondly because the power supplied by this port (500mA) is enough to activate a relay without any additional power supply.</p>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/proto04.jpg" alt="Prototype" width="450" height="338" /></p>
<p><span id="more-9"></span></p>
<p>The firmware is based in <a title="SIXCA USBDAQ" href="http://www.sixca.com/eng/articles/usbdaq/">SIXCA USBDAQ</a> which is in turn based on <a title="Microchip's CDC sample" href="http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&amp;nodeId=2124&amp;param=en532204&amp;page=wwwFullSpeedUSB">microchip&#8217;s CDC sample</a>. USBDAQ is extremely easy to use. It implements a very simple set of ASCII commands to turn on/off the digital outputs, that I use to control the two relays. I just needed to adjust the bMaxPower in order to negotiate the 500mA with the host, and I also changed the vendor ID and the name of the device to USocket.</p>
<p>I&#8217;ve also made the schematic and board design of the circuit with <a title="CadSoft Eagle" href="http://www.cadsoftusa.com/">Eagle</a>, but I never used it since I made it on a strip board <img src='http://www.alfersoft.com.ar/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> , so if you plan to use it please double check everything before.</p>
<p>The whole project including PIC firmware and Eagle files is available for download. You are free to use or modify it as you like, but please verify Microchip&#8217;s CDC sample license if you plan to use it for commercial applications.</p>
<p><span class="BODYTEXT"><span class="BODYTEXT"><strong>Disclaimer:</strong> This project involves high voltage electricity. You should not attempt this project unless you are comfortable with basic concepts of AC and DC electricity, induction, and reading circuit schematics. You and your adult supervisor are responsible for your safety when doing this project!</span></span></p>
<p><strong>Advice:</strong> Despites the high voltage part of the circuit is virtually isolated from the controller circuit by relays, these devices always represents a risk of mechanical failure and they can possibly damage your equipment or even cause physical injuries, so we (the author or the webmasters) are not responsible of any lose or damage.</p>
<h2>The Circuit</h2>
<p style="text-align: center;"><a title="Schematic" href="http://www.alfersoft.com.ar/files/usocket/pictures/full.png" target="_blank"><img class="aligncenter" style="border: 1px solid black;" src="http://www.alfersoft.com.ar/files/usocket/pictures/full.png" alt="Figure 1 - Schematic" width="450" height="393" /></a></p>
<p style="text-align: center;">Figure 1 &#8211; Schematic</p>
<p>Pretty much the same as USBDAQ but two (identical) sub-circuits were added to control the relays. A Darlington-transistor is used to protect the hardware. Once again, the idea was obtained from the web (thanks to google) and <a title="Relay control" href="http://b-l-w.de/serialrelay_en.php">here is the link to the original article</a>.</p>
<p>The PCB is divided in 3 little boards, for commodity and socket case space issues. (I&#8217;ve used only 2 strip boards in the actual prototype).</p>
<p style="text-align: center;"><a title="Board" href="http://www.alfersoft.com.ar/files/usocket/pictures/full_board.png" target="_blank"><img class="aligncenter" style="border: 1px solid black;" src="http://www.alfersoft.com.ar/files/usocket/pictures/full_board.png" alt="Figure 2 - Board" width="450" height="271" /></a></p>
<p style="text-align: center;">Figure 2 &#8211; Board</p>
<p>List of materials:</p>
<ul>
<li>IC1 &#8211; PIC16F4550 Microchip&#8217;s micro-controller (<a title="PIC18F4550 by Microchip" href="http://www.alfersoft.com.ar/files/usocket/datasheets/pic18f4550-microchip.pdf">datasheet</a>)</li>
<li>Q2 &#8211; Crystal 20Mhz</li>
<li>R1 &#8211; Resistor 4.7K</li>
<li>R2 &#8211; Resistor 1M</li>
<li>R3, R5 &#8211; Resistors 150</li>
<li>R4, R6 &#8211; Resistors 100K</li>
<li>K1, K3 &#8211; 5v Relays. I&#8217;ve used FBR211 by Fujitsu (<a title="FBR211 Relay by Fujitsu" href="http://www.alfersoft.com.ar/files/usocket/datasheets/fbr211-relay-fujitsu.pdf">datasheet</a>)</li>
<li>D1, D2, D3. D4 &#8211; Diodes 1N4004</li>
<li>Q1, Q3 &#8211; BC517 Darlington-transistors (<a title="BC517 Darlington Transistor" href="http://www.alfersoft.com.ar/files/usocket/datasheets/bc517.pdf">datasheet</a>)</li>
<li>LED1, LED2 &#8211; Regular leds</li>
<li>C1, C2 &#8211; Capacitors 22pF</li>
<li>C3 &#8211; Capacitor 470pF</li>
<li>X1 &#8211; Mini-USB Connector Type B (<a title="USB connector type B" href="http://www.alfersoft.com.ar/files/usocket/datasheets/2411-02-lumberg-usb-connector-type-b.pdf">datasheet</a>)</li>
</ul>
<p></p>
<h2>PIC Firmware</h2>
<p>Here is what I needed to modify from the original <a title="SIXCA USBDAQ" href="http://www.sixca.com/eng/articles/usbdaq/" target="_blank">SIXCA USBDAQ</a>:</p>
<p>I set bMaxPower to 250, which means 500mA. The power provided by hosts and hubs is twice the value of bMaxPower field, but in reality they are likely to allocate either 100 or 500 milliamperes rather than the specified amount.</p>
<p>The file modified is the one that contains the USB descriptor: fw/cdc/autofiles/usbdsc.c</p>
<pre class="brush: cpp;">

/* Configuration 1 Descriptor */
CFG01=
{
/* Configuration Descriptor */
sizeof(USB_CFG_DSC),    // Size of this descriptor in bytes
DSC_CFG,                // CONFIGURATION descriptor type
sizeof(cfg01),          // Total length of data for this cfg
2,                      // Number of interfaces in this cfg
1,                      // Index value of this configuration
0,                      // Configuration string index
_DEFAULT,               // Attributes, see usbdefs_std_dsc.h
250,                    // Max power consumption (2X mA) 250 = 500mA

...
</pre>
<p>In the same file I modified the Vendor ID, Product ID and corresponding strings</p>
<pre class="brush: cpp;">

/* Device Descriptor */
rom USB_DEV_DSC device_dsc=
{
sizeof(USB_DEV_DSC),    // Size of this descriptor in bytes
DSC_DEV,                // DEVICE descriptor type
0x0200,                 // USB Spec Release Number in BCD format
CDC_DEVICE,             // Class Code
0x00,                   // Subclass code
0x00,                   // Protocol code
EP0_BUFF_SIZE,          // Max packet size for EP0, see usbcfg.h
0xAF01,                 // Vendor ID
0xAF0A,                 // Product ID: CDC RS-232 Emulation Demo
0x0000,                 // Device release number in BCD format
0x01,                   // Manufacturer string index
0x02,                   // Product string index
0x00,                   // Device serial number string index
0x01                    // Number of possible configurations
};

...

rom struct{byte bLength;byte bDscType;word string[16];}sd001={
sizeof(sd001),DSC_STR,
'a','l','f','e','r','s','o','f','t','.',
'c','o','m','.','a','r'};

rom struct{byte bLength;byte bDscType;word string[21];}sd002={
sizeof(sd002),DSC_STR,
'A','l','f','e','r','S','o','f','t',' ',
'U','S','o','c','k','e','t',' ','1','.','0'};
</pre>
<p>And the file driver/win2k_winxp/mchpcdc.inf  Windows, to match the new vendor, product ID and description</p>
<pre class="brush: cpp;">

[DeviceList]
%DESCRIPTION%=DriverInstall, USB\VID_AF01&amp;PID_AF0A

...

;------------------------------------------------------------------------------
;  String Definitions
;------------------------------------------------------------------------------

[Strings]
MCHP=&quot;alfersoft.com.ar&quot;
MFGNAME=&quot;alfersoft.com.ar&quot;
DESCRIPTION=&quot;Communications Port&quot;
SERVICE=&quot;AlferSoft USocket 1.0&quot;
</pre>
<h2>Programming the PIC</h2>
<p>I&#8217;ve used WinPic800 to program the PIC. Here is a screenshot of the parameters I used to program it.</p>
<p><img style="border: 1px solid black;" src="http://www.alfersoft.com.ar/files/usocket/pictures/usbparams.png" alt="Parameters to program the PIC" width="450" height="304" /></p>
<h2>Commands</h2>
<p>I didn&#8217;t modified any of the USBDAQ commands. They are all available but I only need to use these 4 commands:</p>
<ul>
<li>*A01 (activate relay 1)</li>
<li>*A00 (deactivate relay 1)</li>
<li>*A11 (activate relay 2)</li>
<li>*A10 (deactivate relay 2)</li>
</ul>
<p>All the commands are followed by an enter (chr(13) or &#8216;\n&#8217;).</p>
<h2>Installing (Windows)</h2>
<ol>
<li>Plug the device into the USB port, the following message will appear in the tray bar.</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket01_inst_win.png" alt="Installation Windows step 1" width="440" height="180" /></p>
<li>In the first page of the &#8220;Found New Hardware Wizard&#8221; select &#8220;No, not this time&#8221; and click Next button.</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket02_inst_win.png" alt="Installation Windows step 2" width="450" height="351" /></p>
<li>Select the &#8220;Install from a list or specific location (Advanced)&#8221; option and click Next button.</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket03_inst_win.png" alt="Installation Windows step 3" width="450" height="351" /></p>
<li>Mark the &#8220;Include this location in the search:&#8221; and browse for the directory where the .inf file is located (driver\win2k_winxp)</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket04_inst_win.png" alt="Installation Windows step 4" width="450" height="351" /></p>
<li>This message will appear:</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket05_inst_win.png" alt="Installation Windows step 5" width="450" height="351" /></p>
<li>And then this one:</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket06_inst_win.png" alt="Installation Windows step 6" width="450" height="351" /></p>
<li>When the installation is done press &#8220;Finish&#8221;</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket07_inst_win.png" alt="Installation Windows step 7" width="450" height="351" /></p>
<li>After that the hardware is ready to use.</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket08_inst_win.png" alt="Installation Windows step 8" width="440" height="180" /></p>
</ol>
<h2>Testing (Windows)</h2>
<ol>
<li>Go to the device manager and locate the new COM port added, in my example COM10 was added</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket09_test_win.png" alt="Installation Windows step 9" width="450" height="317" /></p>
<li>Open Hyperterminal and choose a name for the new conection</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket10_test_win.png" alt="Installation Windows step 10" width="332" height="299" /></p>
<li>Select the new COM port</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket11_test_win.png" alt="Installation Windows step 11" width="296" height="300" /></p>
<li>Set the Bit rate to 115200</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket12_test_win.png" alt="Installation Windows step 12" width="344" height="407" /></p>
<li>This step is optional to see what we are typing on the screen, choose ASCII Setup</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket13_test_win.png" alt="Installation Windows step 13" width="344" height="423" /></p>
<li>Then mark &#8220;Echo typed characters locally&#8221;</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket14_test_win.png" alt="Installation Windows step 14" width="263" height="316" /></p>
<li>Finally connect and type one of the commands</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket15_test_win.png" alt="Installation Windows step 15" width="450" height="302" /></p>
</ol>
<h2>Installing (Linux &#8211; Ubuntu)</h2>
<ol>
<li>Plug it. That&#8217;s it! a new device probably called /dev/ttyACM0 will be added by the OS.</li>
</ol>
<p></p>
<h2>Testing (Linux &#8211; Ubuntu)</h2>
<ol>
<li>Open gtkterm if you don&#8217;t have it installed type &#8220;sudo apt-get install gtkterm&#8221; from a terminal</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket01_linux.png" alt="Testing Linux step 1" width="450" height="294" /></p>
<li>Go to Configuration -&gt; Port, set the port to /dev/ttyACM0, the speed to 115200 and click OK</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket02_linux.png" alt="Testing Linux step 2" width="450" height="234" /></p>
<li>Select Configuration -&gt; Local echo</li>
<li>Type the command and press enter</li>
<p style="text-align: center;"><img class="aligncenter" src="http://www.alfersoft.com.ar/files/usocket/pictures/screenshots/usocket03_linux.png" alt="Testing Linux step 3" width="450" height="294" /></p>
<p style="text-align: center;">
</ol>
<h2>Problem</h2>
<p>After I mounted everything in the case a problem appeared, if I plug the USB cable to the computer and then the socket to the wall, the PIC hangs. But it works well the other way round (first plug to the wall and the to the computer). That is probably because the high voltage cables are too close to the PIC and unfortunatelly I&#8217;m not an expert in that matter and I don&#8217;t know how to fix it except by putting the circuit in a separated box. Experts needed! If you know another way to fix it please let me know!</p>
<h2>Pictures</h2>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/proto01.jpg" alt="Prototype" width="450" height="338" /></p>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/proto05.jpg" alt="Prototype" width="450" height="338" /></p>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/board02.jpg" alt="Board" width="450" height="338" /></p>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/board04.jpg" alt="Board" width="450" height="338" /></p>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/case01.jpg" alt="Case" width="450" height="338" /></p>
<p><img src="http://www.alfersoft.com.ar/files/usocket/pictures/case02.jpg" alt="Case" width="450" height="338" /></p>
<h2>Download</h2>
<p><a title="USocket Project" href="http://www.alfersoft.com.ar/files/usocket/usocket.zip">Link to download USocket project</a>, enjoy it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alfersoft.com.ar/blog/2008/07/08/usocket-usb-controlled-socket-with-pic18f4550/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
