Alirezadigi
Alirezadigi

Reputation: 55

Use microphone in Raspberry Pi Pico (Micropython)

How can i use micropython firmware alongside a Max9814? I have written the code below but cant hear clear voice in audacity...

from machine import Pin, ADC
import ustruct , time

analog_value = machine.ADC(26)
conversion_factor =3.3/(65536)

samples = []

while True:
    reading = analog_value.read_u16()*conversion_factor   
samples.append(int(reading)) #print("ADC: ",reading)
time.sleep(0.002)



with open('Voice.bin', 'wb') as output:
     for sample in samples:
        output.write(struct.pack('<h', sample))

Upvotes: 1

Views: 3515

Answers (1)

Robin McBurnie
Robin McBurnie

Reputation: 21

Try changing

conversion_factor = 3.3/(65536)

to

conversion_factor = 3.3/(4096)

This is because, although the ADC result is returned as a 16-bit integer the actual result is only the lower 12 bits - it is a 12-bit ADC!

Using 65536 (16 bits), the resulting audio will seem quiet as it is only capable of reaching 1/16 of the full-scale range of a 16-bit value.

I would also suggest using the Normalise effect in Audacity, bearing in mind the audio will always sound a bit noisy.

A further point to bear in mind is that you sample rate is unlikely to 100% stable by doing the timing using code. If you want hardware-timed audio it is worth learning to use DMA. e.g. https://iosoft.blog/2021/10/26/pico-adc-dma/

Upvotes: 2

Related Questions