For a school project I made a circuit that amplifies a microphone input and sends it to the arduino analog input, there it samples it at approximately 16kHz. I did this like this:
#define FASTADC 1
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
void setup(){ // set prescale to 64
#if FASTADC
sbi(ADCSRA,ADPS2) ;
sbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
endif
Next I just analogread on the input and save this as 8bit data on a 23LC1024 (1Mbit SRAM) chip. But now I need to send this sampled values trough an 8ohm speaker so I can hear what I recorded and saved.
I have read a lot about pwm and I think this is the way to go for this project, however I know little about setting register bits, and I already have set my prescaler to 64 for the analogread so I dont know how this effects all the pwm tutorials I find online.
I use this time test in the setup:
start = millis() ;
for (i = 0 ; i < 1000 ; i++)
analogWrite(speaker, i);delay(44);
timeTest = (millis() - start);
Serial.print(timeTest) ;
Serial.print("msec / 1000 calls -> ");
Serial.print(1000000/timeTest);
Serial.println("Hz writing output") ;
My time test results in "+-18000Hz sampling output" so I thought I could just connect my speaker in series with 250ohm resistor to a pwm pin and write the data that comes from the SPI to the digital pwm pin with analogWrite(speakerPin, value) directly but this results in only a sinus-like beep.
I already looked at the resulting waveform of the recorded samples and the sampling part is ok, with a mean value of 125 and the signal always staying between 0-255.
All help is welcome since I am stuck here for over three days, thanks in advance !