ADC audio monitoring on 08M2

Lup

New Member
Hi everyone on picaxe forum!

I burned my first picaxe chip so I think this is the right moment for some educated help from fellow picaxers :)

Yes, I am a total noob when it comes to uCs, programming and whole IC field. I wanted to buy a module that does what I want it to do but to my surprise I only found out there is nothing like this out there. So I am trying to graduate in three complex areas in three weeks.


I want to let Picaxe listen for amplified audio signal through ADC on pin C.1. If bit value on ADC is below certain threshold then timer/counter starts and after 5 minutes or so it sets pin C.4 low.

Now the tricky part:
I know audio volume (dB) is logarithmic, not linear, like bit value on ADC reading (0-255: perfect even steps).

Multimeter revealed approx. this:
Volume min .I.........................................................................I. Volume max
AC(V)............ 0.2.....0.2x.....0.3.....0.4.........1.5.........3......5......8

When no song is playing, you can hear faint background noise from the speaker (which is ok) and voltage reads 0.2 V.

Unfortunately my Multimeter is not sensitive enough to tell me hundredths and thousandths of Volts AC. As you can see from the "picture", voltage goes up reluctantly and then goes up really fast just before hitting max volume. Here I burned my picaxe on those 8 volts :). So my questions are:
1)How do I protect Picaxe from that high peek voltage(I want to retain as much sensitivity as I can so it could read those low V values as precisely as possible)? Or should I take care or that high voltage with resistor and program Picaxe to automatically adjust it's sensitivity on reading low value bit on ADC input?

2)Will it work with only one wire coming from amplifier to picaxe or do I need the other wire going also from amplifier to ground? The player/amplifier and picaxe share the same V0, but amplifier is powered by 18 volts and picaxe is powered with 5 V thanks to DC DC stepdown converter.

3) Do I need to use diodes since amplified audio is AC and not DC?

4) How to program it?
I want it to:
- listen for ADC continuously
- if ADC bit value is too low for 5 minutes, it puts pin C.4 low thus ending itself.
- if during that countdown it registers high bit value again (another song playing), it resets the timer
(By putting C.4 low the whole cirtuit shuts down by cutting power to itself so therefore that command is the last command of the program.)



Pseudo code:
Code:
ADCmonitor:

Readadc pinC.1, b0
If b0 <= xxx then      'xxx=unknown threshold value 
   timer
timer = timer+1second
elseif 
b0 > xxx then timer=0
Endif

If timer > 5minutes then
   Low C.4
else 
goto ADCmonitor
Endif
Any suggestions? Ideas? I see the light at the end of the tunnel but also a lot of obstacles along the way.

Thank you all!
 
Last edited:

hippy

Technical Support
Staff member
Just using a current limiting resistor in series should allow the PICAXE to withstand 8V and even higher.

To monitor for audio at low levels you may need to use an op-amp amplifier and will likely need to monitor both left and right channels. You may also need some form of rectification of the AC signal and peak detection circuit to ensure the PICAXE can detect peaks of signal.

You will need a common 0V connection between the PICAXE circuit and the existing amplifier.
 

AllyCat

Senior Member
Hi,

As hippy says, then:

"Audio" is an alternating signal, but the PICaxe ADC can measure only an "instantaneous" voltage, so you need to decide how you are going to "couple" and "bias" the signal into the PICaxe. Also, audio has a large frequency range, from say 20 Hz to 20 kHz; the PCaxe ADC can just about follow the lowest frequencies (i.e. see some higher voltages and then some lower voltages in sequence) but at high frequencies it's just "pot luck" how high or low will be each measured value.

One possibility is to use "hardware", i.e. a diode, a capacitor and a "bleed" resistor to "rectify" the signal (i.e. convert it to "dc"); the diode charges up the capacitor to the highest voltage encountered and the resistor slowly reduces the voltage again ready for the next peak (a multimeter on an "ac" range normally works this way). The PICaxe ADC only needs to read the capacitor voltage occasionally, but a problem is that the diode has a forward voltage drop (typically 600 mV) so small signals might not be detected:

A solution is to couple the audio signal via a capacitor to the diode and bias their junction at around 1 volt (using a resistor chain) so that the ADC input "sits up" a little (e.g. at 400 mV). The PICaxe (ADC) could measure this voltage when there is "silence" and then subtract the value from any subsequent measurements. You may need to attenuate the raw audio signal (with a resistor divider) so that the "peak" signal applied to the PICaxe pin never exceeds its supply rail (~5 volts).

Another possibility (which needs less hardware) is for the PICaxe to take a large number of measurements (at least a hundred) and calculate the highest and/or the lowest voltages encountered. The highest and/or lowest values will indicate the peak or peak-peak amplitude of the audio. Again the audio will need to be capacitor coupled, but you may need to attenuate the signal more, so that the entire peak-peak audio waveform fits within the PICaxe's 0 - 5 volt rails.

Once you have a voltage measurement you have two problems (at least). Firstly, low and high frequency signals don't sound as "loud" as mid-range signals (~1 kHz), This means that "hiss" and "mains hum" (50 Hz) may affect the voltage measured much more that they sound as if they should. Unfortunately there's not much the PICaxe can do about this, it needs a more complex hardware interface (i.e. a "filter").

The other factor, as you suggest, is the logarithmic characteristic of "loudness" (for which the dB scale was devised). A large "dynamic range" (perhaps > 40 dB) will be difficult, but less than that can probably be handled with "piecewise linear" analysis, e.g. voltages in ranges between 100-200 mV, 200-400 mV, 400-800 mV, 800-1600 mV and 1600-3200 mV may be considered to produce similar changes in loudness.

Complete coding will need to be done gradually, but you'll need a DO loop (or similar), a sequence of IF commands (or SELECT.. CASE, etc.), the time variable (for an M2) or timer command (for an X2) and of course HIGH or LOW to drive the LED ouput pin.

Cheers, Alan.
 
Last edited:

hippy

Technical Support
Staff member
I reckon a simple diode-capacitor charge circuit would probably work. With its time constant long enough it should be okay for READADC in a loop.

The only thing then is getting the charge voltage high enough for the minimum signal level of the audio input. A high gain op-amp or comparator would probably do the job. That it's clamping/limiting negative or higher positive audio shouldn't be a problem.

Code:
      ___ 
L )--|___|---.   |\
      ___    }---| >---|>|----.-----.----> PICAXE ADC
R )--|___|---'   |/          _|_   .|.
                             ---   |_|
                             _|_   _|_
 

Lup

New Member
Happy End

Praise the PICAXE, praise you, guys who helped me to understand and to see further!

Today a new chip arrived so I thought I would give it a go. Experimenting with components I had by me I was able to actually see what I was doing - simple do-and-see. I took care not to do the same mistake and wisely used resistors to limit current (thanks hippy!). I got close but it was not enough. I was thinking I should give the op-amp a go or perhaps PICAXE-sound IC (thanks, Technical). I see now where were you heading with your advises.

Then I tried something I did not tried before because I thought since the beginning it can't work. But it does!!

So I bypassed the whole amplifier (that adds a lot of noise, too) and simply put V0 and V+ wire from Bluetooth module audio outputs directly to picaxe V0 and C.1 pin. Then changed READADC C.1,b0 to READADC C.1,w0 and it bloody works! When music is not playing, the value pings from 0 to 256. and even on the lowest volume it goes up to 512 and higher. I tried pumping music (some techno), rock and relaxing music and it works just fine :) I call it a luck but hell I take it because it means no more IC's, no more parts to buy, no more programming sub routines etc.

Thank you Hippy for your hints, thank you Technical for that sound pdf, thank you AllyCat for insight into audio processing.
What I found along the way is the fact that more I play with PICAXE more I love it - like all of you guys I guess so this is definitely not the end of my PICAXE ride. It is just the beginning!

:D

If anyone is interested what bluetooth module works with PICAXE like that, it's KRC-86B V4.0
 

Lup

New Member
In a case someone would be interested here is the code. Just to clarify what it does:
PICAXE listens for audio signal coming from bluetooth module. When music is playing, no action is taken. When music stops playing, PICAXE starts timer and when it reaches cca.5 minutes, it puts pin C.4 low thus powering off the whole IC and itself. Also PICAXE waits if button is pressed anytime for at least 0.5 second. When it is, it puts C.4 low again. There is no routine for power on because power on is done by hardware button - that allows for almost no power loss during OFF state except for some tiny tiny loss on MOSFET leakage. And yes, there are no two buttons, only one DPST tactile button. If someone is interested for schematics, I can put them here, too.





Init: 'setting up nicknames for variables
Symbol button_press = b0
Symbol audiomon = b3
Symbol PICAXE_Ears = w1
Symbol countdown = b8
GoTo PowerOn


PowerOn:
pwm 2,1,5 pause 10 pwm 2,2,5 pause 10 pwm 2,3,5 pause 10 pwm 2,4,5 'start up chime (could be worse :)
pause 10 pwm 2,5,5 pause 10 pwm 2,6,5 pause 1 pwm 2,7,5 pause 1 pwm 2,8,10
pwm 2,7,5 pwm 2,8,5 pwm 2,7,5 pwm 2,8,5 pwm 2,7,5 pwm 2,8,40

high C.4 'turning on power to transistor so the IC will keep powered up
pause 2000 'this is just to prevent accidental turn off by button press
GoTo KeepPower


KeepPower:
let countdown = time + 45 'setting up timer (time value will catch up
GoTo PowerOffManual 'with countdown value within ~ 5 minutes)


PowerOffManual: 'what to do when you press a button
button_press = 0
do while pinC.3 = 1
button_press = button_press + 1
pause 100
loop
if button_press > 5 then 'if you keep pressing for at least ~ 0.5 seconds
goto ShutDown 'then we are going to shutdown procedure
else
goto BatterySave 'otherwise we're going to check sleep mode
endif
GoTo BatterySave 'if button was not pressed, skip the previous and jump to other part of the program


BatterySave:
button_press = 0
readadc10 c.1, audiomon 'read ADC values with high sensitivity
debug

if PICAXE_Ears <= 512 then 'when nothing is playing (audio signal is almost 0)
if countdown < time and PICAXE_Ears<= 512 then 'and 5 minutes elapsed since last audio signal was detected, then
goto ShutDown 'we're gonna sleep
endif
else
let countdown = 0 'otherwise we reset countdown and time so our 5 minuter counter would work again..
let time = 0
goto KeepPower '...and we repeat setting up counter, checking for button press and sleep again
endif
GoTo PowerOffManual 'if music is playing, PICAXE has a break. No, not really. It is asked to go again and check the
'button and then sleep routine and again and again...

ShutDown:
pwm 2,6,20 pause 200 pwm 2,6,20 pause 400 pwm 2,7,5 pwm 2,6,5 'final bye-bye chime
pwm 2,5,5 pwm 2,4,5 pwm 2,3,5 pwm 2,2,5 pwm 2,1,10
pause 1000
low C.4 'Power down the transistor which turns off MOSFET
'thus powering down whole IC. Now the PICAXE has a break!
 
Top