.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _3.1.3_py: 3.1.3 Audio Module ===================== Introduction ----------------- In this project, let's make a DIY stereo with an audio power amplifier module, 8ohm/2w speakers and a 3.5mm Audio cable. Required Components ------------------------------ In this project, we need the following components. .. image:: ../img/audio2.png :width: 700 :align: center It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_gpio_extension_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_audio_speaker` - \- Experimental Procedures ------------------------------ **Step 1:** Build the circuit. .. image:: ../img/4.1.4fritzing.png :width: 800 :align: center After building the circuit according to the above diagram, then plug the audio cable into the Raspberry Pi’s 3.5mm audio jack. .. image:: ../img/audio4.png :width: 400 :align: center **Step 2:** Get into the folder of the code. .. raw:: html .. code-block:: cd ~/raphael-kit/python/ **Step 3:** Run. .. raw:: html .. code-block:: python3 3.1.3_AudioModule.py After the code runs, you can enjoy the music. .. note:: If your speaker have no sound, it may be because the Raspberry Pi has selected the wrong audio output (The default is HDMI), you need to :ref:`change_audio_output` to Headphones. If you feel that the volume of the speakers is too low, you can :ref:`adjust_volume`. **Code** .. note:: You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect. .. raw:: html .. code-block:: python from pygame import mixer import os user = os.getlogin() user_home = os.path.expanduser(f'~{user}') mixer.init() def main(): mixer.music.load(f'{user_home}/raphael-kit/music/my_music.mp3') mixer.music.set_volume(0.7) mixer.music.play() while True: pass# Don't do anything. def destroy(): mixer.music.stop() if __name__ == '__main__': try: main() except KeyboardInterrupt: destroy() **Code Explanation** .. code-block:: python from pygame import mixer mixer.init() Import the ``mixer`` method in the ``pygame`` library and initialize the method. .. code-block:: python mixer.music.load(f'{user_home}/raphael-kit/music/my_music.mp3') mixer.music.set_volume(0.7) mixer.music.play() This code reads the ``my_music.mp3`` file in the ``~/raphael-kit/music`` directory and sets the volume to 0.7(The range is 0~1). The Raspberry Pi will start playing audio When ``mixer.music.play()`` is called. .. note:: You can also upload other music files to your Raspberry Pi. For a detailed tutorial, please refer to: :ref:`filezilla` .. code-block:: python mixer.music.stop() Calling ``mixer.music.stop()`` will stop playing audio. In addition, you can also pause with ``mixer.music.pause()`` and continue with ``mixer.music.unpause()``. **Phenomenon Picture** ------------------------ .. image:: ../img/3.1.3audio.JPG :align: center