Main Content

Play High-Quality Audio from Raspberry Pi Using I2S-Based DAC

Inter-IC Sound Bus (I2S) is a communication protocol for transferring digital audio signal. Using an I2S-based digital-to-analog convertor (DAC), you can play high-quality audio from Raspberry Pi®.

Connect DAC HAT and Install Raspbian Image on Raspberry Pi

  1. Mount a DAC HAT on your Raspberry Pi.

  2. Connect a pair of speakers or headphones to the analog audio output of the DAC HAT.

  3. Configure your Raspberry Pi hardware as described in Hardware Setup.

    Note

    Ensure that you install the Raspbian image provided by MathWorks®.

Enable I2S Drivers on Raspberry Pi

  1. In the MATLAB® Command Window, type the listAudioDevices command. If this command lists your audio device, skip the steps in this section and directly perform the steps in Play Audio in Simulink.

  2. Open an SSH command-line session as described in Open Command-Line Session with Raspberry Pi Hardware.

  3. In the terminal, type sudo nano /boot/config.txt to edit the config.txt file.

    1. To enable the I2S interface in the Raspberry Pi device tree, uncomment the line dtparam=i2s=on in the hardware interface section of the file.

    2. To configure the HiFiBerry DAC, add dtoverlay=hifiberry-dacplus in the lirc-rpi module section of the file.

    3. To disable the onboard ALSA audio devices for Raspberry Pi, comment out the line dtparam=audio=on in the enable audio section.

  4. Disconnect Raspberry Pi from your computer and connect it again.

    Warning

    Ensure that the DAC HAT is still mounted on Raspberry Pi.

Play Audio in Simulink

  1. Configure your Simulink® model to run on Raspberry Pi as described in Model Configuration Simulink Support Package for Raspberry Pi Hardware.

  2. Add an ALSA Audio Playback block to the model.

  3. Open the ALSA Audio Playback block in your model. Specify the device number of the DAC HAT in the Device name parameter.

    If you do not know the device number of DAC, use the listAudioDevices function as described in List Available ALSA Audio Output Devices. The resulting structured array displays the device number of your DAC.

  4. On the Hardware tab of the toolstrip, in the Mode section, select Run on board, and then select Build, Deploy & Start. This action builds, downloads, and runs the model on the Raspberry Pi hardware. The audio starts playing.

See Also