BASIC Stamp Sensors Overview

If you are trying to program the BASIC stamp, see Programming the BASIC Stamp for more detail.

A BASIC Stamp Tutorial focusing on using sensors, originally written by Shivang Patel.

Introduction
Start up the basic stamp editor and make sure the Board of Education BASIC Stamp is connected to the computer through the serial port or via the USB cable. uk-essays.org

You can use the debug window to print out information about your program, such as variables and senor values, as it's running. The format for the debug command is

DEBUG string[,CR]

Enter the following into the Basic Stamp Editor

' {$STAMP BS2} DEBUG "Hello World!",CR

The CR at the end tells the basic stamp to add a return to the end. Download and run the program on the stamp by clicking the Run button on the toolbar.

Turning on a LED
Another good way to debug your program while it's running is to use a LED as an indicator of the start or end of an event. Connect the LED in series with a 220Ω resistor to pin 1 on the BASIC Stamp Board of Education and then to ground (VSS). To turn the LED on and off use the HIGH and LOW command respectably. In the Basic Stamp editor enter the following code:

' {$STAMP BS2} HIGH 0 ' Pass a high voltage to pin 0 (Turns LED on) SLEEP 2 ' Pause the program for two seconds LOW 0 ' Pass no voltage to pin 0 (Turns LED off)

The HIGH and LOW commands take a pin number as its argument, in this case we want to use pin 1. The SLEEP command will cause the Basic Stamp to wait a certain number of seconds before continuing; in this case it will wait 2 seconds.

Light Sensor (Cadmium Sulfide Photocells)
A variety of sensors exists that can be used with the Basic Stamp. Cadmium sulfide photocells act variable resistor with the resistance proportional to the amount of visible light. The Basic Stamp can measure voltages, to measure resistance a RC circuit must be used. A basic RC circuit schematic is shown below. The Basic Stamp can calculate the resistance by measure the amount of time it takes for the capacitor to charge.

Connect the circuit to pin 2 and enter the following into the Basic Stamp editor.

' {$STAMP BS2} result VAR Word HIGH 2 ' charge the cap PAUSE 1 ' for 1 ms   RCTIME 2, 1, result ' measure RC discharge time DEBUG DEC ? result ' display resultHIGH 2 ' charge the cap PAUSE 1 ' for 1 ms   RCTIME 2, 1, result ' measure RC discharge time DEBUG DEC ? result ' display result

In the above code result is a variable, variables are declared using the following format:

[Variable name] VAR [Data type]

