M a r k   C r o s b i e

You are in: STAMP Buzzer  Home  Resume  Linux  LEGO  Electronics  Movies  Mac OS X 

Overview Simple buzzer LCD control Buzzer and LCD PWM motor control Stepper motor control
Contact Mark at mark@mastincrosbie.com

STAMP Buzzer

This simple circuit will buzz a speaker and generate varying tones. It illustrates the use of the RCTIME and FREQOUT commands, and also shows how much can be achieved using a little PBASIC code.


  • Simple example of using the RCTIME command
  • Shows how to make tones using FREQOUT
  • As simple as it gets :-)

PBASIC Code listing

The following code will work on a Basic Stamp 2. The RC network is placed across pin 8, and the speaker is connected to pin 7. The results of charging the RC network is multiplied by 3 to give a frequency value that results in an audible tone. I played around with this a bit and you can try different values here.
This program is pretty boring, until you connect a potentiometer across the charging pin. Then you can twiddle the pot, and here the tone from the speaker change in pitch.
Click here to download the PBASIC source code.
' buzzer.bs2
' Simple program to buzz a speaker based on the charge across
' a pot-capacitor pair.
' Basic Stamp 2 code only.
' Copyright Mark Crosbie (mark@mastincrosbie.com), 1998.
' A variable to hold the result of the timing
result	var	word

' Symbolic constants for pins used in the circuit
RC	con	8		' pin to drive RC network
SPEAKER con	7		' pin to drive speaker

	dir7 = 1		' set pin 7 to output
	dir8 = 1		' set pin 8 to output
	high RC			' charge the capacitor
	pause 10		' wait for it to charge
	rctime RC,1,result	' do the RC timing
	freqout SPEAKER,10,result * 3 ' output a frequency on pin 7
	goto loop
In the following walk through the code, I'll put commands in boldface and variable names in italics.

Declaring variables

Let's take this apart line by line. First, we declare a variable named result, which is of type word. This variable is going to hold the result of timing the RC network. By declaring it a word, the variable can hold a 16 bit value: 0 through 65535. If you try different values for the capacitor and resistors, you'll notice the value of result changing dramtically in size. To accomodate all the range of values that result can take on, I declare it as a word.
To make the program easier to read, I declare two constant values: RC and SPEAKER. These values correspond to the pins that the RC network and the speaker are connected to, respectively. Why do this? Well I'm always rebuilding my circuits and moving them around on a breadboard, and often I'll connect components to new pins when I move things. Instead of having to search through the entire program for every occurance of a command that uses that pin, I simply change the symbolic constant declared at the top of the code that corresponds to that pin. Much easier, and less error prone! It may seem like overkill now, but wait until we get to more complex pieces of code, and it will make life much easier!

Setting pin output

Next, we declare that pin 7 is an output by using the dir7 command. By setting this to the value 1 we simply tell the Stamp to only allow output on pin 7. Pin 7 is used to drive the speaker. We also declare pin 8 to be an output, as it is used to drive the charging of the RC network.

Charging the capacitor - RCTIME

The RCTIME instruction is used to measure the discharge time of the resistor-capacitor pair. The command takes three arguments:
  1. A pin to which the resistor-capacitor pair are connected.
  2. A state variable that indicates the desired final state of the pin. When the pin reaches this state, RCTIME will return.
  3. A result variable to hold the value returned by RCTIME in the range 0 to 65535.
RCTIME works as follows: a counter is started which increments every 2 microseconds (that's 500,000 ticks a second!) For as long as the input pin is not in the state specified in state variable, the counter is incremented. Once the pin has reached that state, the value of the counter is stored in result variable. While the capacitor charges through the output pin from the Stamp, the counter in RCTIME is incrementing. Once the capacitor has charged, the RCTIME command returns, with the value of the counter in the variable result.
The length of time it takes a capacitor-resistor pair to charge or discharge is proportional to R x C - the resistance times the capacitance. The larger either of these values are, the larger the charge time. For any given values of R and C, the RCTIME command will return a value. If we increase the value of R, the value returned by RCTIME will increase. If we decrease the value of C, the valuse returned by RCTIME will decrease. Thus, by altering the resistance R (using the potentiometer in this circuit), we can change the value returned by the RCTIME command. Nifty, eh!
Now we must discharge the capacitor through the resistor-potentiometer pair. We set the output on pin 8 high. This will charge both of the plates of the capacitor to 5 volts - effectively there is no charge on the capacitor, as both of its plates are at the same potential.
We then pause for 10 milliseconds to allow the capacitor to discharge.

Generating a tone - FREQOUT

Once we have obtained the charge time, we can use the FREQOUT command to generate a sinewave output on the speaker pin. This will cause the speaker to generate an audible tone. The first argument to FREQOUT is the pin to drive, in this case the symbolic constant SPEAKER, which is our speaker pin. The second parameter is the duration of the output, which is simply 10 milliseconds. Finally, the value in result is used to specify the frequency of the tone in hertz.
This body of code is wrapped in an infinite loop that will continually generate a tone on the speaker, the frequency of which is controlled by the setting on the potentiometer.

Wiring Guide

It's important to place the 22 ohm resistor in series with the 8 ohm speaker, otherwise the current through the speaker may dammage the Stamp output. If you have a speaker with a higher ohm rating, you can leave the 22 ohm resistor out of the circuit.

Components Listing

All resistors are 1/4 Watt. Play around with different values for the capacitor and resistor to see what effect it has on the output frequency.
  • 1x Basic Stamp II
  • 1x 22 ohm resistor
  • 1x 220 ohm resistor
  • 1x 10k pot
  • 1x 0.1uF capacitor
  • 1x speaker, 8 ohm impedance

PIC Microcontroller Project Book
Lot's of great PIC project ideas!

Programming and Customizing the Pic Microcontroller

If you are learning to program microcontrollers then Myke's book is good start.
The Art of Electronics
A classic in the field. Teaches you the art and science of linear and digital electronic design. If you want to learn why your circuit is not working, read this book and you'll know why.
Mobile Robots: Inspiration to Implementation
A very readable introduction to the art of robotic design and implementation from the best practioners in the field: the MIT Artificial Intelligence Lab!
Microcontroller Cookbook
A set of cookbook style designs for the 8051 and PIC microcontrollers. A handy reference to have if you need a quick solution to a problem.

© 2002-2004 Mark Crosbie   shareright © 2002 Phlash