Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Photon Audio Sample 3

Sample program for streaming audio from a Particle Photon to a node.js server over TCP using DMA!

Prototype Image

This simple project uses an [Adafruit 1713] ( Electret Microphone Amplifier - MAX9814 with Auto Gain Control to capture audio on the Photon, then sends the data via TCP to a server.

I used a local server running node.js to receive and process the files in this case, but the data rate is only 64K bytes/second so it should work over the Internet, as well.

Because of the efficiency of having the processor do all of the work using hardware timers and DMA, it easily runs sampling at 32000 Hz with 16-bit samples!

Photon Side

The connections from the microphone board are:

  • AR - No connection
  • Out - Audio out (analog) to Photon A0
  • Gain - No connection
  • VDD - 3V3
  • GND - GND

Then you just need to flash the [audio3.cpp] ( file to your Photon. Make sure you edit the IP address of your server!

The code has many comments and should be self-explanatory.

I should warn you that this code is quite experimental as I only just learned how to do this crazy DMA audio stuff on the Photon. There might be better ways to do this, but it does work and might be a good starting point if you want to do this.

I found [this code example] ( on the Internet and it was invaluable.

The [STM32 standard peripheral library] ( documentation for the STM32F2xx is also quite helpful.

The idea is that instead of having a timer that kicks off for every sample, you just set up the ADC, Timer and DMA once, and tell the hardware to go to town. The hardware grabs the samples continuously without using the main processor.

From the main loop we poll to see if there's a buffer ready to be transmitted, do some light processing of the data and send it off. The good part is that this is not particularly timing-sensitive.

Server Side

The server-side code is written in [node.js] ( You'll want to copy the [audioserver.js] ( file to your computer.

It requires the standard Node 4.5.0 features plus the [wav package] ( to output the audio files. You install it using:

npm install wav

Then you run the server:

node audioserver.js

A directory called out will be created in the same directory and will contain audio files like 00001.wav, 00002.wav, etc.. You should be able to play these on your computer.

Using It

With the code loaded on the Photon and the server running, hit the SETUP button to being recording. The blue D7 LED turns on after a connection is made to the server. Recording stops when you hit the SETUP button again, or the maximum recording time is reached. That's currently 30 seconds, but there's really nothing on either side that requires that limit. It could be hours if you wanted it to be.