Friday, 12 February 2016

Arduino Signal Experiment #1 Sine Wave Input to Arduino using Laptop Speaker

Most of us who would like to do actual experiments for FFT or Goertzel algorithms using Arduino will need a simple signal generator to be fed into the analog pins of Arduino. We could buy some cheap signal generators from Ebay or Aliexpress, but there is a cheaper alternative that is already in front of you.... your PC or Laptop's Speaker output. In this blog post, I will show a step-by-step guide on how to use this.

Our PC or Laptop's speaker output has a DC offset of 0V(due to AC coupling)  as seen on majority of audio designs. This means that the sine wave can reach down to negative voltage which is not an ideal input to Arduino since it will not be able to measure the negative voltages. To resolve this, the sine wave must have a DC offset to be able to read the positive and negative peak of the sine wave. Here is a simple circuit to do that:


On the simulation, we can see that the input V_speakerInput was offset to Vcc/2. On the actual circuit, the R4 must be removed since it was only placed here to act as a dummy load (some simulators have trouble simulating circuit without load). The R1 and R2 can range from 1K to 3K. The R3 can range from 100 to 470 ohms. Here is the actual circuit:


I am using the great Arduino Mega 2560 and I soldered the surface mount resistor and capacitor on a separate bare PCB board. As you notice, there is only 1 wire from the TRRS audio jack that I used. This is because I omitted the ground wire to avoid ground loop. I will just simply use the USB's ground as the main ground source of the circuit.

Next part is the software to generate the sine wave ----->  the great open source Audacity. This is a beautiful piece of software which I use most of the time for music recording/composition. I never expected  to use this someday on my engineering work as well. :) Going to back to our project, once you run Audacity, go to Generate menu > Tone  and you can see that:
  • You can generate Sine/Sawtooth/Squarewave
  • You can set the amplitude and time duration
  • You can even produce and mix different tones and play them all together!

For the Arduino, here is the code that I am using currently using to verify if Arduino can read the sine wave (with DC offset) http://codepad.org/infZ2OLV

To prove that it works, I generated a sine wave tone on Audacity at 400Hz. I captured it using Arduino and graphed it:


Voila! A sine wave with DC offset. :)

No comments:

Post a Comment