Discover how to use I2S with Micropython on the TinyPICO board in this detailed tutorial, comparing C++ and Micropython implementations. Learn how to build custom Micropython firmware with I2S support, set up an environment for the project, and use I2S for recording and playing back audio.
[0:00] We’ve been doing lots of audio projects using Arduino and IDF code.
[0:04] All in C++
[0:06] But what about Micropython - does it support audio?
[0:09] So, let’s check this.
[0:10] I’ve got my TinyPico wired up and I’ve flashed it with the latest Mircopython firmware.
[0:15] Let’s see if we can use I2S
[0:19] It looks like I2S support is not built-in yet,
[0:22] but fortunately, when we look on GitHub we do find a pull request that will add support.
[0:26] Let’s do a custom build of Micropython and try it out.
[0:31] But first I’d like to thank PCBWay for sponsoring this video,
[0:34] PCBWay offer PCB Production, CNC and 3D Printing, PCB Assembly and much much more.
[0:40] They are great to deal with and offer excellent quality, service and value for money.
[0:44] Check out the link in the description.
[0:47] Building Micropython is actually pretty straightforward.
[0:50] The first thing we’ll need is an installation of the Esspresif IDF
[0:54] I’m also going to set my Python version using a program called pyenv.
[0:59] This will make sure I’m using python3 for everything.
[1:02] We clone the ESP-IDF from GitHub using this command:
[1:07] This will clone the repository at release 4.2 and include all the required git submodules.
[1:13] I’m using 4.2 as this is the latest version that the Micopython README says is compatible
[1:19] If you’re feeling brave you can try other releases. And even try the bleeding edge releases.
[1:25] Once we’ve got the IDF cloned we just need to change into the directory and run this command.
[1:30] This will download all the required dependencies and gets everything set up.
[1:34] From now on, when we want to use the IDF to build a project
[1:37] we just change into the ESP-IDF directory and run this little snipped of code.
[1:43] This configures all our paths and environment variables for the IDF.
[1:46] To get our custom version of Micropython we just clone the main Micropython repository.
[1:51] Using this command.
[1:54] This gives us the latest code
[1:55] and then to add support for I2S we need to get the pull request using this command:
[2:01] This command fetches the pull request with id 7183 and puts it in a local branch called i2s_support.
[2:08] We can now simply merge that branch into our current branch using git merge:
[2:13] Obviously, this does come with a big caveat.
[2:15] The pull request has not been finished so the APIs and the functionality may change.
[2:21] We can now follow the instructions for building Micropython for the ESP32.
[2:25] First, we build the cross-compiler in the root of the repository:
[2:29] And then we build the ESP32 port.
[2:32] I’m going to build explicitly for the TinyPICO board.
[2:36] You can also build for Generic boards and Generic boards with SPI ram.
[2:39] Once the build is finished it actually tells us the command to run to flash the new firmware to the device.
[2:46] We just need to copy and paste this and our board will be updated.
[2:50] Now if we go back to the Thonny IDE and reconnect can see that I2S is now available.
[2:57] I’ve created a pretty simple python script to record audio to flash and then play it back.
[3:02] I’ll run through the bits of this code
[3:04] and put the equivalent C++ code next to it so we can compare implementations.
[3:09] The first thing we do is import I2S and Pin. This is very similar to the hash includes that we see in the C++ code.
[3:18] Here we have the code for setting up a GPIO pin. We are defining it as an input pin and setting it to have an internal pull-down resistor.
[3:26] And I’ve created a little helper function to wait for the button to be pressed with a small delay to handle any bouncing.
[3:31] You can see that the code pretty much maps across between Python and C++
[3:36] Here we have the setup for the I2S microphone. This is very similar to the C++ code but it’s a bit less verbose.
[3:44] This is true again for setting up the I2S amplifier. It all looks quite familiar - again the python code is less verbose than the C++ code.
[3:53] Here’s the code for recording the audio to a file.
[3:57] We open up the file for writing binary.
[3:59] We’re using the with operator which ensures that the file is closed when we are finished with it.
[4:04] We read in the samples from the I2S device.
[4:08] Amplify them by using this helper function that shifts values to the left.
[4:11] Initially, I tried doing this in python code, but it was pretty slow.
[4:16] And then we simply write the samples to the file.
[4:19] Playback is pretty similar looking code.
[4:22] Once again we open up the file - this time for reading binary.
[4:26] We read in the samples.
[4:28] And repeatedly write the samples to the i2s device until there is no more data in the file.
[4:33] I’ve put the complete code side by side here, python on the left and C++ on the right
[4:38] so you can get an idea of how long each implementation is.
[4:41] Obviously, you could make the C++ code shorter by removing comments and merging lines together
[4:46] but the python code is definitely less verbose and possibly easier to understand.
[4:52] but this does come at the code of some flexibility
[4:55] We don’t have all the options that we have in C++ world with access to all the Arduino and IDF functions.
[5:01] Let’s see it in action
[5:03] I’ve copied the script into Thonny.
[5:05] All we have to do is hit run.
[5:17] It seems to work pretty well.
[5:19] I’ve put my sample code on GitHub - there’s a link in the description.
[5:23] There’s a repo on GitHub as well with a lot more examples
[5:26] That’s linked from the PR on MicroPyhton.
[5:28] I’ve added a link to it as well.
[5:30] As I have already said - the code is likely to change before it makes it into the main release of Micropython
[5:36] so keep an eye out.
[5:37] Once the PR has been finished and merged into the main release I’ll revisit this topic and make any updates necessary.
[5:43] Thanks for watching
[5:45] I’ll see you in the next video!