A method to make complex sounds with just an 08M2

Buzby

Senior Member
Looking for a low component method of making a sonar 'ping' for the subhunter, I thought this up.

The M2 chips have a Digital-to-Analog converter.
This can amplitude modulate a signal arriving on the +Vref pin.

By connecting a PWM output to the +Vref input the DAC can then modulate the PWM, with no extra components needed.
By adjusting the PWM 'on the fly' the tone can be changed as the amplitude varies, giving lots of possibilities.

The DAC output is very low current, but it can drive a piezo.
An amplifier ( LM386 or similar ) could be used to drive a speaker, but I wanted an ultra low component count.

Code:
#picaxe 08M2
#no_data

' Sonar ping
' ----------

' Hardware wiring
'
'                     __   __
'                    |  |_|  |
'                +v -|       |- 0v
'                    |       |
'                   -|C.5 C.0|->----------------------
'                    |       |                       |
'                   -|C.4 C.1|-<-                    |
'                    |       |   | Link              -| Piezo
'                   -|C.3 C.2|->-                    -|
'                    |_______|                       |
'                                                    |
'------------------------------------------------------ 0v
'
'
'
' C.0 is DAC output
' C.1 is Vref input
' C.2 is PWM output
'

Symbol  Alev1  = b2 ' Amplitude level 1 
Symbol  Alev2  = b3 ' Amplitude level 2
Symbol  Pduty  = b4 ' PWM duty

setfreq m8				' Run at 8MHz

dacsetup  %10100100 	' Set DAC to use external +Vref, DAC out to C.0

do	

	Pduty = 100  
	
	pwmout pwmdiv16, 2, 199, Pduty ' Set PWM 
	
	daclevel  31

	for Alev1 = 31 to 0 step -1    ' Make a descending amplitude envelope.
	  
		Alev2 = Alev1 * 9 / 10 
 		daclevel  Alev1
		pause 20
		Pduty = Pduty + 10 : pwmduty c.2, Pduty
 		daclevel  Alev2 
		pause 20		
		Pduty = Pduty + 10 : pwmduty c.2, Pduty
 		daclevel  Alev1
		pause 20
		Pduty = Pduty + 10 : pwmduty c.2, Pduty
 		daclevel  Alev2 
		pause 20
		Pduty = Pduty + 10 : pwmduty c.2, Pduty
 		daclevel  Alev1
		pause 20
		Pduty = Pduty + 10 : pwmduty c.2, Pduty
 		daclevel  Alev2 
		pause 20	
			  
	next


pause 4000

loop
It should be able to make lots of sounds this way, bird tweets, cat meows, etc.

Cheers,

Buzby
 

Attachments

hippy

Technical Support
Staff member
Excellent trick. You can connect the 0V of the piezo to C.4 and add LOW C.4 at the start of the program to avoid the cacophony of noise during downloads.
 

Buzby

Senior Member
Excellent trick. You can connect the 0V of the piezo to C.4 and add LOW C.4 at the start of the program to avoid the cacophony of noise during downloads.
Unfortunately I need the other pins for the rest of the device.
In fact, I'm a pin short. ( I could do it easy if I moved up a chip, but I'm determined to get this working with just an 08. )

There are only 3 pins left, and I need 4 inputs, so my next trick is to how detect 2 switches on 1 pin.
ADC with resistors is the obvious way, but, in keeping with my 'no extra components' challenge, I think I have a method which uses no extra components.

Stay tuned for developments !

Buzby
 

techElder

Well-known member
Except for your sworn aversion for adding extra components, you might consider adding a low pass filter (capacitor and resistor) to the PWM output for additional effects on the analog output (via Vref input.)
 

Bill.b

Senior Member
Hi Buzby

For you additional inputs why not use a resistor divider and an analogue input.


alin.jpg

This part of the code for my bot to decode the selector switch. The select case is an alternate way to select the switch positions.

