Home Chapter 7 BS2 Building the Circuit

Site Search


Chinese (Simplified) French German Italian Portuguese Russian Spanish
BS2 Building the Circuit

Notice the placement of the switch in (first image below) in relation to the LEDs to allow your finger access.

  1. Build the circuit below.

  2. Remember to be very careful about how you attach the wires. You should never have any wires touching accidentally.

    Enter the code below

                {$STAMP BS2} ‘Tells the editor you are working with the BS2

               ' {$PBASIC 2.5}
    1. Top: ‘Label called top

      IF (IN15 = 1) THEN Top ‘Conditional if then statement.

      HIGH 0

      Pause 100

      LOW 0

      GOTO Top:


    Always disconnect power from your Board of Education or BASIC Stamp Homework Board before making any changes to your test circuit. From here onward, the instructions will no longer say “Disconnect power…” between each circuit modification. It is up to you to remember to do this.

 Always reconnect power to your Board of Education or BASIC Stamp Homework Board before downloading a program to the BASIC Stamp.


    LEDs on Board connected to pins 0 and 1


Image above showing 2 leds and switch on board of EDU

 Circuit schematic: Note about reading schematics. There are three places where wires intersect in the bottom drawing. The black dot connected to the resistor, switch is connected, and the dot indicates this fact. The points where the LEDs come into the ground plane are also connected and each is correct and common.

    Introducing a new command

    IF condition THEN address

    IF condition THEN Address is a PBASIC command that allows you to make a decision based on the state of a pin. IF condition THEN address tests the condition, and when that condition is true, it allows the program to go to that point or label in the program.

    • Condition is a statement like “y = 3” that can be tested. The Condition can have multiple conditions in a single statement such as:

    IF (x = 8 and y = 3) THEN beginning          'beginning could be a label in your BS2 program that will send the controller to that place in your program. 

    • Address is a label that specifies where to go if the condition is true in this case to the beginning of the program.

    This is a very powerful command and is at the heart of conditional logic. Because you can ask many conditional statements of the Basic Stamp, you will now have the ability to create a program that will make decisions based on the state of a pin or many pins.

    As well IF…. THEN, will allow you to have sensors or switches become the modifiers of the condition. For example if you have a switch set as in input to a pin on your BASIC Stamp you can now ask with IF….THEN if the switch or sensor is activated and have this send your program to a new place.

    More specifically if you wanted to read the state of pin 15 to determine if the pin was at 0 volt logic low or 5 volts logic high, you could create a loop with a label called Top. You would then ask the question IF (IN15 = 1), a logic high, THEN GOTO Top. This would make the program flash the LEDs high and low until PIN 15 had a logic level of 1.


    Enter the program below


 ' {$STAMP BS2}                               ‘Tells the editor you are working with the BS2
  ' {$PBASIC 2.5}
  INPUT 15                                       ‘turns pin15 into an input
  OUTPUT 0                                      ‘turns pin0 into an output Beginning: ‘start of program loop ‘Named "beginning"
 Beginning:                                       'a label called beginning

LOW 0                                              ‘turns led off
IF IN15 = 0 THEN lightup                   ‘check to see if pin15 is equal to 0, if yes goto lightup
PAUSE 20                                        ‘Pause the program for 20 Milliseconds

GOTO beginning                                ‘goto label called beginning: lightup: ‘starting point for lightup sub-program

lightup:                                               'a label called lightup
HIGH 0                                              ‘turn led on
PAUSE 500                                        ‘keep led on for 1/2 second
LOW 0                                                ‘turn led off

GOTO beginning                                  ‘go back to main program ‘beginning


    Now add the second LED into the mix by looking at the program above and your electronic board design. You notice you have a pin 1 that also has the LED attached and the code to make pin 1 an output? You should specify this in your program.

    The program loop in association with a conditional if then statement is an extremely important bit of code and it offers an excellent lesson on how to approach solving your programming issues for projects you will be developing in the future.

    First when you approach your programming and hardware design always work in small bite size elements. Break the problem up into smaller achievable elements and work methodically.

    That is if you were to invent this program your process would be 1) Use an existing schematic to design the circuit and wire it up. 2) Test that you can activate the circuit, as you would like it to creating a software subroutine that will do just that element of behavior. 3) Now add the switch using existing designs you have found in other hardware/software resources in other basic stamp books or hardware designs. 4) Now test the switch ignoring the previous hardware and software that you have built and scripted. You do not need to remove your original circuit from the board and just leave it there and do not write code to it but test your switch hardware and software subroutine. Now work to integrate the two programs.

    Think about the software methodically, read through, and figure out the possible behaviors and sequences of behavior that can occur. Some artists like to use flow charts and this is a good idea. For a good online resource for flowcharts go to:
    http://freeware4u.com/modules/mydownloads/ see what freeware flowchart software is available.



  1. Use your knowledge to make the second LED flash when you push the mini switch.
  2. Use your knowledge to make pin 0 flash at twice the rate of pin 1.