Raspberry Pi Pico Audio output

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 มิ.ย. 2024
  • In this video I create a very basic and simple PWM based audio output circuit and take you through the code on how to output recorded WAV files.
    This is not intended to be the highest quality audio circuit. The main idea is to keep it simple.
    00:00 Introduction
    00:31 Circuit Diagram
    00:56 Building on a Breadboard
    01:18 Pico Code Explanation
    04:30 Code to covert WAV files to C arrays
    05:44 Demo of Audio Working!
    The circuit is derived from the 'Hardware Design Guide with RP2040' (which is the RPi Pico) datasheets.raspberrypi.org/rp...
    The source code from this video is shared on GitHub
    github.com/rgrosset/pico-pwm-...
    The blog I mention in the video from Greg Chadwick is here
    gregchadwick.co.uk/blog/playi...
    This video is not sponsored in any way.
    Resistors: 220 ohm, 100 ohm, 1.8K ohm
    Capacitors: 0.1uF, 47uF
    Audio jack breakout board
    www.amazon.ca/Breakout-Headse...
    I bought my Raspberry Pi Pico from BuyAPi.ca
    www.buyapi.ca/
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 62

  • @HighfireX
    @HighfireX 2 ปีที่แล้ว +2

    Thanks to your work, I'm learning quite a bit about how to get audio to work on a pico. I found directly plugging a speaker to Pin 28 (PWM) / Ground gave a larger boost in volume at the cost of the audio artifacts. I want to follow your circuit if I decide to line out / amplify the speaker.

  • @hellcoreproductions
    @hellcoreproductions 3 ปีที่แล้ว +2

    Nice, going to have a play and try some bytebeat streaming. That blog is gold.

  • @PATRIK67KALLBACK
    @PATRIK67KALLBACK 2 ปีที่แล้ว

    Cool project! Thank you for showing.

  • @mrneat-vi5hd
    @mrneat-vi5hd ปีที่แล้ว +1

    Amazing tutorial! Thank you for posting :-)

  • @WACkZerden
    @WACkZerden ปีที่แล้ว

    Thanks for sharing. great video. it sounds pretty cool too

  • @S-MKim
    @S-MKim 2 ปีที่แล้ว +1

    HI Robin, Amazing sound. Could you do it similarly for the sound input from a microphone, without using a dedicated sound card? Thanks in advance for your tip.

  • @dlese9641
    @dlese9641 3 ปีที่แล้ว

    great lesson ! works also under the Arduino IDE. thx

  • @mrlobbyist2061
    @mrlobbyist2061 2 ปีที่แล้ว

    Hey Robin, I'm working on implementing this code for continuous outdoor-garden-area usage. Is it fine to overclock the pico with a PIR sensor and your code running side by side at outside temps reaching 35 Celsius? Either way, is there a way to not overclock it and use it at 22Khz instead of 44Khz?

  • @plasmamac
    @plasmamac 3 ปีที่แล้ว

    immedealatly bought a pico ! thx for the explanation

    • @mmikse3
      @mmikse3 2 ปีที่แล้ว

      I'm sorry...

  • @kahnzo
    @kahnzo 2 ปีที่แล้ว

    Oh, man, this is cool!

  • @patrickworking3065
    @patrickworking3065 2 ปีที่แล้ว

    Wow, really interesting project. I am looking to borrow this design for a project I am currently developing. I'm curious what the buffer is doing in the original schematic and why you could just remove it and still get good results.

  • @sCealt
    @sCealt 3 ปีที่แล้ว

    awesome :)
    would love to know what would happen if you had a function that shifts and rearranges the content of header files :)

  • @cheapnoiseinthehouse5578
    @cheapnoiseinthehouse5578 2 ปีที่แล้ว

    Hi. I would like to create a sampling keyboard a little bit like the Casio SK-1. I would like to have at least 4 voices. The thing is I'm blind so coding the Pico will be impossible so I wondered if some one can help me with this.

  • @trichlingable
    @trichlingable ปีที่แล้ว

    Hey, thanks for the great tutorial. I try to output audio sampled from a microphone trhoug pwm, and i need to build the filter you added behind the pwm output. I am not really an expert in this, but are you sure about the 47uF capaditor? I can not even find one in ceramic and they are not included in the common sortiments. Is is it more like 4.7uF, becuase these semm to be much more common. Just try to buy the right things :-)

    • @takiktos1265
      @takiktos1265 3 หลายเดือนก่อน

      You can use electrolite capacitor. Just make sure polarity is good

  • @AbdullahKahramanPhD
    @AbdullahKahramanPhD 2 ปีที่แล้ว

    Could this be done via DMA?

  • @skellious
    @skellious 3 ปีที่แล้ว +1

    very nice :)

  • @Sam-tg4ii
    @Sam-tg4ii ปีที่แล้ว

    1:10 so pico has a built-in speaker? Where is the sound coming from exactly?

  • @AleksandrIvin
    @AleksandrIvin ปีที่แล้ว

    Cool project! How do you think, would this audio scheme with capacitors and resistors work for ATtiny chips?

    • @robingrosset6941
      @robingrosset6941  ปีที่แล้ว

      Yes I think it will. Attiny chips clock up to about 20MHz with tiny RAM 2K, 4K or 8k. You can certainly generate various wave forms and they support PWM, trouble will be getting more than a fraction of a second of sampled audio from RAM. Also single core so will need tight code and maybe an SD card? There are enough pins to have SD card and PWM audio.

    • @AleksandrIvin
      @AleksandrIvin ปีที่แล้ว

      @@robingrosset6941 Thank you. I tried to use PWM as simple beeper for my locomotive. It even works. But using another timer for IR receiving and detecting NEC contorol signals, something goes wrong with sound. I think to try using your way :)

    • @robingrosset6941
      @robingrosset6941  ปีที่แล้ว +1

      @@AleksandrIvin The IR receiver will be time sensitive and the PWM sound generation is too... so you would have to do something to slice time between them.

  • @BI1LWM
    @BI1LWM 2 ปีที่แล้ว

    will this work on raspberry pi 4b?

  • @kennethcate3263
    @kennethcate3263 3 ปีที่แล้ว +1

    Please fix the link for "Audio jack breakout board", part of it ended up as plain text.

    • @robingrosset6941
      @robingrosset6941  3 ปีที่แล้ว

      link in description fixed, here it is here too
      www.amazon.ca/Breakout-Headset-Stereo-Socket-Extension/dp/B0183KF5KI

  • @grantkobe9
    @grantkobe9 3 ปีที่แล้ว

    hello, can you tell me what is name of the audio out ?
    i am beginner . so i know a little. i can buy the capacitor, resistance, but i do not know how to got the audio output like you show in the video.
    i want try this way in your video.
    thanks in advance.

    • @robingrosset6941
      @robingrosset6941  3 ปีที่แล้ว +1

      Here is a link to a breakout board for a 3.5mm audio jack. This is the jack I was using.
      www.sparkfun.com/products/11570

    • @grantkobe9
      @grantkobe9 3 ปีที่แล้ว

      @@robingrosset6941 thank you very much :)

  • @yiqiujia4587
    @yiqiujia4587 3 ปีที่แล้ว

    Very good, can you record and send it?

  • @_..---
    @_..--- 3 ปีที่แล้ว +1

    THATS_COOL.H, I wonder if it can do FM transmission since the overclock can go that high.

    • @robingrosset6941
      @robingrosset6941  3 ปีที่แล้ว +1

      176MHz is not that high either someone managed to overclock a Pico to 270 MHz !
      www.raspberrypi.org/forums/viewtopic.php?t=301902&p=1810769

  • @zappyad
    @zappyad 3 ปีที่แล้ว +1

    Is there an explanation anywhere about what the hardware components are actually doing? Like why have each resistor / capacitor and what happens if you take them away? I can follow the software explanation to understand what is going on there, but the circuit side of it just 'appears' and it would be great to get an understanding of what is happening. Or is that a case of 'learn electronics and then it is obvious?' :)

    • @robingrosset6941
      @robingrosset6941  3 ปีที่แล้ว +7

      The Pico output is fed into an analogue filter called an RC filter (Low Pass Filter) this is the 220 ohm resistor and 100nF capacitor. then you have the 100 ohm resistor which effective acts as a voltage divider to get the voltage down to 1v ish for the audio levels. You could leave out the next bit too if you want and just have 3 components. The next capacitor 47 uF a.c. couples the analog signal in the audible frequency range. which we can then connected to an amp or headphones. The a.c. coupling is the last stage and you can omit it and it still works.

    • @robingrosset6941
      @robingrosset6941  3 ปีที่แล้ว +2

      This is based on the original Raspberry Pi Foundation hardware design circuit here datasheets.raspberrypi.org/rp2040/hardware-design-with-rp2040.pdf
      page 22 has the audio circuit and an explanation. It's not as detailed as above.
      The other place to look for background is this link. Which is a written blog describing RC filter for PWM audio
      gregchadwick.co.uk/blog/playing-with-the-pico-pt3/

    • @zappyad
      @zappyad 3 ปีที่แล้ว +1

      @@robingrosset6941 Thanks for the extra information. I was able to look up low pass / high pass filter online and now understand how it works. I'm even going to have a go at building it!👍

  • @nathancusson467
    @nathancusson467 5 หลายเดือนก่อน

    Hey, I am building a Raspberry Pico Gameboy Emulator, would this solution work for me?
    I am using Pico-GB from YouMakeTech

  • @olafschermann1592
    @olafschermann1592 2 หลายเดือนก่อน

    Awesome easy

  • @jeroentaverne8232
    @jeroentaverne8232 9 หลายเดือนก่อน

    Why do you need to set the same PWM duty cycle 8 times for every audio sample? Overclocking is not required when you set it only one time per sample I guess.

  • @M0XYM
    @M0XYM 3 ปีที่แล้ว +1

    Hi, do you think this could be done in micropython? I don't suppose you have tried?

    • @kevinwalters2076
      @kevinwalters2076 3 ปีที่แล้ว +1

      I spend more time in CircuitPython which can do PWM audio, I've not looked at RP2040 MicroPython for this. The 6.2.0-beta.4 release was a touch buggy for wav playback under certain conditions but I'm sure it'll be fixed soon. Several examples of audio on my channel with links to source code in the description.

    • @iC3mdbN
      @iC3mdbN 3 ปีที่แล้ว

      Have you tried in in Micropython, does it work?

    • @kevinwalters2076
      @kevinwalters2076 3 ปีที่แล้ว

      @@iC3mdbN Haven't tried sound samples there so far, I spend most of my time in CircuitPython unless I'm coding for the BBC micro:bit.

  • @JeremyCook
    @JeremyCook ปีที่แล้ว

    As the clip says, "that's cool." Nice work!
    Your audio circuit is a form of band pass filter, correct?
    If I wanted to use this code with the Arduino IDE, where would I get the needed libraries, e.g. stdio.h

  • @peterguthrie9924
    @peterguthrie9924 2 ปีที่แล้ว

    Why are you using a TRRS audio jack is it purely for the convenience of the breakout board?

    • @robingrosset6941
      @robingrosset6941  2 ปีที่แล้ว

      Yes its just an easy way to wire up an audit jack on a breadboard.

    • @peterguthrie9924
      @peterguthrie9924 2 ปีที่แล้ว

      @@robingrosset6941 I'm a bit new to Python and programming but I have a raspberry pi 3 with the code:
      if GPIO.input
      os.system('omxplayer -o local /file1.wav &')
      I'm just not sure how to modify this for the pico and to this circuit you created.

    • @robingrosset6941
      @robingrosset6941  2 ปีที่แล้ว

      @@peterguthrie9924 did you get the circuit with the pico to run the demo code and make sound? The code here that you have pasted takes a wav file and plays but it requires the Raspberry Pi Rspbian Operating System to work, this won’t work on a pico as it has no operating. You have to use the pico SDK or libraries to do things. To play your wav file you need to convert it to from a wav file to a header file using the python code in the example I gave. I hope this helps. Let me know which part is not working for you.

    • @peterguthrie9924
      @peterguthrie9924 2 ปีที่แล้ว

      @@robingrosset6941 I've only built my button pushing project with pi 3b so far, waiting on parts for the pico.

    • @peterguthrie9924
      @peterguthrie9924 2 ปีที่แล้ว

      @Robin Grosset my ultimate goal is to have a board that has 15 input push buttons which when each is pushed play a different sound file ("Help required in ...Department). So far I have working code for pi3 but ultimately using pico would a cool way to do it integrating it all on one PCB.

  • @spinal_cord
    @spinal_cord 4 หลายเดือนก่อน

    I realise this is a couple of years old now, but is it possible to play 16bit samples using pwm with this code?

    • @robingrosset6941
      @robingrosset6941  4 หลายเดือนก่อน

      Yes you can.
      So inside the interrupt handler there is a call to a function pwm_set_gpio_level (uint gpio, uint16_t level). the second paramter is the level and this is from the WAV_DATA so you can setup the WAV_DATA to be 16 bit integers uint_16_t instead of 8 bit . It should work fine.
      Do you have a 16 bit sample you want to play as 16 bit or is it that you want to covert the 16 bit sample to 8 bit? The reason for this being 8 bit to begin with was to reduce the memory size needed to store the WAV data and make it more compact. But you can use 16 bit its just your sample memory with twice as big.

    • @spinal_cord
      @spinal_cord 4 หลายเดือนก่อน

      ​@@robingrosset6941 I tried this sort of thing...
      int offset = wav_position>>3;
      uint16_t c = (WAV_DATA[offset + 1]

    • @spinal_cord
      @spinal_cord 3 หลายเดือนก่อน

      After experimenting a bit, it seems impossible to have any better than 12bit pwm for audio, anything higher makes too much noise, it seems it can't do the timing properly.@@robingrosset6941

  • @markday3145
    @markday3145 3 ปีที่แล้ว +1

    What is the PWM frequency relative to the sample frequency? I image you want that to be much higher, so that you get multiple cycles per sample.

    • @robingrosset6941
      @robingrosset6941  3 ปีที่แล้ว

      Yeah much higher at least 2x the sample frequency is normal. I had it at ~80kHz for an 11kHz sample. Making it super high also helps avoid hearing artifacts from it. Human hearing can hear 20kHz so I went with 4x that. The code is capable of 41kHz CD quality output with that too.

  • @manuelr7121
    @manuelr7121 ปีที่แล้ว

    this is so hard to do lol