Basic Stamp 2 voltage Detector

This is part of my Smart Home series available exclusively on Jafty.com, Jafty Interactive Web Development’s service site. It is part of a series I am writing while building my own smart home out of a home I purchased in Akron, Ohio. The premise is that I have used a micro-controller(Basic Stamp 2) device to link a laptop PC to several electrical objects around my home. I now have it so there are battery powered LED lights in every room of the house and they can be turned on and off using a computer which the Basic Stamp 2 micro-controller is plugged into. The lights and battery also link to the micro-controller. The micro-controller serves as a medium between the PC, battery, lights, etc.

The system works good, but I thought for the home to be truly “smart”, it should also know the voltage of the battery banks used throughout the home to power different objects(batteries are currently charged using a small 900 watt gasoline generator). This article is about how to build a voltage monitoring device that uses a Basic Stamp 2 micro-controller to check the voltage of a battery and report it on the computer screen it is connected to.

How to build a Voltmeter Circuit for Basic Stamp 2

 

Here is a schematic of the voltage meter circuit for the Basic Stamp 2 I drew using Open Office Draw:

Voltmeter-circuit

The above  schematic contains a small modification I made to the resistance. Adjusting the resistance of the 690k ohm resistor adjusts the voltage value. Adding more resistance lessens the value of the voltage read, so you can add or subtract to it as needed to fit your requirements.  Basically what we have is a 470 ohm resistor(r1) coming off of one of the io pins on the Basic Stamp 2(BS2-io-pin). The other end of the resistor(r1) attaches to the positive leg of a 0.02 uf film capacitor(c1). Then attached to the same positive leg is a 600k ohm resistor(r2) and this is the positive lead used for checking voltage on the positive side of the battery to be tested. Coming off of the other, negative lead of the capacitor(c1), is a wire going to the vss ground on the Basic Stamp 2 micro-controller. Finally another wire lead comes off of the Basic Stamp 2 vss ground to go to the negative terminal of the battery or device to be tested for voltage. Here are the values of the required components summarized:

  • r1 = 470 ohm
  • r2 = 690k ohm
  • c1 = 0.02 uf micro film capacitor(the green ones)

As you can see, the circuit for testing voltage is very simple and we all know that testing voltage is not quite considered simple. There can be analog to digital conversion integrated circuits used and many other more complex components, but this is a very simple circuit made possible due to the use of the Basic Stamp 2 micro-controller and it’s rc time ability. Therefore, the program we need to run is not quite as simple as the circuit, but is still very easy and this is by far the easiest way I could find to accurately check DC voltage and report the results on my PC. Next, we’ll cover how to write a program in pbasic to put onto the Basic Stamp 2 in order to check voltage with the circuit outlined above.

PBasic Code to Check Voltage Using Basic Stamp 2

‘ {$STAMP BS2}
‘ {$PBASIC 2.5}

rct VAR Word  ‘RCtime value
Vx VAR Word  ‘calculated voltage (this will = applied voltage Vx)
Cn1 CON 48576  ‘first constant, see below
Cn2 CON 8  ‘second constant used below
‘ circuit attached to Pin 0
LOW 0
TheLOOP:
RCTIME 0,0,rct
LOW 0
Vx=Cn1 / rct + Cn2
DEBUG HOME,DEC? rct,”Vx= “, DEC Vx/10,”.”,DEC1 Vx
PAUSE 500
GOTO TheLOOP

As you can see, the above code may look a little complicated, but it is as short and sweet as you will find when it comes to code to check voltage using a micro-controller. I promise you, this is the simplest way to check DC voltage using a Basic Stamp 2 micro-controller. Also we need to report that voltage to a computer running Windows 7 or 8. This turned out to be a challenge that took me several hours to overcome, but not to worry, I’ll share my ultimate solution with you next.

How to send Data from Basic Stamp 2 to a Windows 7 or 8 PC and Present to Screen

The basic challenge for me was: How do I get the voltage value from the Basic Stamp 2 to a web page running on my Windows 7 PC using XAMPP for Windows? The main issue seemed to be that Windows computers running either windows 7 or above don’t allow a scripting language such as PHP to read data from serial ports. I found that I was very easily able to write data to the Basic Stamp 2 micro-controller from Windows 7 using PHP and the fopen/fput commands and similar methods. However, while I was able to open a connection using PHP’s fopen command, I was not able to read data from the serial port using PHP at all. I tried almost every possible method I could find and nothing worked. That was very aggravating to say the least! I ended up finding a method that worked if you first wrote something to the serial port and then read it after a 1 second pause. This is the only way that would allow me to read data from a serial port using Windows 7 and PHP. Below is the function I used. It writes a “v” to the stamp micro-controller first which is used to ask for a voltage reading. Then it reads and parses the response from the serial port.

<?php

//this file is included in ControlLights.php to show voltage
function getSerialData($port){
‘mode $port: BAUD=9600 PARITY=N data=8 stop=1 xon=off’;
$fp = fopen ($port.”:”, “r+”);
if (!$fp) {
echo “ERROR! COM3 Port failed to open.“;
} else {
//send command to get voltage from stamp(cannot read data from serial without using fputs first for some reason!)
fputs ($fp, “v”);
sleep(1);
//get serial data:
$response = fread($fp,19);
//echo “response: $response”;//NOTE: this produced text before voltage, so I parsed it out below:
$resparr = explode(“volts:”,$response);
$voltage = $resparr[1];
echo “$voltage Volts“;
fclose ($fp);
}
}//end function.
getSerialData(“COM3”);
?>

I included the above PHP file within the web page that I wanted to show the voltage on and it worked perfect! To view the whole thing, see my tutorial on controlling lights which includes the above script in the ControlLights.php file to show the battery voltage on screen in the app I use to turn lights and appliances in my home on and off.

 

Leave a Reply