Code:
readadc10 ModeSw,ModeSel

	if ModeSel > 640  then  		' Mode 7 - Learn / playback  - load program in slot 3 	
		Mode = 7
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,("   Learn mode   " )
      	 run 3
		goto main
	endif
	if ModeSel > 600 then   		' mode 6 - follow Me   - Load program in slot 2
		Mode = 6
		
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,("   Follow ME    " )
      	 run 2
		goto main
	endif
	if ModeSel > 550 then   		' mode 5 - Light Avoidance   - Load program in slot 2
		Mode = 5
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,("Light Avoidence " )
      	 run 2
		goto initalise
	endif
	if ModeSel > 460 then   		' mode 4 - Light follower   - Load program in slot 2
		Mode = 4
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,(" Light Follower" )
      	 run 2
		goto main
	endif
	if ModeSel > 360 then   		' mode 3 - Line follower   - Load program in slot 2
		Mode = 3
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,(" Line Follower " )
      	 run 2
		goto main
	endif
	if ModeSel > 250 then   		' mode 2 - Remote Control RF Link   - Load program in slot 1
		Mode = 2
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,(" Remote Control " )
      	 run 1
      	  goto main
		
	endif
	setint %01000000,%01000000,b
	if ModeSel > 80 then    		' mode 1 - Auto Roam    
		Mode = 1
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
      	 serout OLED,Baud,("  Auto Roam  " )
      	 goto roam
		goto main
	endif
	if ModeSel < 80 then    		' mode 0 - Wall Follower  
		Mode = 0
		serout OLED,Baud,(254,1)
		 serout OLED,Baud,(254,128)
		 pause 10
	      b6 =3
		gosub rgbcolour
      	 serout OLED,Baud,(" Wall Follower  " )
      	 goto Wall1
		goto main
	endif
	Mode = 0

Bill
 

jims

Senior Member
Nice idea, maybe we should call you buzzerby. :)

Speaking of which, see http://www.picaxeforum.co.uk/showthread.php?28427-Tune-help-req&p=293626&viewfull=1#post293626

I used a buzzer instead of a piezo, so same number of parts and probably cheaper & louder than a piezo. DTMF-like sounds come easy.
erco...What "buzzer" do you use?? I tried a Radio Shack piezo buzzer P/N 2730793 with my 5.2 volt supply for the 08m2. Can only get an audible sound with a 100Msec or longer high signal. The faster PWM signals don't do it.Thank you, JimS
 

erco

Senior Member
erco...What "buzzer" do you use?? I tried a Radio Shack piezo buzzer P/N 2730793 with my 5.2 volt supply for the 08m2. Can only get an audible sound with a 100Msec or longer high signal. The faster PWM signals don't do it.Thank you, JimS
Sorry for the delay, I missed this request. Ten for a buck & change: http://www.ebay.com/itm/1Set-10Pcs-Active-Buzzer-Magnetic-Long-Continous-Beep-Tone-Alarm-Ringer-5V-12MM-/201406944357

Be sure to wire them active low and they will work fine connected directly to a PICAXE, no driver transistor required. They are polarized, so connect the + lead to +5V (3xAA works fine) and the other pin to a PICAXE output pin. Beeper's on when the pin goes low. PICAXE pins can sink a lot more current than they can source. The beeper sounds pretty wimpy if you connect it between an output pin and ground (active high).

BTW I just sent tones through the buzzer in that demo video.
 

jims

Senior Member
Sorry for the delay, I missed this request. Ten for a buck & change: http://www.ebay.com/itm/1Set-10Pcs-Active-Buzzer-Magnetic-Long-Continous-Beep-Tone-Alarm-Ringer-5V-12MM-/201406944357

Be sure to wire them active low and they will work fine connected directly to a PICAXE, no driver transistor required. They are polarized, so connect the + lead to +5V (3xAA works fine) and the other pin to a PICAXE output pin. Beeper's on when the pin goes low. PICAXE pins can sink a lot more current than they can source. The beeper sounds pretty wimpy if you connect it between an output pin and ground (active high).

BTW I just sent tones through the buzzer in that demo video.
Thank you erco... JimS
 

tmfkam

Senior Member
Some naked piezo devices, once 'moved' by applying a voltage to them, may not quickly return to their rest position unless a voltage is applied in the opposite direction. I have found that a resistor in parallel with the piezo will reduce the need for this significantly. Something in the order of 1k -10k may be beneficial? [Value found by experimentation]
 

erco

Senior Member
Just for clarification, those are mechanical buzzers I linked to, although they do sound like high-pitch piezos. I would call them beepers, but Ebay sellers call them "active buzzers" so I'll stick with their nomenclature. They beep steadily when connected to 5VDC, no input signal required. In that video, I was going some offbeat experimenting with sending a TUNE through the buzzer, which gave 7 notes of DTMF-like sound followed by a pure DC tone.

 
Top