Home Chapter 9 BS2 Programming the project

Site Search


Chinese (Simplified) French German Italian Portuguese Russian Spanish
BS2 Programming the project

As you learned in Chapter 7, VAR is the way you define a variable. A FOR…NEXT LOOP can program and sequence the ON-OFF behavior of the LED. In this lesson, you will use similar code to program your AC bulb to turn on and off for a specific duration of time at particular speeds.

As a review, remember that variables in bold should be in mixed-case and start with an uppercase letter such as Bit, Nib, Byte, or Word. The program should automatically change these words to uppercase if they follow VAR.

Variable names should use lower-case and uppercase mixed letters and should be descriptive, such as: Lamp, Bright, Infrared, and Motor.

Lamp VAR Bit ‘a bit permits either 0 or 1
Bright VAR Nib ‘a nib permits variables between 0--15
Infrared VAR Byte ‘a byte permits variable between 0--255
Motor VAR Word ‘ a word permits variables between 0--65535

The syntax for a FOR…NEXT LOOP is:
FOR Variable = StartValue TO EndValue
Introductory Example Program: FlashbulbSSR

1) Enter the code below into your Parallax editor. This code will allow you to turn the bulb off and on in 100 and 300 millisecond flashes.

'Solid State Relay 10 times off and on.bs2

'Turn a solid state relay on and off 10 times

' {$STAMP BS2} ‘a directive to the Stamp that you are using the Basic Stamp 2
' {$PBASIC 2.5}

Bulb VAR Byte ‘create a variable called bulb

FOR bulb = 1 to 10 ‘for the variable called bulb count between 1 and 10

High 4 'Turn pin 4 high

pause 100 'stop program for 100 milliseconds

Low 4 'turn pin 4 low

Pause 300 'stop program for 300 milliseconds

Next 'goto the next number in the count

End 'stop the program after 10 loops

2) Make sure the bulb is screwed tightly into the socket and carefully plug the AC lamp into an AC source or plug.
3) Turn on the Board of Education and download the code by pressing run. Your light should be flashing!

Congratulations! You are now controlling an AC device with 5 volts DC from the Stamp. If it is not functioning, then you must check these things:

  1. Is your primary power for the AC bulb plugged in?
  2. Is the control wire for the pins coming from the stamp into the SSR making good contact with the output pins on pin 1 and ground? Push it down or turn it slightly if not.
  3. Is the programming cable plugged in all the way?
  4. Check the debug terminal to see if Com 1 port is selected.
  5. Is your bulb burnt out? Test it in another AC socket to make sure.


Passive Infrared Sensors (PIR)

When you pass a house at night the porch light suddenly switches on have you wondered what that is? Well most likely, it is not the owner of the house but in fact a passive infrared sensor. These are ubiquitous sensors that are everywhere in our environments. The sensors that activate lights on our houses are AC versions of the passive infrared sensors which you will be experimenting with here.

The K76 PIR (also called PIR module KC7783 or KC778B IC) is a pyro electric sensor that runs on DC current and this sensor has been specially developed to sense body heat. The k76 PIR detector is combined with a Fresnel type lens that allows the sensor to image your infrared body heat and look for a change in this quantity of infrared.

Parallax now sells these sensors PIR sensors as well at this link. Notice the Parallax sensor does not have long wires, though does have pins that you can plug into your breadboard directly.


Passive Infrared Sensor with Fresnel lens cap over sensor


The device you will use in this experiment is a special 5-volt DC PIR that you can interface to the Stamp directly. This is a great part because it has its own on-board circuitry that allows it to monitor whether there is someone or something with an infrared heat signal nearby. The sensor will only be activated when the sensors sees infrared movement, which allows your Basic Stamp 2 to process other things while the sensor looks for this change in infrared. The PIR senses at distance of 1-9 feet.


PIR bottom. Keep this away from metal when electrified.


You should not allow your Basic Stamp to be encumbered by too many sensors tying up processing power and use smart sensors as much as possible.

The PIR is a one-bit sensor, in that it can returns a value of either on (5 volt) or off (o volts). Either it senses someones movement or it does not.

You will be able to monitor this sensor with your Basic Stamp as if it were a normal switch, as demonstrated in Chapter 7.

This will allow the Basic Stamp to make decisions to activate different things based on the interaction of the viewer/participant. Remember, this is not a good sensor for a mobile platform because it images a quantity of infrared and then looks for a change in that quantity.

These PIR sensors also take about 25 seconds to warm up before they function, so be patient in your testing. They will remain activated for about one second after they see an infrared source of heat. You should also make sure this sensor is not pointing toward or sitting in the sun.