Friday, April 15, 2022

Simulate your controllers with WOKWI

For an index to all my stories click this text.

There is always something new to play with just around the corner.
And for one reason or another I frequently stumble on something called WOKWI. So I decided to have a look on what's it all about.

Short cut
WOKWI is a simulator. It simulates several Arduino and Raspberry controlers. In the right part of the screen you draw your circuit by placing the micro-controller and adding sensors and actuators. Then connect all items with wires just like you would do for example on a breadboard. At the left part of the screen you type your code. When everything is done you press the run button and your circuit runs on the screen just like it would do in a real setup.

So let's have a look on how it functions.

First WOKWI is a free service. You can find it here: https://wokwi.com/

The opening page offers you to login. The good part is that you do not need to. You do not have to login to use the service. The draw-back is that you can not save your projects if not logged in. You can build them, test them but just can not save them. So the choice is yours. I decided to create a login.



Start with choosing the controller you want to simulate. The opening screen shows that you can choose from:
- Arduino Uno
- Arduino Mega
- ESP32
- Arduino Nano
- Raspberry Pi Pico
- MicroPython on ESP32
- Franzininho Project (never heard of it)
- MicroPython on Pi Pico



Clicking on more options reveal some more.

So next to programming the ESP32 with C++ (Arduino) you can also try programming the ESP32 with MicroPython. And for the Raspberry Pi Pico there is C++, MicroPython and CircuitPython. And there are several Arduino's to experiment with. All in all a lot to dive into.

As you might (no should !!) know I have written a book on the Raspberry Pi Pico which focussed on programming the Pico with Python for beginners. 

You can find more information here: http://lucstechblog.blogspot.com/2021/08/raspberry-pi-pico-simplified-now.html

And as you can see WOKWI offers a simulation for the Raspberry Pi Pico with Python. So I just had to try that !! I tried two examples from my book.



At the main page on the top right side there is a drop down menu. Choosing my projects gives a graphical list of all your projects. That is if you made some previously ;)



The first example is a simple setup with a button and a led. The led turns on when the button is pressed.And it worked direct. Pressing the start-simulation button showed a range of 1's when the simulation started and that changed into 0's when the blue button was pressed.

Please remember that WOKWI is a simulator, so the code on the right side of the screen has to be written by yourself. Like said, this example was taken from my book.

You can try this simulation yourself with this link:

https://wokwi.com/projects/328570227913654868




The second simulation is also from my book and that uses a joystick to alter the position of the servo. And as you can see that worked also as it should.

The servo's arm indeed moved according to the position of the joystick.

You can try this simulation using this link: 

https://wokwi.com/projects/328572656023503444



When the simulation is not running you can add sensors. You can choose them by clicking on the button in the middle on top of the simulation screen. At the moment of writing there were about 40 sensors and actuators that you can use.

Wokwi is free to use. But there is a paid membership. Paying members have some privileges. They can choose what the next sensor/actuator or feature is that will be implemented.

Altering the schematics is easy.



If the above picture is not to your liking you can click on a component.



When the component (here the Pico) is selected simply press the letter R on your keyboard and it will rotate. The wires will stay connected to their pins but it does not look fancy.



So you have to alter the wiring manually. Which I have done here.



At the right side of your screen is the editor for the program. There is also a tab called diagram. Clicking that tab shows the JSON code for the components of your schematic.



This detail shows the JSON code for the connection of the LED to the Pico.



As you can see by manually changing the colors in the JSON code you can alter the colors of the wires in your schematic. Originally the wires were black and green and I changed them into black and blue.





This is the JSON code for the led itself. WOKWI chooses a red led standard as the led component. By altering the line attrs (attributes) in "attrs": {"color": "green"} the led will change from a red led to a green one.

There is a lot more to be discovered and please do so, as it is fun to play with.

Any Flaws ?

Well while playing with the first example (the led and the butrton) I noticed that when the simulation starts the led is ON just like if the button is pressed while it is not. After once pressing the button everything functions as normal.

This is a small flaw I noticed in my brief tests with WOKWI. At this moment I am not sure if there are more flaws.

Is it usefull ???

Well if you do not own one of the supported microcontrollers Wokwi offers a fun way to explore the possibilities. You can learn programming microcontrollers with MicroPython or C++ (arduino language) without actually owning one. Another option is when you want kids to learn working with microcontrollers without having to fear that something breaks. And you can use Wokwi to test some setups before actually building a prototype on a breadboard.

And: yes it is fun to play with.

Till next time
have fun

Luc Volders