Siren Sound

ccdubs

Member
Hi All,

I need to use a picaxe 18X to drive an amplifier that inturn feeds a horn. I would like the output sound to resemble a police siren or a simple frequency ramp up and down(from say 100Hz to 1000Hz to 100Hz and repeat) depending on input status. The result of the latter is an effort to simulate the rise and fall of an old air raid type mechanical siren being turned on and off.

I have used the 18x and the PWMOUT command before to generate pulsed 1kHz tones but now I want to simulate siren tones.

The sound needs to be continuous, when I have tried this with PWMOUT there is always an audible break in sound as the frequency rises, if memory serves the steps of frequency change were also very noticable.

As always any assistance is greatly appreciated.
 

moxhamj

New Member
Agree, those little steps in the tone, or brief pauses are not very natural.

I'll leave it to others to maybe suggest picaxe solutions. There are lots of circuits out there that can be built with other chips, eg 555s, op amps, XR2206, even the old standard sound chip from console games from the 70s/80s, the 76477. Not sure if you can even get that one any more, but it was a 40 pin chip that could do sirens, bombs falling, even the explosion at the end, all with one chip.

A siren needs a VCO (voltage controlled oscillator). The tone itself might be a sawtooth or a sine wave (a square wave sounds a bit harsh). And the driving waveform might be a sine or sawtooth.

You could use a picaxe to generate the sawtooth and then filter it a bit with an R/C network and then drive something like an xr2206. Or use two xr2206s.

From memory I'm not sure if a sine or a sawtooth sounded most natural for a siren. Probably a sawtooth as the frequency is rising in a linear fashion.
 

boriz

Senior Member
You can to do it easily, but it would be a waste of an 18X. IE:
Code:
#picaxe 14m
'Piezo disk connected between ground and output1 (physical leg 12)
do
	for b0=1 to 255
		pulsout 1,b0
	next b0
	for b0=255 to 1 step -1
		pulsout 1,b0
	next b0
loop
(Tested on 14m coz I just happened to have one on the breadboard, but it should work with all Picaxes)

Your 18X won’t be able to do much else during this loop. But that might not matter. Depends on your application.

Assuming your amplifier is expecting a standard Audio level input. You can replace the Piezo disk with two resistors to divide the signal down to <1v. Try say, 10k and 1k, with the top of the 10k to the Picaxe output, the bottom of the 1k to ground and the junction of the two is the active signal. A DC blocking capacitor may be necessary. Dunno without more details.
 

Jeremy Leach

Senior Member
After a bit of browsing, air-raid siren sounds were typically two-tone, with the tones at 5/6 ratio (a minor third apparently), going up to around 500Hz. Tones quite harsh due to the way the sounds were produced.

It's something that would just need to be experimented with to get sounding right, but you never know - two square waves at this ratio might sound ok.

Because 500Hz is quite low, there is a dim possibility you might be able to generate the square waves in code. I've no idea if this would work but ...

Code:
For Delay = ?? to ?? 'for ramp up
    For Address= 1 to 30
	    Read Address,Pins
	    Pulsout pin?, Delay
    Next
Next
You set up 30 addresses in EEPROM with the data to change two pins. You need 30 addresses because if you think of the 5/6 ratio both signals will line up in 30 counts. But it might just all be too slow !

Apart from this, you could just use the picaxe as an envelope generator feeding two oscillators (ie use voltage controlled oscillators and use PWM signal driving resistor and capacitor to get an 'envelope' voltage to drive the oscillators). Or could feed just one oscillator that drives a binary ripple counter with taps off it at dive-by-10 and divide-by-12 to get the two frequencies.

Or use two 08Ms, with bit-banged waves. e.g a while ago I messed about with bit-banging sounds, and a siren effect http://www.picaxeforum.co.uk/showthread.php?t=5881&highlight=hard+way
 
Last edited:

BeanieBots

Moderator
The PWM timer is reset every time a PWMout command is issued.
(hence the 'clicks').
Only the X1 parts have a solution to this problem. See command "PWMduty".
 

ccdubs

Member
Thanks everyone for the feedback.

I would like to give Boriz's suggestion a go. But, I forgot to mention that I would ideally also like to use a 50% duty cycle and I can't see how this can be acheived with pulsout? I assume Boriz's solution would have quite a high duty cycle as the pin is only low for as long as it takes the FOR loop to restart...a couple of clock cycles I would have thought.

The pause function is of no use as the resoltion isn't good enough. I guess I could use a second pulsout command on a different pin to simply provide the delay. Any thoughts?

Unfortunately the hardware is not available for a couple of days so I can't test yet.
 

moxhamj

