Raspberry Pi Pico: MP3-Dateien abspielen (mit CircuitPython) (2024)

Ein Raspberry Pi Pico hat leider keine GPIOs, die sich als analoge Ausgänge konfigurieren lassen. Man kann also nicht einfach so Töne oder sogar Audio-Dateien abspielen. Allerdings gibt es einen Umweg über die Ausgabe von PWM-Signalen. Darüber ist es möglich, mit ganz einfachen Mitteln und spielend leicht MP3-Dateien mit dem Raspberry Pi Pico abzuspielen. Dafür eignet sich schon ein kleiner Lautsprecher (Mono) vom Typ KY-006. Für ernsthafte Anwendungen und Experimente wäre ein richtiger Lautsprecher mit z. B. 1 Watt / 8 Ohm und einem zusätzlichen Verstärker-Modul zu empfehlen.

Der hier verwendete Programmcode benötigt zum Abspielen der MP3-Dateien die CircuitPython-Firmware, weil dort bereits ein MP3-Decoder enthalten ist. Mit MicroPython funktioniert der verwendete Programmcode nicht.

Die Kombination aus Raspberry Pi Pico und CircuitPython ermöglicht in nur wenigen Schritten kreative Sound-Projekte. Nur eine Sache muss man dabei beachten, dass der Speicher des Raspberry Pi Pico nicht besonders groß ist und nur kleine MP3-Dateien oder ganz wenige auf dem Pico gespeichert werden können.

Aufbau und Bauteile

Raspberry Pi Pico: MP3-Dateien abspielen (mit CircuitPython) (1)

  • KY-006 - Lautsprecher-Modul
  • GPIO-Belegung (Pinout)
Raspberry Pi PicoKY-006
Pin 3GND-
Pin 1GPIO 0S

Hinweis: Der KY-006 ist nicht wirklich für die Tonausgabe von MP3-Dateien geeignet. Er rauscht, knackt, knarzt und schnarrt, je nach dem was genau abgespielt wird. Zum Testen ist das sicherlich ausreichend. Ernsthafte Anwendungen benötigen einen Verstärker und einen richtigen, zum Verstärker und zur Anwendung passenden Lautsprecher.

Schnell und einfach alle Bauteile zusammen bestellen

  • Jetzt alle Bauteile bestellen
  • Eigenes Elektronik-Set konfigurieren

MP3-Dateien zum Abspielen

Grundsätzlich eignen sich fast alle MP3-Dateien. Übliche MP3-Dateien mit Mono und Stereo, bis 64 kBit/s und 8 kHz bis 24 kHz werden unterstützt.
Um sicher zu sein, abspielfähige Dateien zu verwenden, bieten wir hierzu ein paar geeignete Dateien zum Download an, die direkt mit einem Dateimanager auf den Pico kopiert werden können.

  • MP3-Dateien

Programmcode für CircuitPython: Eine einzelne MP3-Datei abspielen

Wenn es darum geht, eine einzelne Datei abzuspielen, dann reichen folgende Zeilen aus.

  • Zuerst werden die erforderlichen Bibliotheken geladen.
  • Dann wird der GPIO 0 (Pin 1) als Ausgang für ein PWM-Audio-Signal initialisiert.
  • Anschließend wird die Datei für den MP3-Decoder ausgewählt.
  • Und danach wird die Datei abgespielt.
  • Die Endlosschleife dient hier dazu, das Ende des Abspielens zu erkennen. Alternativ bestünde die Möglichkeit den Pico während des Abspielens etwas anderes machen zu lassen.

Nach dem Ausführen des Programmcodes wird die Datei (im Verzeichnis mp3) sofort abgespielt.

# Bibliotheken ladenimport boardfrom audiomp3 import MP3Decoderfrom audiopwmio import PWMAudioOut# Initialisierung: GPIO 0 (Pin 1) als PWM-Audio-Ausgangaudio_out = PWMAudioOut(board.GP0)# MP3-Datei auswählenmp3 = MP3Decoder('mp3/pico.mp3')# MP3 abspielenprint('Abspielen')audio_out.play(mp3)# Ausführung während dem Abspielenwhile audio_out.playing: passprint('Ende')

Hinweis: Wenn Du keinen Ton hörst, dann prüfe bitte, ob der Lautsprecher mit den richtigen Pins verbunden ist, die im Programmcode stehen. Desweiteren solltest Du berücksichtigen, dass die Lautstärke sehr leise ist und man nur dann etwas hört, wenn man sich den Lautsprecher direkt ans Ohr hält. Laute Umgebungsgeräusche können diese Tonausgabe sehr leicht übertönen.

