Making Sound

Audible Feedback the Chipaxe Way

By The Chipaxe Team

There are many electronic gadgets that beep when you press a button. This is known as audible feedback. We will recreate that audible feedback here with a Piezo speaker and a Microchip PIC12F683 eight pin microcontroller. The microcontroller will first sense a momentary switch press with a digital input. When the switch is pressed the software will generate a square wave at a fixed frequency in the audible range on a digital output pin connected to the Piezo speaker. The Piezo speaker will generate the tone for a short time and then wait for the switch to be pressed again. Figure 1 shows the completed project built on a breadboard.

Sound ProjectFigure 1. Final Creating Sound Project

Hardware

The PIC12F683 is installed in one of our 8-pin CHIPAXE modules that have a handy in-circuit serial programming (ICSP) header connector. The CHIPAXE programmer connects to this ICSP header so you can program the part in-circuit right from the USB port of your computer. The CHIPAXE programmer (shown in Figure 2) will also power the breadboard circuit with five volts from the USB port of the PC. In the final project setup, two jumper wires connect the power and ground from the CHIPAXE module to the power rails on the sides of the breadboard for the rest of the circuit to use.


The schematic for this project is shown in Figure 3. The momentary normally open switch is connected to the GP4 pin of the 12F683. The circuit also has a 10k pull-up resistor connected between the switch and the 5V rail. This makes the switch look like a high signal when it's not pressed and a low signal when it is pressed. The GP4 pin will be setup as a digital input in software.

The GP2 pin is configured as a digital output in software and connected to the Piezo speaker through a 10µF capacitor. The software will create a square wave pulse on that pin to drive the speaker. The square wave needs to be converted into a rounded signal to work best with the Piezo speaker so we place a 10µF capacitor in series between the GP2 pin and the Piezo positive lead. The Piezo speaker is then grounded. The connection table below describes how to connect all this on the breadboard using jumper wire kit.

Starter KitFigure 2. Chipaxe.com Breadboard Starter Kit

Connection Table

CHIPAXE – 8 Pin 1 at C6
Yellow Jumper – a6 to +rail
Yellow Jumper – j6 to −rail
Orange Jumper – j9 to j12
10µf 35V – Positive – h12, Negative – h13
Piezo – Positive – g17, Negative – i19
Yellow Jumper – f13 to f17
Yellow Jumper – j19 to −rail
Yellow Jumper – j22 to −rail
Orange Jumper – f22 to e22
Orange Jumper – b22 to b19
White Jumper – b8 to b17
10k ohm – a17 to +rail
Switch – d17 to d19
Schematic
Figure 3. Sound Project Schematic

Software

The software we'll use to control the PIC12F683 is the sample version of the micro Engineering Labs PICBASIC PRO compiler. This software is included with our breadboard starter kit (Figure 2) or can be downloaded for free from melabs.com. You don't need the full version to do this project; the sample version handles it fine.

The software takes full advantage of the SOUND command in PICBASIC PRO. This command creates the square wave that drives the Piezo speaker. To get started we need to setup the digital ports in the 12F683 by setting a few register locations. We make the pins digital by clearing the ANSEL register.

ANSEL = 0 ' Set I/O to digital

The internal comparator is turned off by setting the CMCON0 register to seven.

CMCON0 = 7 ' Comparator off

The GP4 pin will sense the switch so we need to make sure it is an input by using the input command followed by pin name. We use the optional port.pinname format.

input GPIO.4 ' Make GP4 and input

The main loop of code starts with the "main:" label.

main:

An If-Then command is used to test the GP4 pin for a low state which indicates the switch has been pressed.


If GPIO.4 = 0 then 'If switch pressed create sound


If the switch is pressed then the SOUND command creates a square wave for a period of time. The first value has be a value between 1 and 127. The value 1 produces a signal of about 78Hz and 127 produces 10,000Hz so the 100 value is somewhere between those. The second value is the duration which can be between 1 and 255. Each value represents about a 12 millisecond duration. In this case the sound lasts about 1.2 seconds.


sound GPIO.2,[100,100] 'Sound speaker for 1.2 sec


The If-Then command ends with the ENDIF command.

ENDIF


The GOTO command finishes the main loop of code by jumping the program control back to the main: label.

goto main 'Loop Back to test the switch


Software Listing

Here is the complete software listing for you to type into your editor. The PICBASIC PRO sample version comes with a free version of the MicroCode Studio editor. Our CHIPAXE Breadboard Starter Kit also includes that and instructions on how to set it all up.

'********************************************************************
'*   Name:Sound.BAS
'*   Author:CHIPAXE Team
'*   Date:2/15/2010
'*   Version:1.0
'*   Notes:
'* :CHIPAXE-8 Pin 1 at C6
'* :Yellow Jumper - a6 to +rail
'* :Yellow Jumper - j6 to -rail
'* : Orange Jumper - j9 to j12
'* : 10µf 35V - Positive-h12, Negative-h13
'* :Piezo - Positive-g17, Negative-i19
'* :Yellow Jumper - f13 to f17
'* :Yellow Jumper - j19 to -rail
'* :Yellow Jumper - j22 to -rail
'* :Orange Jumper - f22 to e22
'* :Orange Jumper - b22 to b19
'* :White Jumper - b8 to b17
'* :10k ohm - a17 to +rail
'* :Switch - d17 to d19
'********************************************************************

ANSEL = 0 ' Set I/O to digital
CMCON0 = 7 ' Comparator off
input GPIO.4 ' Make GP4 and input

main:
If GPIO.4 = 0 then 'If switch pressed create sound
sound GPIO.2,[100,100] 'Sound speaker for 1/10 sec
ENDIF

goto main 'Loop Back to test the switch


Jameco Parts Needed to Build the Project

Transducer
PIC12F683-I/P
Breadboard
PB Switch
Wire Jumper Kit
PIC Software

Next Steps

There are many options to this simple program. You can make the Piezo sound last longer or shorter by changing the values in the SOUND command. You can make the sound occur constantly unless the switch is pressed. This would be handy if you needed to keep an eye on something. Have it sit on top of the switch so if someone lifts up the item then the alarm will go off. You could also add another switch so you have one turn on the sound and the other turn it off. This could be a fun project to modify.

If you have any questions about this project please email us at [email protected]. You can get the parts used in this kit from Jameco and the CHIPAXE module from our website www.chipaxe.com. The kit used in this project was CHIP016.

If you are new to programming then check out the books of Chuck Hellebuyck. The CHIPAXE Team has contracted Chuck to write books based on the CHIPAXE modules and he has many more that we recommend. His website is www.elproducts.com.

We put together a packaged starter kit that includes Chuck's book "Programming PICs in BASIC" which uses our CHIPAXE Breadboard Starter kit. The book has several projects similar to this one here and our packaged kit includes the CHIPAXE Breadboard Starter kit and the Jameco components used in the projects to give you a complete getting started package. Check it out at our CHIPAXE.com website.