New Member
http://www.elecfree.com/circuit/IC-555/american-police-car-siren-by-ic-555/

Shows how to tweak pin 5 of a 555 to alter the frequency. The first 555 could be a monostable or an astable. The first 555 could even be a picaxe with an R/C filter so that as the voltage ramps up in 1/256 steps it comes out smooth.

You could build it first on a protoboard with two 555s. Then replace the first 555 with a picaxe. Then add the R/C filter (might just end up a C, somewhere round the transistor).

The advantage of picaxe instead of the first 555 is you can ramp up, then stay at that value for a while then ramp down again. Like an air raid siren.
 

boriz

Senior Member
Yep. To reduce the load on the Picaxe, an external 555 would be ideal. Here’s the code modified for 50% duty (squarewave):
Code:
#picaxe 14m
setfreq m8
'Piezo disk connected between ground and output1 (physical leg 12)
do
	for b0=1 to 255
		toggle 1:pulsout 0,b0
	next b0
	for b0=255 to 1 step -1
		toggle 1:pulsout 0,b0
	next b0
loop
Output 0 (14m physical leg 13) is a dummy used only for the timing, though as a side effect, an LED connected here gives a pulsing effect synchronised to the sound. The Piezo is still connected to output 1 (14m physical leg 12), but since I’m using the toggle command, the frequency is halved, so I’m doubling the clock frequency (Setfreq m8).

Something interesting happened while experimenting that I cannot yet explain. If the Pulsout pin is changed to 1, the same as the Toggle pin, the volume goes up! IE:
Code:
#picaxe 14m
setfreq m8
'Piezo disk connected between ground and output1 (physical leg 12)
do
	for b0=1 to 255
		toggle 1:pulsout 0,b0
	next b0
	for b0=255 to 1 step -1
		toggle 1:pulsout 0,b0
	next b0
loop
The frequency sounds about the same, but the tone is much louder and clearer. On the scope the wave has changed to something difficult to describe. Certainly not a square wave. More like a short sequence of pulses (repeated) with an overall symmetrical total-high and total-low time, but only part of the pattern changes length with frequency.

So why would this give a much louder and cleaner sound from a Piezo than just a straight squarewave?
 

ccdubs

Member
Thanks Boriz,

Can you please explain what is happening with this syntax:

"toggle 1:pulsout 0,b0"

I've never seen this kind of notation before (combining two commands), any idea where i can learn more about this?

From what I think I know, this command will be toggling pin 1 everytime it receives a leading edge from the pulsout command. Right?

I'm interested in what response you receive from your last post. Is there anyway you can post the waveforms? I'm suspicious that the extra volume may be percieved rather than measured due to the human ears sensitivity to frequency.
 

boriz

Senior Member
OOPS!

Sorry, I seem to have pasted the wrong code for the second example. It Should be the same as example one, but with the Pulsout on pin 1, thus:
Code:
#picaxe 14m
setfreq m8
'Piezo disk connected between ground and output1 (physical leg 12)
do
	for b0=1 to 255
		toggle 1:pulsout 1,b0
	next b0
	for b0=255 to 1 step -1
		toggle 1:pulsout 1,b0
	next b0
loop
@Ccdubs:
Code:
 toggle 1:pulsout 1,b0
Is exactly the same as:
Code:
toggle 1
pulsout 1,b0
The colon is just a method of putting more than one statement on the same line. EG:
Code:
Do:bit2=bit0:bit3=bit1:bit0=pin0:bit1=pin1:toggle 1:pulsout 1,b1:loop until bit0<>bit2
Is exactly equivalent to:
Code:
Do
   bit2=bit0
   bit3=bit1
   bit0=pin0
   bit1=pin1
   toggle 1
   pulsout 1,b1
loop until bit0<>bit2
I just find it convenient to group certain functions. It’s personal preference.

I will try to sort out a photograph of the trace for you.
 

boriz

Senior Member


My interpretation of the transitions:

A. Toggle.
B. PULSOUT begins.
C. PULSOUT ends.
D. Next Toggle.

My interpretation of the plateaus:

A-B. Minimum delay between statement execution. Interrupt poll etc.
B-C. Duration of the PULSOUT command.
C-D. The rest of the loop executing. I’m using some additional code here to allow frequency changing, so this plateau will be a little shorter in the straight siren code.

What follows is the same pattern again but inverted. Timescale between A and D is 5.5mS. Why is this louder and clearer than a squarewave?
 

ccdubs

Member
Hi Boriz,

The following is a hypothesis only.

While the trace you have shown has a period of 5.5ms (180Hz), the fact that there are 2 leading edges means that the frequency is more like 360Hz. The human ear percieves loudness differently at different frequencies. This is the origin of the dBA weighting for sound, see http://en.wikipedia.org/wiki/A-weighted .

