Friday, January 20, 2023

Importing libraries in Wokwi

For an index to all my stories click this text.

Few months ago I was playing with Wokwi. And then I discovered something that even the developers did not know was possible. And things like that always flatters my ego.

For those that are not familiar with Wokwi here is a short description.

Wokwi is a simulator. It simulates Arduino, Raspberry Pi, ESP32 and Raspberry Pi Pico controllers. You can build simulations using the C++ and MicroPython languages. I did a more in depth story on Wokwi earlier on this weblog. You can find that story here: http://lucstechblog.blogspot.com/2022/04/simulate-your-controllers-with-wokwi.html  Wokwi self can be found here: https://wokwi.com/

What I wanted to do is to attach a TM1637 display to a Raspberry Pi Pico and simulate that in Wokwi. The TM1637 display is available in Wokwi but there is no support for MicroPython. Only the Arduino IDE is supported. Bummer !!

Then I had an idea.

I opened a new project in Wokwi. I chose for opening a project with MicroPython on Pi Pico.



With the + sign I added the TM1637 display and attached the wires.

As I wrote in my books Raspberry Pi Pico Simplified and Raspberry Pico W Simplified most MicroPython libraries are just programs.

 

I downloaded the library and in Wokwi I opened a new file and called it tm1637.py and pasted the code from the Library. In the main.py program I put import tm1637 and that's it.



And there you go. Worked like a charm.

Just make sure that you give the new opened file in Wokwi exact the same name as the library has. If you differ the program will not be able to find the library, and generally the library will not work.

You can find the example here:
https://wokwi.com/projects/339373435833549395

There is a program called main.py and that imports tm1637.py which is the library and a separate file. The library is copied from the github page and just pasted into the file.

If you need to work with the TM1637 in Wokwi just copy my project in Wokwi and add your own MicroPython code.

I can not guarantee that this trick will work with any library you find on the internet. Just try it. You can not break anything physically in a simulation................

On the Wokwi channel in Discord I mentioned this to Wokwi's developers. They did not know that this was possible and they said that they would put this into the documentation. And that indeed flatters my ego.

Have fun and be creative !
Till next time.

Luc Volders