Building a Basic Stamp I & PC Relay Controller

This project shows you how to build a relay controller using the Basic Stamp I interfaced to the PC serial port.  The Visual Basic 5 software developed for the interface lets you interact with the Basic Stamp to turn ON/OFF up to (2) relays attached to the Basic Stamp I/O pins.  As shown below in the screen capture of PC-Relay, it's easy to select the desired com port using the drop-down menu.

The default communications settings are comm1 "2400,N,8,1".  When you start the PC-Relay software the default com port settings will be in affect unless you change them via the drop-down menu.  If you're using com1 at 2400,N,8,1 you can start using the program without selecting the port first.   If you prefer to use another com port or faster baud rates, you will need to make your selection using the drop-down menu each time you start the PC-Relay software. Here's a screen capture of the interface developed for this project.

Note:  Notice that you can select between 2400 and 9600 baud.  The maximum baud rate using the BS1 is 2400, but 9600 was added to the PC-Relay software to let people designing with the BS2 use the PC-Relay software also.  If you're using the BS2 you will need to make modifications to the Serin statement in the basic code shown below.

Controlling relays with the Basic Stamp and your PC
Click picture for a larger view
PC-Relay schematic for the LED test circuit
Click HERE for the PC-Relay Software

The screen shot above is actually the main control screen you will use  for this project to control (2) relays attached to the Basic Stamp circuit.   After you download and install the Visual Basic 5 software, build the circuit shown above.  Click here on the blue filename pc-relay.bas and download the basic file for the Basic Stamp I.  Use the circuit above to verify that everything is working correctly, then proceed to the actual relay circuit later on in this article.

Getting Started:

Once you have installed the PC-Relay software you're ready to build the circuit shown above.  This is a handy & quick circuit that will let you test everything before you build the bigger (relay) circuit.  If everything works, i.e.. you can control the LED's, you're ready to build the relay circuit.  Use the drop-down menu for the complete schematic for the Basic Stamp PC-Relay circuit.  I will include a more detailed explanation of the relay circuit later on in this project article.

More Schematics:

The drop-down menu (Schematics), will let you select two more schematic screens similar to the main control screen shown above.  The first schematic shows how the Basic Stamp I interfaces to the PC for programming the Basic Stamp using the Parallax Basic Stamp programming software.  The second schematic shows how to build the (relay) circuit for this project.

Applications:

The ability to control your world from your PC has more advantages than I have room to write about on this web page.  For the moment, we'll just say it's fun.  If you browse through a few electronics catalogs, you will no doubt see quite a few large companies that offer similar devices for sale... Most with a hefty price tag.  The Basic Stamp gives us the ability to design some pretty sophisticated control applications with a minimum out of pocket cost.

Anything you can control with a relay, you can control with this project.  From the bedroom lights to the TV or stereo, you're only limited by your imagination with this project.  The price & availability of solid state relays makes it possible to control large current loads with (very small) control current requirements.  You can find solid state relays that can easily handle up to 40 Amps.  For the purpose of this project, we will stick to smaller 5 volt relays that can handle up to 1 Amp.

The Basic Stamp I Code:

The Basic Stamp I code for the PC-Relay project was kept pretty simple.  Below is the complete listing for Basic Stamp I code.

symbol relay = b3                     ' Define storage space & name for relay input
symbol stat = b4                      ' Relay status for ON/OFF

dirs = %11111110                      ' All pins outputs except 0 for serial input

read 1,stat                           ' Read EEPROM at address 1 and store in STAT or b4
if stat = 1 then on1                  ' If relay #1 was turned on before power down, turn it back on
goto off1                             ' Otherwise turn it off

begin:                                '
   read 2,stat                        ' Read EEPROM at address 2 and store in STAT or b4
   if stat = 1 then on2               ' If relay #2 was turned on before power down, turn it back on
   goto off2                          ' Otherwise turn it off

loop:
   serin 0,N2400,(254),relay,stat     ' Get serial data on pin 0 into b3 (relay) and b4 (stat)
   if relay = 1 then out1             ' If we have asked to control relay #1 then goto its routine
   goto out2                          ' Otherwise goto the relay #2 routine
   goto loop                          ' Go get some more input from the serial port

out1:
   write 1,stat                       ' Record the requested state for relay #1 on/off in EEPROM
   if stat = 1 then on1               ' If we asked to turn it on, then go to the turn-on routine
   goto off1                          ' Otherwise go to the turn-off routine
   goto loop

out2:
   write 2,stat                       ' Record the requested state for relay #2 on/off in EEPROM
   if stat = 1 then on2               ' If we asked to turn it on, then go to the turn-on routine
   goto off2                          ' Otherwise go to the turn-off routine
   goto loop

on1:                                  ' Each of these routines will simply make our I/O pins high or low
   high 1                             ' Since we are using NPN transistors, a logic (1) high, will
   goto begin                         ' turn-on the relays. A logic (0) low, will turn them off

off1:
   low 1
   goto begin

on2:
   high 2
   goto loop

off2:
   low 2
   goto loop

Program Flow:

The code for PC-Relay is pretty simple to follow.  The routine Loop waits for the number 254 to arrive before accepting any more serial data.  This technique is handy because it helps to avoid noise or spikes from causing our program to act erratically and turn our relays ON or OFF unexpectedly.  It also provides us with a way to add more than one PC-Relay circuit to our serial data bus.  You would need to modify the Visual Basic code to send different addresses to each individual PC-Relay circuit however.  The routines out1 and out2 will record the value held in (stat) into the onboard EEPROM locations assigned to each relay.  Location #1 for relay #1 and location #2 for relay #2.  The beginning of our program will then read each of these values after a power failure and restore each relay to its prior ON or OFF state.

Notice that routines on1 and off1 will return to routine begin.  This is because when power fails we need to restore our relays to the last state they were in before the power failure. After we restore relay #1, the routine begin will do the same for relay #2.  The routines on2 and off2 will then return to Loop to wait for more commands from the PC.  There are (many) different ways to achieve the same results, but this way is effective and simple enough to ensure our relays always return to their original state on power-up, reset or a power failure.

The Relay Schematic:

Once you have verified that the PC-Relay Visual Basic software and your LED circuit is working correctly, you're ready to build the relay circuit.  Take note of the reverse bias diodes across the coils of each relay.  These diodes (often referred to as Fly-Back diodes) will help protect your switching transistors from the nasty voltage spikes caused by the relays inductor (coil) when the relay is suddenly switched off.  (don't forget them).   A standard 1N4001 type diode is sufficient to protect the transistors.  For the transistors I used two 2N2222A type.  You can substitute the 2N2222A for another NPN if you don't have the 2N2222A handy.

Note:  Notice that +5 vdc is applied to the stamp VDD pin and not VIN.  This is because you don't want to use the stamps on-board +5 volt regulator.  The BSI has a very small current rating on its regulator and trying to use the on-board regulator will damage your stamp.  The maximum current rating for the on-board regulator is normally about 50mA and is not sufficient for driving several relays, therefore it's necessary to use your own 5 volt regulator and supply +5 volts from your regulator to the VDD pin# 5 of the BS1.   Using a 7805 type with a maximum current rating of 1 Amp should be more than adequate for this project.  Take care however when you select your relays and make sure the total combined current requirements for (all) of the relays being energized at the same time will not go beyond the current handling capabilities of your selected voltage regulator.

Controlling relays with the Basic Stamp and your PC
Click picture for a larger view
PC-Relay schematic for the relay circuit
Click HERE for the PC-Relay Software

Simple & Effective:

The main control screen of PC-Relay will look like the first screen capture shown in the beginning of this project article.  The schematic for the relay circuit can be selected from the (Schematics) drop-down menu.   This project is very simple to build and yet it is quite effective at what it does.   By using this project as a starting point you can design your own relay controller with more capacity.  Building one to control (7) relays with the Basic Stamp I is simple using this project as a blueprint.

The Visual Basic Code:

If you want to try your hand at designing your own PC-Relay interface, HERE are the raw Visual Basic source code files for this project.  No special controls or OCX files were used for this one so it's pretty easy to re-configure to your own needs.   You can also get rid of the (Splash Screen) that comes up each time you start PC-Relay. 

Operation:

Clicking the ON/OFF buttons beside each LED on the main screen schematic will send ON or OFF commands to each relay.  In the event of a power failure or a reset, your relays will return to the last state that you selected.  Each separate schematic has a button in the lower right corner marked CLOSE.  This will close that particular schematics' screen, and return you to the main control screen with the LED's.  You can refer to the different schematics at any time during operation.

Further Study Recommendation:

If you're new to serial communications between the PC and the outside world, you can get a really (solid) introduction to interfacing the PC to your microcontroller or other projects from Jan Axelsons' excellent book titled Serial Port Complete.  I most highly recommend Jans' book if you plan to do any serious applications development, or you would just like to gain a better understanding of what goes on under the hood.  I have just about every book ever published on serial communications and I have yet to find a book that even comes close to Jans' on this topic.  Applications "just like this one" are simple with the fantastic information Jan provides in her book.

Installing/Uninstalling PC-Relay:

To install the complete version of PC-Relay on your PC, just download the file named pc-relay.zip to a temporary directory on your hard drive.  Unzip it in the same temporary directory and click on the file Setup.exe.  PC-Relay will install itself into the programs directory unless you select a different location.  Once you have run the installation/setup program, PC-Relay should now reside on your menu bar with the rest of your programs under windows 95/98.  The actual executable file is named relay2.exe, but will show on your menu as pc-relay.  To uninstall PC-Relay, just click on the desktop icon for My Computer, next click on the icon for Control Panel, next click on Add/Remove Programs.  Scroll down until you see pc-relay.  Click on pc-relay to highlight it and then click on the Add/Remove button.  Follow the on-screen instructions to completely remove PC-Relay from your PC...  That's it, you're done...

Note:  Here's the Basic Stamp I code, pc-relay.bas .   Don't miss our project for controlling up to (8) hobby servo motors with your PC...  Click HERE to see the project.