Piezospeaker Volume Control
Here is a·
activity you can use to control the volume of your Piezospeaker with a potentiometer.·
The volume ranges from barely audible to annoyingly loud.·
You can find most of these parts in the What's a Microcontroller kit.·
The LM358 is part of the Basic Analog and Digital kit, but you can also purchase it online through the Parallax Component Shop.·
The 9 V battery clip is an item you can probably get at your local Radio Shack or other electronics outlet.
Part Number···· Quantity··· Description
10 k potentiometer
10 k resistors
or (1) 5 k
9 V batteries
9 V battery clip with tinned leads
Amplifier Circuit Between P9 and Piezospeaker
The circuit you are about to build is called an inverting amplifier circuit.·
It has an input that receives the audio signal from the BASIC Stamp(R)
2 microcontroller's P9 I/O pin.··
It's output is connected to the piezospeaker, and you can control the volume with a potentiometer.··
The LM358 in the amplifier circuit·
will need a dual power supply, which·
you will use two 9 V batteries and·
one 9 V battery clip with tinned leads to build.
Dual Supply Schematic
To get good amplification with one opamp circuit, the opamp chip can needs two supplies, a negative voltage and a positive voltage.·
By plugging a 9 V battery into the Board of Education's battery clip, this will give you + 9 V at the Vin sockets above the breadboard.·
If you measure this with a voltmeter, connect the positive lead to Vin and the negative lead to Vss.·
To make the -9 V supply, simply connect the positive lead of your second 9 V battery to Vss.·
The negative lead will then supply -9 V.·
Most 9 V battery clips with tinned leads have a red lead connected to the battery's positive terminal, and a black lead connected to the battery's negative terminal.·
Verify this with a voltmeter.·
If the red lead is indeed the connected to the 9 V battery's positive terminal, then connect the it to Vss, and set the black lead aside.·
You will connect the black lead to the opamp's negative power supply input when you build that circuit.
LM358 Pin Map
This is a pin map of the LM358 opamp chip.·
It has two opamp circuits in it along with a positive and negative power inputs.·
Remember to use the semicircle reference notch to get the chip right-side-up on the breadboard.·
If you plug it in upside-down but wire it as though it's right-side-up, it could damage the chip and maybe your BASIC Stamp too.
This is the circuit you will need to build.·
It's really easy to make a wiring mistake on this kind of circuit; I corrected several before I could get my own circuit to work.·
Fortunately for me, I didn't damage anything.·
Before you actually plug in the batteries, triple check the circuit.
Note that the opamp triangle in the schematic has pin number references that match the ones on the pin map.·
Build this circuit.·
You will be using the dual power supply you built earlier, make sure to connect Vin to pin 8 on the LM358, and connect the -9 V (battery clip's black lead) to pin 4 on the LM358.·
Vss should be connected to pin 3 on the opamp.·
Here is a photo of the completed circuit that you can use as a wiring diagram for trouble-shooting.·
Testing the Circuit
When I plug in my batteries, I usually monitor the opamp's temperature for fifteen or so seconds by resting my fingertip on the top of the chip.·
If it gets hot in this amount of time, I disconnect power immediately and start looking for wiring errors.
If your LM358 stays cool, run a program to make your BASIC Stamp generate a ten second tone:
I like playing a tone for about ten seconds.·
This gives me enough time to turn the pot and verify that it controls volume, but it doesn't go on long enough to disturb·
If I want to run the program a second time, I just press and release the Reset button on my board.·
Run this program and verify·
that you can control the tone volume with·
The full range of potentiometer motion should change the volume from quiet to loud, and visa-versa.
DEBUG "Mi...", CR: FREQOUT 9, 10000, 1319 ' E6
How the Circuit Works
Here is a simplified version of the opamp circuit.·
circuit is called an inverting opamp circuit.·
Vi is the input voltage, Vo is the output voltage, and Ri and Rf are called the input and feedback resistors.·
In your circuit, Ri is two 10 k resistors in parallel, for an equivalent resistance of 5 k.·
Rf is a 10 k pot, and it can be adjusted anywhere from about 100 ohms·
to 10 k.
Opamp is short for operational amplifier.·
Here is the math operation that this operational amplifier circuit performs:
Vo = (-Vi * Rf / Ri)
Vo is the inverting amplifier's output voltage, and it is the negative of the input voltage (Vi), scaled by the ratio of the feedback resistor (Rf) divided by the input resistor (Ri).·
Keep in mind that you turn the potentiometer to control the value of Rf.·
the potentiometer is·
in the middle of its range of motion, Rf and Ri are both 5000, so Vo is simply the negative of the input voltage.·
When Rf is near 10 k, Rf/Ri = 10000/5000 = 2.·
So, the output voltage is -2 times the input voltage.·
If you turn the potentiometer the other direction, below 5000, to say, 500, then the output voltage is -1/10 of the input voltage.
Your Turn - Digital Potentiometer Control
Try replacing the potentiometer with the digital potentiometer from What's a Microcontroller
, Chapter 9.·
You can then modify the program to control the volume automatically, or even with the Debug Terminal.
The piezospeaker plays notes loudest at 5 kHz, and the device's response is optimal between 4.5 and 5.5 kHz.·
As the notes get lower, they are attenuated by the speaker and they sound quieter.·
With a digital potentiometer, the BASIC Stamp can be programmed to automatically adjust the volume of each note so that it compensates for the piezospeaker's tendency to play some notes louder than others.·
This is a project that will require some time, experimentation and tuning.·
This draft material is part of a forthcoming Stamps in Class text by Andy Lindsay.
by Parallax Inc - all rights reserved.··
Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 6:34:21 PM GMT