arduino uno as a class d amplifier

arduino uno as a class d amplifier

Okay, this is really dorky. I ported George Gardiner’s class d amp for an attiny over to an arduino uno. http://www.georgegardner.info/electronics/class-d-avr.html is what he did. Here’s my code, intended for the arduino IDE and an atmega328.

void setup() {
pinMode(9, OUTPUT); //pwm
pinMode(10, OUTPUT);//pwm inverted
pinMode(A3, INPUT); //analog input
// com1a1 is non-inverted, com1b1 and com1b0 is inverted,
TCCR1A = (1<<COM1A1) | (1<<COM1B1) | (1<< COM1B0) | (1<<WGM10) | (1<<WGM11);
// this should set 10 bit fast pwm mode, set clock full speed
TCCR1B = (1 << WGM12) | (1 << CS10) | (0<<CS11);
OCR1A = 0; // initialize
OCR1B = 0; // initialize
ICR1 = 0b0000001111111111; //set to 10 bit pwm

DIDR0 = 0x3F; // digital inputs disabled
ADMUX = 0x43; // measuring on ADC3, use the internal 1.1 reference

// edited the below line to improve sound quality from appalling to merely terrible
ADCSRA = 0b10101111;           // AD-converter on, interrupt enabled, prescaler = 128
ADCSRB = 0x40; // AD channels MUX on, free running mode
ADCSRA |= (1<<ADSC);  // Start the conversion by setting bit 6 (=ADSC) in ADCSRA
sei(); // set interrupt flag
}

void loop() {
}

ISR(ADC_vect) {
uint16_t analog_value;
analog_value = ADCL; // store lower byte ADC
analog_value += ADCH << 8; // store higher bytes ADC
OCR1A = analog_value;
OCR1B = analog_value;
}

As he did, I put a cap between VREF and ground, and used another cap to couple the audio signal into the arduino’s analog input. The inverted/noninverted PWM outputs from the arduino drive one of those cheap L298 dual h-bridge boards, which drives the speaker.
The best I can say is that it works. Audio is clearly coming out of the speaker, through the noise and clicks.

 

Short video clip of it allegedly working:

arduino class d amp

Leave a Reply

Your email address will not be published. Required fields are marked *