Programmcode für CircuitPython: Mehrere MP3-Dateien hintereinander abspielen

Wenn man mehrere Dateien hintereinander abspielen will, muss der Programmcode um eine Schleife und eine Liste mit den Dateinamen ergänzt werden.

# Bibliotheken ladenimport boardimport audiomp3import audiopwmio# Initialisierung: GPIO 0 (Pin 1) als PWM-Audio-Ausgangaudio_out = audiopwmio.PWMAudioOut(board.GP0)# MP3-Dateienmp3dir = 'mp3/'mp3files = ['pico.mp3','restaurant.mp3']# Wiederholungfor filename in mp3files: print('Abspielen:', filename) # Datei auswählen mp3 = audiomp3.MP3Decoder(open(mp3dir + filename, 'rb')) # Abspielen audio_out.play(mp3) # Ausführung während dem Abspielen while audio_out.playing: passprint('Ende')

Ergänzungen

Wer den Aufbau durchführt und den Programmcode ausprobiert, wird zwei Dinge bemängeln.

  1. Die Audio-Ausgabe ist sehr leise: Das liegt daran, dass der Strom, den ein GPIO-Ausgang liefern kann, nicht besonders groß ist. Deshalb muss der das Ausgangssignal (Strom) zur Ausgabe auf einem Lautsprecher verstärkt werden.
  2. Am Anfang und am Ende des Abspielen knackt es manchmal: Letzten Endes ist diese Art der Tonverarbeitung nur eine Notlösung, deren auditive Wahrnehmung nicht gefallen muss.

PWM-Signal verstärken

Um die Audio-Ausgabe sinnvoll nutzen zu können, muss das PWM-Signal (Strom) verstärkt werden. Man muss also den GPIO mit einem Verstärker (Eingang) verbinden und am Verstärker (Ausgang) den Lautsprecher anschließen. Hierfür eignen sich verschiedene Audio-Verstärker-Module.

  • Raspberry Pi Pico: Audio-Verstärker-Module

Weitere verwandte Themen:

  • Raspberry Pi Pico: Tonausgabe mit Buzzer KY-006
  • Raspberry Pi Pico: MP3-Audioausgabe mit DFPlayer Mini
  • Raspberry Pi Pico: Summer einschalten und ausschalten
  • Raspberry Pi Pico: MP3, Musik, Sprache und Audio

Frag Elektronik-Kompendium.de

Hardware-nahes Programmieren mit dem Raspberry Pi Pico und MicroPython

Das Elektronik-Set Pico Edition ist ein Bauteile-Sortiment mit Anleitung zum Experimentieren und Programmieren mit MicroPython.

  • LED: Einschalten, ausschalten, blinken und Helligkeit steuern
  • Taster: Entprellen und Zustände anzeigen
  • LED mit Taster einschalten und ausschalten
  • Ampel- und Lauflicht-Steuerung
  • Elektronischer Würfel
  • Eigene Steuerungen programmieren

Elektronik-Set jetzt bestellen Online-Workshop buchen

Online-Workshop: Programmieren mit dem Raspberry Pi Pico

Gemeinsam mit anderen und unter Anleitung experimentieren? Wir bieten unterschiedliche Online-Workshops zum Raspberry Pi Pico und MicroPython an. Einführung in die Programmierung, Sensoren programmieren und kalibrieren, sowie Internet of Things und Smart Home über WLAN und MQTT.

Online-Workshop buchen

Besuchen Sie unser fast monatlich stattfindendes Online-Meeting PicoTalk und lernen Sie uns kennen. Die Teilnahme ist kostenfrei.

Termine und Newsletter-Anmeldung

Elektronik-Sets für das Hardware-nahe Programmieren

Pico Edition

Pico WLAN Edition

Sensor Edition

Eingabe Ausgabe Edition

Raspberry Pi Pico: MP3-Dateien abspielen (mit CircuitPython) (2024)

FAQs

Can you use CircuitPython on Raspberry Pi Pico? ›

In order to open up the Pico as a drive, you need to hold down the BOOTSEL key while plugging in the Pico USB cable. You'll then be prompted about a new drive on your machine called “RPI-RP2”, this is the Pico “drive” that we will require to install CircuitPython.

Is Raspberry Pi Pico worth it? ›

RPI Pico's Strengths

It is much easier to do simple tasks on the internet using MicroPython on a Raspberry Pi Pico than it would be on an Arduino. For very simple projects the Raspberry Pi Pico is great. It is much easier for people who are new to programming to write code for a Raspberry Pi Pico than an Arduino.

How to program Raspberry Pi Pico using Python? ›

