How to send audio signal directly from an arduino to a speaker
- Connect the black wire (ground) of your speaker to ground on the arduino.
- Connect the red wire (input) of your speaker to an amplification circuit - some info surrounding these are here for simply using a transistor and here for something a bit more sophisticated
- Connnect the input of the amplification circuit to pin 11 on your arduino.
- Download the PCM ardunio library as a zip (click clone or download, then Download ZIP).
- Unzip the file.
- Rename the resulting folder as simply PCM.
- If it doesn’t already exist, create a folder called “libraries” inside your Arduino sketchbook folder. (You can find the location of your sketchbook folder in the Arduino preferences dialog box.)
Move the PCM folder into the “libraries” folder in your Arduino sketchbook folder.
- Test your circuit works by using the example sketch (go to File > Examples > PCM, and open the playback example).
- Now we need to make your audio file small enough to fit into the arduino's memory. In audacity (this can be downloaded for free here), make sure your audio clip is less than 4 seconds, make your clip mono if it isn't already, change the format to 16 bit PCM, and change your project rate Hz to 8000. Then export as mp3.
- Now upload your clip below (where it says "Upload your sound file") and click copy text to copy your audio file in an arduino-readable format. You can look under the hood at the javascript code used to achieve this on my github.
- Paste the array of numbers into the example arduino sketch, replacing the array of numbers that was there originally.
- Upload your sketch to your arduino and it will now play your sound through your speaker.
- Now you can use that example sketch as a reference to use audio in your own sketches.
Articles that helped with this: high low tech "Simple Arduino audio Samples", Instructables "Playing a MP3 With Arduino Without Any Module"
Note that I've only tested that this works in the latest version of Firefox and Chrome (there appears to be a slight difference in how these browsers process sound as the cut off points for the start/end of the audio differ, but this isn't a big deal), so if you are using a different browser and it doesn't work properly then use one of those instead.