Introduction: Play Songs With Arduino Using ADC to PWM on Flyback Transformer or Speaker
Hello Guys,
This is the second part of my another instructable(that was much difficult), Basically, In this Project, I have used the ADC and TIMERS on my Arduino to convert Audio Signal into a PWM Signal.
This is much easier than my previous Instructable, Here is the link of my first Instructable if you want to see. link
To understand the theory of Audio signal, Bitrate, Bit depth, Sampling rate, You can read the theory in my last tutorial on Instructable. The link is above.
Step 1: Things We Need for This Project(Requirements)
1. Arduino Board(we can use any Board (328,2560) i.e Mega, Uno, Mini, etc but with specific different pins)
2. PC with Arduino Studio.
3. Breadboard or Perfboard
4. Connecting Wires
5. TC4420 ( Mosfet driver or such thing)
6. Power Mosfet(N or P channel, please wire then accordingly ) ( I have used N-channel )
7. Speaker or Flyback Transformer(Yes you read it right!!)
8. Suitable Power Supply (0-12V) ( I have used my own ATX Power Supply )
9. Heat Sink (I have salvaged from my old PC).
10. An Amplifier(normal Music Amplifier) or Amplifier Circuit.
Step 2: Theory of ADC to PWM
So in this Project, I have used in built ADC of Arduino to do data sampling of an Audio Signal.
ADC(Analog-To-Digital Converter) as name defines, ADC converts Analog signal into Digital samples. And for Arduino with a maximum of 10-bit depth. But for this Project, we'll use 8-bit Sampling.
While using the ADC of Arduino, we have to keep in mind the ADC_reference Voltage.
Arduino Uno offers 1.1V, 5V(Internal reference, which can be set defining in code) or an external reference(which we have to apply externally to AREF pin).
According to my experience, a minimum of 2.0V should be used as a reference voltage to get a good result from ADC. As 1.1V didn't go well at least for me. (Personal Experience)
*IMPORTANT* *IMPORTANT**IMPORTANT**IMPORTANT**IMPORTANT*
We need to use an amplified audio signal from an Amplifier or an Amplifier Circuit with a peak voltage(Max. Voltage) of 5V.
Because I did set the internal Voltage Reference of 5V, for our Project. And I am using an amplified Signal using a normal Amplifier(Music Amplifier), which is mostly available in our household or you can build one for yourself.
So now the main part. Sampling Rate, which is how much samples our ADC takes per second, more is the conversion rate, better will be output result, more similar will be output wave as compared to input.
So, we will be using a sampling rate of 33.33Khz in this Project, by setting the ADC clock at 500Khz.To understand how it is so, we have to see the ADC Timing Page in the datasheet of Atmega(328p) chip.
We can see that, we need 13.5 ADC clock cycles to complete one sample with auto sampling. With a frequency of 500Khz, it means 1/500Khz=2uS for one ADC cycle, which means 13.5*2uS=27uS are needed to complete a sample when auto sampling is used. By giving 3uS more to the Microcontroller(for safe side), Making a total of 30uS totally for one sample.
So 1 Sample at 30uS means 1/30uS=33.33 KSamples/S.
To set the Sampling rate, which is dependent on TIMER0 of Arduino, because ADC auto sampling trigger is dependent on that in our case, as you can also see in the code and datasheet, we have made the value of OCR0A=60(Why so???)
Because according to the formula given in the datasheet.
frequency(or here Sample Rate)=Clock frequency of Arduino/Prescaler*Value of OCR0A (in our case)
Frequency or Sample rate we want=33.33KHz
Clock frequency=16MHz
Prescaler value=8(in our case)
Value of OCR0A=we wants to find??
which simply gives OCR0A=60, also in our Arduino code.
TIMER1 is used for carrier wave of an audio signal, And I'll not go in so many details of that.
So, that was the short theory of the concept of ADC to PWM with Arduino.
Step 3: Schematic
Connect all the Components as shown in the schematic. So you have here two options:-
1. Connect a Speaker( Connected with 5V)
2. Connect a Flyback Transformer (Connected with 12V)
I have tried both. And both work pretty well.
*IMPORTANT* *IMPORTANT**IMPORTANT**IMPORTANT**IMPORTANT*
We need to use an amplified audio signal from an Amplifier or an Amplifier Circuit with a peak voltage(Max. Voltage) of 5V.
Disclaimer:-
*I recommend using Flyback Transformer with Precaution as it can be dangerous because it produces High Voltages. And I'll be not liable to any damage.*
Step 4: Final Test
So upload the given code to your Arduino, And connect the Amplified Signal to A0 pin.
And don't forget to connect all ground pins to a common ground.
And Just Enjoy listening to music.