Playing Arduino Stored Audio with MOSFET Amplifier

0
43

 Arduino is a popular open-source electronics platform that is widely used for various DIY projects. One of the things that you can do with Arduino is to play sound. To play sound with Arduino, you need a speaker and an amplifier. The speaker is the device that converts electrical signals into sound, while the amplifier is used to boost the electrical signal so that it is strong enough to drive the speaker.

In this blog post, we will show you how to play sound with Arduino and a MOSFET amplifier. A MOSFET (Metal-Oxide-Semiconductor Field-Effect Transistor) amplifier is an electronic device that is used to amplify signals. It works by controlling the flow of current through a channel between the source and drain. The voltage applied to the gate terminal determines the resistance of the channel, which in turn affects the current flow. In this arduino audio tutorial we will use IRF540N N-channel  enhancement MOSFET(E-MOSFET). The MOSFET is used as an amplifier hence it must be properly biased. Here the MOSFET amplifier is biased in saturation region using E-MOSFET drain feedback biasing method.

Playing Sound with Arduino and MOSFET Amplifier

Required Parts

Before we start, you will need to gather the following components:

  • Arduino Board (e.g. Arduino Uno)
  • MOSFET (e.g. IRF540N)
  • Speaker
  • Capacitor (10uF)
  • Resistor (10 Ohms)
  • Breadboard
  • Jumper Wires

Building the audio circuit

Once you have all the components, we can start building the circuit. Connect the components as shown in the following diagram:Playing Sound with Arduino and MOSFET Amplifier circuit diagram

 The circuit diagram shows a sound amplification circuit using an Arduino microcontroller and a MOSFET amplifier. The components in the circuit include an Arduino board, a N-channel MOSFET (IRF540), a 10KOhm potentiometer, a 4Ohm speaker, and a few resistors and capacitors.

The Arduino board generates a audio PWM wave signal, which is used to drive the MOSFET. The potentiometer is used to adjust the volume of the sound produced by the speaker. The MOSFET acts as a switch to amplify the square wave signal and drive the speaker. The resistors and capacitors help to filter the signal and protect the MOSFET from damage.

Here’s how to connect the components:

  1. Connect the E-MOSFET source terminal to the ground rail on the breadboard.
  2. Connect the E-MOSFET drain terminal with 18Ohm resistor to the positive rail on the breadboard.
  3. Connect the digital pin 9 of the Arduino board to the 2nd order low pass filter(0.1uF and 3.3KOhm).
  4. Connect the LPF output to the coupling capacitor volume control potentiometer.
  5. Connect output of the volume control potentiometer to the coupling capacitor 1uF.
  6. Connect the output from the coupling capacitor to the gate terminal of the MOSFET.
  7. Connect one terminal of the speaker to the drain terminal of the MOSFET via output coupling capacitor.
  8. Connect the other terminal of the speaker to the ground rail on the breadboard.

Now that the circuit is built, we can write the code to play sound using the Arduino. 

Arduino Playing Sound Code

Here’s the code for playing Jingle Bell sound with Arduino:

const int spkPin = 9;

// We'll set up an array with the notes we want to play
// change these values to make different songs!

// Length must equal the total number of notes and spaces 

const int songLength = 31;

// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)

char notes[] = "eee eee egcde fff ffee eeggfdc " ; // a space represents a rest

// Beats is an array of values for each note and rest.
// A "1" represents a quarter-note, 2 a half-note, etc.
// Don't forget that the rests (spaces) need a length as well.

int beats[] = {2,2,3,1, 2,2,3,1, 2,2,3,1,4,4, 2,2,3,0, 1,2,2,2,0, 1,1,2,2,2,2,4,4};

// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.

int tempo = 200;


void setup() 
{
  pinMode(spkPin, OUTPUT);
  pinMode(13, OUTPUT);

}


void loop() 
{
  int i, b, duration;

  
  for (i = 0; i < songLength; i++) // step through the song arrays
  {
    duration = beats[i] * tempo;  // length of note/rest in ms
    
    if (notes[i] == ' ')          // is this a rest? 
    {
      delay(duration);            // then pause for a moment
    }
    else                          // otherwise, play the note
    {
      tone(spkPin, frequency(notes[i]), duration);
      digitalWrite(13, HIGH);
      delay(duration);            // wait for tone to finish
    }
    digitalWrite(13, LOW);
    delay(tempo/10);              // brief pause between notes

  }
  
  // We only want to play the song once, so we'll pause forever:
  //while(true){}
  // If you'd like your song to play over and over,
  // remove the above statement
}


int frequency(char note) 
{
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.
  
  int i;
  const int numNotes = 8;  // number of notes we're storing
  
  // The following arrays hold the note characters and their
  // corresponding frequencies. The last "C" note is uppercase
  // to separate it from the first lowercase "c". If you want to
  // add more notes, you'll need to use unique characters.

  // For the "char" (character) type, we put single characters
  // in single quotes.

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  
  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.
  
  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.
}

This is an Arduino code that plays the Jingle bells musical tone tune using a speaker on pin 9. The code sets up arrays for the Jingle bells musical notes and their corresponding duration in beats, as well as a tempo for the song.

The setup() function sets the speaker pin (9) as an output and the pin 13 as an output. The loop() function steps through the notes and beats arrays and plays each note for the duration specified. If the note is a space, a rest is played by using a delay() function for the duration of the rest. If the note is not a space, the tone() function is used to play the note for the duration specified.

The frequency() function takes a note character (c, d, e, f, g, a, b, or C) and returns the corresponding frequency in Hz for the tone() function to use. The function uses a for loop to search for the note character in an array of notes and frequencies. If the note is found, its frequency is returned. If the note is not found, the function returns 0.

After playing the entire song, the code is designed to pause forever. If you want the song to play over and over, you would need to remove the line while(true){} or add a looping mechanism.

Video Demonstration

The following video demonstrates how the sound generated by Arduino, amplified by the E-MOSFET Amplifier(IRF540N) sound likes.

If you are interested in audio electronics with Arduino then also see Arduino Metronome tutorial. In that tutorial we have used push button to turn on and off the music which can be incorporated in this audio tutorial. Also in this tutorial we have used tone() function which uses PWM(Pulse Width Modulation) to output audio. We can also use DAC(Digital to Analog Converter) to play audio as illustrated in the tutorial Audio from Arduino using R2R DAC and transistor amplifier.