In this case the type is a word. Also you will notice that next to each command there are comments, anything after the (') is a comment and is ignored by the stamp.

Light Sensor (Photodiode)
The circuit below shows how to connect a photodiode to the BASIC Stamp. The circuit will output either high or low depending on the amount of light\darkness. Varying the resistor value will adjust the sensitivity.

Connect the photodiode to pin 3 and enter the following into the Basic Stamp editor.

' {$STAMP BS2} DEBUG DEC ? IN3 ' display result

In the above code IN3 gives either a 1 or 0 for a high/low signal connected to pin 3. IN# can be used to get a high/low signal from any pin and can be used just like a any other variable.

Distance sensors (IR Range Finders)
IR Range finders are commonly used to measure the distance an object is in front of the sensor, Ranger finders are also a good way to map the surrounding area. To use a range finder with the Basic Stamp use the following code.

' {$STAMP BS2} val02 VAR Byte 		' value where reading is stored i VAR Byte    		' counter variable for loop cl CON 9    	 	' pin 14 is output (clock) dt CON 10    		' pin 15 is the input (data) INPUT dt 			' make pin 15 the input HIGH cl 			' make pin 14 output and high DEBUG "Start", CR 	' indicate beginning FOR i = 1 TO 100 		' take and display 100 readings GOSUB read02 		' call measurement routine DEBUG DEC val02, CR 	' display the value PAUSE 100 		' stall so display readable NEXT DEBUG "Finish" ' indicate end END ' stop Stamp when done '   ' subroutine read02 '   ' This subroutine takes a reading from the connected ' GP2D02 detector and stores the value in "val02". ' Any two pins can be used. Set "dt" to the data line ' of the GP2D02 (pin 4 on JST connector, yellow wire). ' Set cl to the clock line of the GP2D02 (pin 2 on the   ' JST connector, green wire). read02: LOW cl ' turn on detector for reading rl: IF IN15 = 0 THEN rl ' wait for input high SHIFTIN dt, cl, MSBPOST, [val02] HIGH cl ' turn detector off PAUSE 1 ' let detector reset RETURN

Do/While Loops
Do loops are used to execute a section of code until a certain condition is met. Using the code from the previous sections we will turn a LED on when a photodiode sense a reduction in light. Make sure a LED (with a resistor) is connected to pin 1 and a light sensor circuit is connected to pin 2 and enter the following code into the Basic Stamp editor.

' {$STAMP BS2} ' {$PBASIC 2.5} LOW 1 DO WHILE (IN3 = 1) DEBUG DEC ? IN3 ' display result LOOP HIGH 1 DEBUG "I see light", CR

For Loops
To make a LED blink we will use the code to turn a LED on and off from the previous section in conjuncture with a for loop. A for loop is used to execute a group of code a certain number of times. Make sure a LED (with a resistor) is connected to pin 1 and enter the following code into the Basic Stamp editor.

' {$STAMP BS2} i var word FOR i = 1 to 5 HIGH 0 SLEEP 1 LOW 0 SLEEP 1 NEXT

The above code will turn the LED on, wait for 1 seconds, turn the LED off, and finally wait another 1 seconds. These four tasks are then executed five times, to make the effect of a blinking LED. The for loop keeps track of a counter which indicates the current iteration it is on, in this case the counter is the variable I.

If Statements
If statements are used to execute a section of code based on whether a condition is true. The format of an if state is as follows:

IF (condition) THEN LABEL

LABEL is where to jump to when the condition is true. We will use a if statement to turn on a LED when the light sensor sees no/little light. Make sure a LED (with a resistor) is connected to pin 1 and a light sensor circuit is connected to pin 2, enter the following code into the Basic Stamp editor.

' {$STAMP BS2} ' {$PBASIC 2.5} result VAR Word

LOW 1 DO       DEBUG DEC ? IN3 ' display result IF (IN3 = 0) THEN LIGHT LOW 1 GOTO DONE LIGHT: HIGH 1 DONE: LOOP

Here LIGHT is a label that the if statement will jump to when the light sensor has a value greater then 500. When the light sensor is high(1) the stamp will not do anything and continue to the next command; in this case LOW 1 and GOTO DONE. The GOTO DONE statement is used to jump down to the DONE label and skip over the HIGH 1 command.

Servo Motors
Servo motors are geared motors that can be precisely controlled to turn to a certain angle. The angle of a servo is controlled by sending timed pulse signals to the servo. Connect a servo to pin 15 servo header port on the Board of Education©, it is very important that the board is now powered while connecting the servo and that it is correctly plugged in. Once the servo is correctly connected, plug the power back in and enter the following code into the basic stamp editor.

' {$STAMP BS2} ' {$PBASIC 2.5} DO       PULSOUT 15, 300 PAUSE 10 LOOP

Servo’s are commonly “hacked” or modified for continuous rotation. This provides a cheap geared motor whose speed can be precisely controlled. Connect the continuous rotation servo to the Board of Education© the same way as above. You can adjust the speed of the servo by changing the pulse that is sent. At a certain value the servo will stop moving. At this point increasing the servo value will gradually make the servo turn in one direction. Decreasing this value will gradually increase the speed of the servo in the opposite direction. The code for a continuous rotation servo is below.

' {$STAMP BS2} i var word FOR i = 1 TO 100 PULSOUT 15, 1000 PAUSE 10 NEXT

Helpful Links

 * The Official Acroname Website
 * Learn more about about the BASIC Stamp by reading the BASIC Stamp Manual
 * The BASIC Stamp IDE download link
 * Parallax has published large BASIC Stamp tutorial worth looking into