Programming a Raspberry Pi Pico
  1. Step 1: Connect to your computer. Connect your USB cord to the USB port on the device. ...
  2. Step 2: Install MicroPython on the Pico. Copy the MicroPython file you downloaded. ...
  3. Step 3: Set the interpreter for Thonny. Open the Thonny software. ...
  4. Step 4: Write your program. ...
  5. Step 5: Run program.

Can Raspberry Pi Pico play MP3? ›

You'll need to save your MP3s in the correct format–a bit rate of less than 64kbit/s and sample rate of between 8kHz and 24kHz. Drag your files onto the Pico's Flash memory, and change the names in the code to match your clips.

What is the difference between MicroPython and CircuitPython? ›

In Micro Python, you can have different files running at the same time and sharing the same state. Where in Circuit Python there's no sharing of States, so when one thing's running it's the only thing running and that can make it a lot easier for someone new to understand what's happening if something goes wrong.

What language is best for Raspberry Pi Pico? ›

  • Python. Python takes the crown as the most widely used with Raspberry Pi programming language. ...
  • Scratch. ...
  • C. ...
  • C++ ...
  • HTML/CSS. ...
  • Java. ...
  • JavaScript. ...
  • jQuery.
Aug 17, 2023

Is Raspberry Pi Pico faster than Arduino? ›

When it comes to processing power, the Raspberry Pi Pico boasts a dual-core ARM Cortex-M0+ processor running at 133 MHz, making it a formidable contender in the microcontroller arena. On the other hand, Arduino boards vary in terms of processing capabilities, with some featuring simpler architectures than others.

Is Raspberry Pi Pico good for image processing? ›

The RPi Pico uses an RP2040. RP2040 is a dual-core ARM Cortex-M0+. It comes with "264kB on-chip SRAM". You shouldn't expect this to have any power that's useful for image processing.

Can I run Python on Raspberry Pico? ›

MicroPython is a full implementation of the Python 3 programming language that runs directly on embedded hardware like Raspberry Pi Pico. You get an interactive prompt (the REPL) to execute commands immediately via USB Serial, and a built-in filesystem.

Can Raspberry Pi Pico run machine learning? ›

It allows you to run machine learning models to do things like voice recognition, detect people in images, recognize gestures from an accelerometer, and other sensor analysis tasks. This version has scripts to upstream changes from the Google codebase.

What is the difference between Python and MicroPython? ›

The key difference between Python and MicroPython comes from the fact that they are designed for different purposes. Python is used to write code that runs on powerful processors, whilst MicroPython was designed to be compatible and power microcontrollers and microprocessors.

Can I run Windows on Raspberry Pi Pico? ›

Pico setup for Windows

It is inspired by, and is roughly equivalent to, the pico-setup project for Linux systems. The installer automates the prerequisite installation on Windows, as explained in the official Getting started with Raspberry Pi Pico guide.

Can Raspberry Pi Pico run C++? ›

Raspberry Pi Pico C/C++ SDK

Our official C SDK can be used from the command line, or from popular integrated development environments like Visual Studio Code, Eclipse, and CLion. To get started, download our C/C++ SDK and Examples, and take a look at our 'getting started' documentation to get going.

Can Raspberry Pi Pico run on battery? ›

The Raspberry Pi Pico requires a power supply capable of delivering a minimum of 1.8 volts and a maximum of 5.5V. A battery pack with a USB to micro-USB cable can also power a Raspberry Pi Pico. This battery pack provides up to 2.1A of current at 5V.

Can Raspberry Pi Pico run TensorFlow? ›

This is a version of the TensorFlow Lite Micro library for the Raspberry Pi Pico microcontroller. It allows you to run machine learning models to do things like voice recognition, detect people in images, recognize gestures from an accelerometer, and other sensor analysis tasks.

How to install CircuitPython on Raspberry Pi? ›

MakerSnack: Installing CircuitPython on a Raspberry Pi
  1. Step 1: Launch Terminal and Log Into Your Pi. ...
  2. Step 2: Install / Upgrade pip on your Pi. ...
  3. Step 3: Run the Adafruit Pi Installer Script. ...
  4. Step 4: Reboot When Asked. ...
  5. Step 5: Check on the I2C and SPI Configuration. ...
  6. Step 5: Run the Blinka Test Program.

Can Raspberry Pi Pico run FreeRTOS? ›

FreeRTOS Kernel allows us to add multi-processing to projects on the Raspberry PI Pico. This course teaches the foundations of FreeRTOS Kernel through practical example projects to get you quickly up and running.

References

Top Articles
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 6006

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.