According to the graph on Wikipedia the difference between 180Hz and 360Hz is about 5 dB which is almost twice the percieved loudness.

An interesting experiment would be to alternate between each bit of code but use half the period for the "abnormal trace" compared to the "normal trace" and see if they sound similar. If not.....buggered if I know, maybe something to do with mechanical limitations of the piezo????
 

papaof2

Senior Member
dB

According to the graph on Wikipedia the difference between 180Hz and 360Hz is about 5 dB which is almost twice the percieved loudness.
QUOTE]

Sound level in dB is a logarithmic scale, so a change of 3 dB doubles (or halves) the perceived level. A change of 6 dB is a 4 times change in perceived level; the graphed 5 dB change makes the one sound about 3 times as loud as the other.

John
 

LizzieB

Senior Member
Also:

Piezos have a very non-flat frequency response, so if some component of the signal is closer to a resonance there will be some enhancement.

The harmonic content of the waveforms will be very different.
 

boriz

Senior Member
Sorry. Not 5.5mS.

5.5 DIVISIONS on a 0.5mS timescale. So 2.75mS. Which is 363Hz. But the complete repeat sequence is twice that, (11 Divisions), which is 91Hz.

But the tone I heard was more like 1kHz!

Prolly all sorts of subtle interplay between the Piezo resonance/capacitance and the harmonics. But anyway. Louder is louder :)
 

ccdubs

Member
Ok time to re-open this thread.

I have finally been able to bench test and unfortunately I can't get the frequency response I want using the method suggested by Boriz. Even with the following code I only get an output frequency of 165 Hz and I presume this is due to the overhead in the micro?

do
toggle 3: pulsout 0,1
loop while w3 > b11

When I do the same with a FOR loop I get 287 Hz. I guess I could increase the clock speed to 16MHz to achieve my desired response but was wondering if there is another way?

What I want to do is rise smoothly from 600-1200Hz in 10s, hold this for 20s and ramp back down smoothly to 600 Hz in 10s. Ideally using no extra electronics.
 

BeanieBots

Moderator
I think you have come to the point where you've exausted the code possibilities and need to seriously consider extra hardware.
The only option I can think of to keep to a purely PICAXE solution would be to use a 28X1 and explore the PWMduty command. However, even then there is no gaurantee that the PWM steps won't be noticeable but at least the update 'clicks' shouldn't be an issue.
 

Jeremy Leach

Senior Member
Ok, here's a last-ditch idea: Use the OSCTune register (see Calibfreq command). This varies the picaxe clock frequency with a value -15 to +15. So set the PWM frequency and twiddle the OSCTune value in code and see what happens ;) Might be rubbish, but might not !

BUT - make sure you put the value back to 0 after code has run.
 

InvaderZim

Senior Member
There's a backdoor way to scale the picaxe clock freq, right? Like how it's used for low power apps, by poking OSCCON?

You could set up a pwm, then change the whole chip clock frequency. Note that any pauses will scale as chip frequency does, so without compensation your low notes would not only be lower, but last longer than your high notes.

As for being glitch-free...well, maybe this wasn't such a good idea after all. It'd probably glitch as the chip freq changed. But perhaps worth a try : )

I'm not sure what the valid range for OSCCON is.

Code:
symbol OSCCON = $8f   ' Oscillator control SFR register for speed up/down

poke OSCCON,%00000000 ' = 31.25 kHz slow down (1/128 speed of 4MHz)
poke OSCCON,%01100000 ' = 4 MHz; speed back up
 

eclectic

Moderator
Based on Dippy's post:

http://www.picaxeforum.co.uk/showthread.php?t=5872&highlight=jeremy+pwm*

I've played with this program,
Code:
#picaxe 28x1
symbol delay = b10
delay = 20

peek $12,b1 ' read value
b2 = b1+2   'add 
poke $12,b2

for w3 = 255 to 195 step -1
w4 = w3 *2
pwmout 1,w3,w4

pause delay
next


for w3 = 195 to 255
w4 = w3 *2
pwmout 1,w3,w4
pause delay
next

poke $12,b1 'return value to normal
It might be useful for something, but it's not a siren. :-(

e
 

ccdubs

Member
To close this thread off, I thought I'd let you know how I did this in the end. Unfortunately a PICAXE was not used.

I decided to go for a recorder chip, this allows me to generate any sounds I like on my computer, store them on the chip and playback as desired. I found an off the shelf device to do this and while it's a bit more expensive than something I could make up the flexibility is worth it.

Thanks for all your input.
 
Top