Sigurthr
0
- Joined
- Dec 11, 2011
- Messages
- 4,364
- Points
- 83
Hey everyone! I thought there was an overall arduino thread but a few searches revealed that I must have been misremembering such, so here it is.
I'm new to Arduino, mostly because I held off for years after having to wade through electronics neophytes who wrote ten lines of code, can't identify a capacitor, yet call themselves electronics enthusiasts or the like. Also, I didn't (and mostly still don't) have need for a MCU, except in few rare circumstances.
That being said, only a few days in I've put together a few cool things like a SSTC Interrupter (I've since ported the Digispark code I wrote over to Arduino, so it isn't just ATTiny85 specific now).
My most recent arduino project is threefold; anyone can take analog input and send it to a digital PWM output, but the real trick is getting quality audio out! Out of the box the Uno has a 10KHz sampling rate on the ADC pins and a 1KHz PWM frequency on pin 5 and 6, with half that rate on the others. After doing a lot of digging, reading of obscenely long datasheets, and scouring the web for hours I've been able to piece together and modify several chunks of code to increase the input and output speeds to over 64KHz!
The PWM output on Pin 5 and 6 is set to 64KHz carrier.
The ADC Clock is set to 1MHz, which yields a 77KHz sampling rate (that's better than your PC's inputs!).
The result:
Arduino Hi Fi Audio PWM - YouTube
Here's the code and schematic for anyone who wants to try (that's the whole idea behind arduino isn't it? standardized platform and software for easy sharing!)
I'm new to Arduino, mostly because I held off for years after having to wade through electronics neophytes who wrote ten lines of code, can't identify a capacitor, yet call themselves electronics enthusiasts or the like. Also, I didn't (and mostly still don't) have need for a MCU, except in few rare circumstances.
That being said, only a few days in I've put together a few cool things like a SSTC Interrupter (I've since ported the Digispark code I wrote over to Arduino, so it isn't just ATTiny85 specific now).
My most recent arduino project is threefold; anyone can take analog input and send it to a digital PWM output, but the real trick is getting quality audio out! Out of the box the Uno has a 10KHz sampling rate on the ADC pins and a 1KHz PWM frequency on pin 5 and 6, with half that rate on the others. After doing a lot of digging, reading of obscenely long datasheets, and scouring the web for hours I've been able to piece together and modify several chunks of code to increase the input and output speeds to over 64KHz!
The PWM output on Pin 5 and 6 is set to 64KHz carrier.
The ADC Clock is set to 1MHz, which yields a 77KHz sampling rate (that's better than your PC's inputs!).
The result:
Arduino Hi Fi Audio PWM - YouTube
Here's the code and schematic for anyone who wants to try (that's the whole idea behind arduino isn't it? standardized platform and software for easy sharing!)
Code:
/* Arduino Uno Audio PWM Modulator
by Matt "Sigurthr" Giordano. 1/9/2014.
Audio Input on Analog 0, PWM output on Digital 6.
I/O circuitry as follows:
Input: capacitor couple iPod audio through 4.7uF.
Bias Input pin to 2.5V with one 10k resistor to 5v,
and another 10k resistor to ground to form a voltage divider.
Output goes to a RC filter made of a series 560 ohm resistor
and a parallel 0.1uF capacitor.
Capacitor couple this node to amplifier of choice.
*/
int duty;
int raw;
int bias = 192;
void setup(){
TCCR0B &= B11111000;
TCCR0B |= B00000001;
// PWM Boost pins 5 & 6 to 64KHz
// ADC Boost Start - sets ADC clock to 1MHz
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
// set ADC prescaler to 16
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
cbi(ADCSRA,ADPS0) ;
// ADC Boost End
}
void loop(){
raw = analogRead (0);
duty = bias + ((raw - 512)/4);
analogWrite(6, duty);
}