AO-SPK

From Curuxa

Contents


Low power speaker.

It can be used for generating simple (or complex) sounds at any audible frequency, such as in alarms, indicators...

Electronic circuit

Schematic

Bill of materials:

  • 1x Low power speaker, usually piezoelectric
  • 1x 1kΩ 1/4W resistor
  • 3x Straight female headers (usually sold as 40-pin strips)
  • Some thin wire
  • Some heat shrink tubing

As any other speaker, it will sound when a variable current flows through it.

Avoid supplying constant voltages to the speaker.

The simplest way to generate sounds is by setting an output digital pin on the microcontroller as 1 and 0 repeatedly, that would generate a square wave.

Wiring

Printable version: Single, Quad

Construction

AO-SPK AO-SPK

Test

The following code can be compiled using SDCC.

MBP8, delays

/*==================================================================
* Sample program for AO-SPK using an MBP8
*
* Plug AO-SPK to GP5 (pin 2).
*
* http://curuxa.org
*=================================================================*/
 
#define OSC_8MHz
 
#include <MBP8.h>
#include <Delays.h>
 
ConfigBits(_CPD_OFF & _CP_OFF & _BOD_OFF  & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO);
 
#define Spk GP5
 
void Setup() {
	AdcDisable();
	SetDigitalOutput(5);
	SetIntosc8MHz();
}
 
 
void main() {
	Setup();
 
	while(1) {
		Spk=!Spk;
		Delay(1);
	}
}

MBP8, Timer0

/*==================================================================
* Sample program for AO-SPK using MBP8 Timer0
*
* Plug AO-SPK to GP5 (pin 2).
*
* http://curuxa.org
*=================================================================*/
 
#include <MBP8.h>
 
ConfigBits(_CPD_OFF & _CP_OFF & _BOD_OFF  & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO);
 
#define Spk GP5
 
Interrupt {
	Spk=!Spk;
	TMR0=114;
	T0IF=0;
}
 
void Setup() {
	AdcDisable();
	SetDigitalOutput(5);
 
	//setup timer
	//PIC12F683 runs by default at 4MHz
	//Fosc=4MHz, Tosc=250ns, Tcy=1us
	//for a 440Hz tone whe need a 880Hz timer frequency. That's 1136 instructions per cycle
	//...1:8 prescaler (PS=0b010). 142 TMR0 counts  per cycle. preload=114	
	PSA=0; //assign prescaler to Timer0
	T0CS=0; //clock source=internal oscillator
	PS2=0; //prescaler... 	PS1=1;
	PS0=0;
	T0IF=0;
	GIE=1; //enable global interrupts
	T0IE=1; //enable TMR0 interrupt
}
 
 
void main() {
	Setup();
 
	while(1) {
		//do nothing
	}
}

MBP18, delays

/*==================================================================
* Sample program for AO-SPK using an MBP18
*
* Plug AO-SPK to RA1 (pin 18).
*
* http://curuxa.org
*=================================================================*/
 
#define OSC_8MHz
 
#include <MBP18.h>
#include <Delays.h>
 
ConfigBits1(_CP_OFF & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO);
 
#define Spk RA1
 
void Setup(){
	//set all pins as digital, instead of analog inputs
	ANSEL=0;
 
	//set RA1 as a digital output
	TRISA1=DigitalOutput;
 
	SetIntosc8MHz();
}
 
void main(){
	Setup();
 
	while(1){
		Spk=!Spk;
		Delay(1);
	}
}
Retrieved from "http://curuxa.org/en/AO-SPK"