Plotting audio analog signal in Matlab using Arduino | applied electronics engineering

Latest

Plotting audio analog signal in Matlab using Arduino

By Applied Electronics - Wednesday, February 1, 2017 No Comments
Here we show how to plot audio signal voltages in matlab using Arduino. As you know Arduino has inbuilt ADC at the analog pins. This means that by connecting analog signal source to one of the six analog pins we can get the analog voltages in digital values. This means that we can connect audio signal source such as music or real voice to the analog pin and plot it. To plot the audio signal we use Matlab. Last time we told you how to plot analog signal in Matlab using Arduino in real time where we used potentiometer. This time we plot audio signal.

Headphone Hack

For this tutorial we need old headphone jack. We play audio or music and transmit that signal using headphone jack to the arduino analog pin. There are different types of headphone jacks with different number of wires but they all will work. In this tutorial, we used a old headphone jack which has 3 wires. They were soldered to three jumper wires as shown in the figure below.


Two of the wires are signal wires for left and right headphone and the third is the ground wire. By connecting the either the left or the right headphone soldered jumper wire to one of the analog pin and the ground wire to the ground of the Arduino we can get the analog signal into Arduino.

Headphone, Arduino Hardware connection

 Connection of the headphone wires in Arduino via breadboard is shown below.

connecting headphone wires to arduino for Plotting audio analog signal in Matlab using Arduino




The yellow wire is the signal wire where audio signal flows. This is connected to the black colored wire which is in turn connected to analog pin A2. The blue wire is ground and is connected to the GND pin of Arduino. The red wire is the another signal wire.


Once the hardware connection is made, we can use Matlab to read the digital values of the converted analog signal voltages. These digitally converted signal values can be plotted using Matlab and we can view the audio signal in real time.

The following picture shows the audio signal plot in real time by playing music in PC.

Plotting audio analog signal in Matlab using Arduino

Matlab Program Code

The matlab program code to plot the audio signal in real time is given below.


clear all
close all

a = arduino();

tic;  % starts a stopwatch timer to measure performance, 
      % it internally records starting time
      % toc read elapsed time from stopwatch
      
k = 0;

while (toc < 300) 
    k = k+1;
    
    t(k) = toc;
    v(k) = readVoltage(a,'A2');
    
    figure(1)
    plot(t,v);
    axis([0,inf,0,1]);
    xlabel('time')
    ylabel('voltage')
    grid on
    pause(0.1);
    
end

In the above code, mainly we have used the tic and toc feature of matlab. Basically they are functions that tiggers an internal watchdog timer in matlab. tic starts the timer and toc reads the time elapsed. This feature is used to set the time for plotting the audio signal. To read the analog signal from the analog pin A2 we have used readVoltage() function.

To learn the basic of using Matlab with Arduino see Programming Arduino using Matlab.

Video Demonstration of Plotting audio analog signal in Matlab using Arduino


The following video shows the connections of headphones wires with Arduino and breadboard. It also shows the real time graph plot of the audio signal.


So in this way you can plot audio signal in real time in Matlab using Arduino utilizing its internal ADC.

If you like this tutorial share it and subscribe to our blog.

No Comment to " Plotting audio analog signal in Matlab using Arduino "