Basic Stamp tokenizing and flashing from the Linux CLI

For those of you who aren’t familiar with the Basic Stamp, its a small microcontroller available from Parallax. It runs at a whopping 20Mhz and has a full 2K of storage on board for instructions. Although it may not sound like much, its more than enough to program the stamp to do some interesting things. The Basic Stamp is programmed in PBASIC, parallax’s version of BASIC the stamp interprets.

Rather than waste your time explaining the coolness of the Basic Stamp, (if you’ve found this page you’re probably already interested) I want to focus on how it can be utilized under linux. Currently, Parallax has a very nice PBASIC IDE but its available for windows only. It can however be run under Wine, and with the proper font settings it doesn’t look too horrible and is quite functional. But what is the fun in that?

If you’re more of a ‘vim’ guy like me, there’s a better alternative. A quick visit over to sourceforge and you’ll find a set of command line tools available for download that make it easy to tokenize code and send it to the stamp without needing to reboot into windows.

After downloading the tarball, unpack it and move into the directory. All you need to do now is build it, and it shouldn’t require much more than a simple ‘make’.


tdavis@tdavis-64:~$ tar xvzf bstamp-2006.05.31.tar.gz

move into the directory where you unpacked the tarball…

tdavis@tdavis-64:~$ cd bstamp/

and build it…

tdavis@tdavis-64:~/bstamp$ make
tdavis@tdavis-64:~/bstamp$ make install

The last step is to make a symbolic link to whatever serial port your stamp is hooked up to. In my case it was /dev/ttyUSB0 because I’m using a serial to usb converter, but for a regular serial connection its likely to be /dev/ttyS0 or /dev/ttyS1. The symlink needs to point the serial device to a new location, /dev/bstamp. If at any point you encounter any problems, don’t forget to check out the README.txt that comes with the program.

tdavis@tdavis-64:~/bstamp$ sudo ln -s /dev/ttyUSB0 /dev/bstamp

Now you just need some code to tokenize, as an example here’s a simple program I wrote that does nothing more than monitor the light levels off a photo resistor and produce output accordingly. (It beeps and blinks!) Its certainly not the most beautiful code, but it does the trick.

'for Basic Stamp 2
'basic light meter that shows on 7 segment display
'and controls LEDs related to the amount
'of light detected; can also produce
'audio output through a piezo electric
'speaker based on the amount of light detected
'@ Tyler Davis 2007
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "program running!"
index VAR Nib
time VAR Word
dark CON 25
OUTH = %00000000
DIRH = %11111111
'FREQOUT 2, 2000, 4500 'test spk on p2

GOSUB Get_RC 'grab light level info
GOSUB Delay 'delay between refreshes
GOSUB Update_Display
GOSUB sound 'play sound that changes as light
'measurments do

HIGH 0 '0 pin
RCTIME 0, 1, time
DEBUG HOME, "time = ", DEC5 time
IF (time > 200) THEN HIGH 6 'for green and
IF (time < 200) THEN LOW 6 'red lights
IF (time < 200) THEN HIGH 4
IF (time > 200) THEN LOW 4
IF (time < 35) THEN HIGH 5
IF (time > 35) THEN LOW 5
IF (time > 400) THEN HIGH 3
IF (time < 400) THEN LOW 3
PAUSE time

Update_Display: 'to adjust 7 segment display
IF index = 6 THEN index = 0
LOOKUP index, [ %01000000,
%00100000 ], OUT
index = index + 1

sound: 'to create audible sounds related to
'detected illumination levels
FREQOUT 1, 50, time + 4000

Now I’m gonna assume you’re using your own code since my code is kind of worthless without a corresponding schematic, but I guess it could be figured out. Since I don’t feel like drawing up one I’ll just post a picture and if someone wants to try and figure it out they’re welcome to. (Sorry but they’re terrible pictures, I’ll try and get better ones up as soon as I get a chance).

Otherwise the process of tokenizing the code and writing it to the stamp is quite straightforward.

tdavis@tdavis-64:~/bstamp$ ./bstamp_tokenize lightmeter.bs2 lightmeter.tokenized
: Success
PBASIC Tokenizer Library version 1.23


tdavis@tdavis-64:~/bstamp$ cat lightmeter.tokenized | bstamp_run

If you run into any problems, be sure to verify you’re working with the correct serial device. Try a ‘dmesg | grep ttyS’ and see what it brings up. Or replace ‘ttyS’ with ‘ttyU’ if you have a USB connection.

Leave a Reply

Your email address will not be published. Required fields are marked *