element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Audio Output with Pi, Simplified!
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: shabaz
  • Date Created: 3 Oct 2023 6:26 PM Date Created
  • Views 15252 views
  • Likes 12 likes
  • Comments 3 comments
  • audio
  • pi
  • rpibeginner
  • WAV
  • raspberry_pi
  • raspberry pi
  • sound
  • i2s
  • mp3
  • raspberry_pi_projects
Related
Recommended

Audio Output with Pi, Simplified!

shabaz
shabaz
3 Oct 2023
Audio Output with Pi, Simplified!

Table of Contents

  • Introduction
  • What is Needed?
  • Connections
  • High Power Audio Output
  • Configuration
  • Using It
  • Summary

Introduction

It’s easy to get reasonable quality sound (about as good as an early generation iPod) from a Pi. This ultra-short blog post shows how to do it at a low cost, and with no messing about with seeking out scripts or drivers.

I tested it on a Pi 4, but it should work on most modern Pi boards.

Here's a 30-second demo:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

What is Needed?

The Google search term is i2s board. I2S is a digital protocol that runs over three wires (it can be more wires, but the types of boards that will be discussed here use three), plus the supply connections (GND and 5V often). The I2S board converts from I2S to analog audio. I2S works both ways; you can also get audio input boards, but this blog solely focuses on audio output.

Almost any I2S-capable board will be OK. I2S boards exist with line-level audio output or with a built-in power amplifier for directly connecting speakers. For this blog post, I’m using an I2S board intended for driving a single speaker (mono), but the connections to the Pi, and the configuration, are identical regardless of what board is used, whether it is stereo or not.

The board used in this demo is called MAX98357 I2S Board.

You’ll also need jumper wires and a speaker or two, depending on whether the board contains a mono or stereo speaker output. If you’re using a board with just line output, then theoretically, you could use it with headphones, although that’s not normally advised. If you use a board containing a PCM5102 chip, for example, then the line-level output is quite powerful and will work with earphones/headphones at a pinch.

Connections

There are five connections required usually. Three connections provide the I2S digital signals transmitted from the Pi to the audio board, and two connections carry the power.

The single diagram below is all you need; it shows the five connections from the Pi to the example audio board used in this blog. The pin numberings are labeled; refer to the diagram to see where the pins are on the Pi.

image

There's another view of the pin detail on the pi in the diagram below:

image

Here is what it looks like all wired up:

image

High Power Audio Output

Note: Some I2S boards may consume a lot of power, in which case it’s not advisable to power them from the Pi. Such high-power boards should be provided with a separate power supply (the GND connection is still needed to the Pi since it acts as a return for the I2S digital signals).

Configuration

There is not much to do. As a super-user (e.g. using sudo), edit the file called /boot/config.txt as shown highlighted in the screenshot below.

Specifically, you're making three changes:

(1) Uncomment (i.e. remove the #) the line dtparam=i2s=on 

(2) Comment (i.e. add the #) to the line dtparam=audio=on

(3) Add the line dtoverlay=hifiberry-dacplus

All of the configuration is shown completed in the screenshot below, in red. Once you’ve done the changes, save the file, and reboot! All is done.

image

Using It

First, some tests. If you type at the terminal prompt aplay -L you should see the following:

image

If you type aplay -l (i.e. lower-case L), then the following should appear:

image

Transfer or download a .wav file and a .mp3 file onto your Pi. Both will be used because some examples require WAV format, while others also work with MP3 files. If the WAV examples do not work, then MP3 won't work either. It is best to try the simpler WAV format first if you're troubleshooting.

Here is a WAV file example: type aplay mywavfilename.wav and sound should come out of the speaker!

image

Here’s a MP3 file example; for this example, I installed the mplayer application first, by typing the following:

sudo apt-get install player

The command line syntax for mplayer is: mplayer -ao alsa mymp3filename.mp3 and if you type that, you’ll see the following appear, along with the sound output from the speaker:

image

Here is a Python example. Type the following into a file called (say) mysoundprog.py:

import time
import pygame
pygame.init()
pygame.mixer.music.load('v1.mp3')
pygame.mixer.music.play()
time.sleep(15)
pygame.mixer.music.stop()

Run it by typing python mysoundprog.py and you should hear the file playing!

image



Summary

It is pretty easy to get fairly decent sound out of a Pi. Typically, just five jumper wire connections are needed to popular I2S boards. The Pi emits digital signals that are converted into analog by the I2S board. Some boards contain a built-in amplifier for speakers.

In terms of configuration, a single configuration file needs to be edited on the Pi. 

The example in this blog was tested using the Raspberry Pi OS (desktop version, without the extra applications) and with a Raspberry Pi model 4B, but the information here should work on any other modern Pi, including the upcoming Pi 5.

If you have difficulties, please let me know (if I do not know the answer, others for sure will), and don't forget to supply a copy-paste or screenshot of the troubleshooting test commands and output mentioned in the blog post. If you have any suggestions, then it would be great to hear them. 

Thanks for reading!

  • Sign in to reply
  • shabaz
    shabaz over 1 year ago in reply to genebren

    Hi Gene,

    Thanks! I like that it has become very normal to see I2S interface on a lot of microcontrollers nowadays too. So much nicer to have decent sound, even for tones/announcements, than the old-fashioned square-waves/buzzers! It's also nice that a lot of baby toys these days have better sounding music! I purchased one for a baby neice recently, and it's actually enjoyable even for adults (although the parents may eventually disagree!).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 1 year ago

    Great blog!  I have always been interested in the I2S intereface.  This seems like a great protocol for audio.  While I have designed boards that use I2S, they were always projects where I only designed the hardware and none of the software, so I have never delved too deeply into specification and coding.  This is an area that remains on my list of things I should look into.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 1 year ago

    I like it.

    I will keep this solution in mind the next time I need to make sound with one of my RPi's.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube