4. Embedded programming

Forth week and coding.

For this week we had to simulate a code on the page WOKWI which is a simulation app where we can build a project with code and hardware to test if it works before building it in real life. I started by thinking about the code I wanted to do. After thinking about my options, I realized I wanted to start doing a part of the code for my final project. And this will fit perfectly for this week's assignment.

So, I started developing the code. The code will move 2 Nema's when a bottom is pushed. Originally the idea was that when an infrared sensor detects an interruption it will move the motors, but because the software doesn't count with infrared sensors, I had to change the idea a little.

I haven't decided which microcontroller to use, so I compared 2 of them, the basic one which is an Arduino uno and a ESP32 which is increasing popularity among developers. This comparation is also between old technology and new. Although Arduino is really popular, it's kind of out of date and the newest Esp, in this case the C6 is updated and with newest features.

Comparativa Arduino Uno vs ESP32 C6

Comparativa Arduino Uno vs ESP32 C6

Característica Arduino Uno ESP32 C6
Microcontrolador ATmega328P RISC-V single-core
Velocidad de reloj 16 MHz Hasta 160 MHz
Memoria Flash 32 KB 320 KB ROM, 512 KB SRAM
Entradas/Salidas 14 digitales, 6 analógicas 30 o 22 GPIOs
Conectividad Ninguna Wi-Fi 6, Bluetooth 5 (LE), Zigbee
Voltaje de operación 5V 2.4 GHz
Corriente por pin 40 mA Variable según el modo
Protocolos soportados I2C, SPI, UART I2C, SPI, UART, I2S, RMT, TWAI, PWM
Consumo de energía Moderado Ultra-bajo
Precio aproximado $20 USD $5-10 USD

Programing with Arduino

Programing with Arduino is easy, the code language is C++

Let's open Wokwi on the internet (You can use any search engine you want) and select which controller will be using. I selected Arduino.

The software will give you the option to use featured projects if you already have something in mind.

I will select the new sketch option. Once in we can insert our Arduino code and add new components. I will use 2 step motors, a button with a pull-down configuration and a motor controller A4988

You can insert the components pushing the "add parts" circle.

Now I must wire all the components together. Here you can see an example of A4988 wiring

Now we have to insert the code.

Now we just have to test if everything works correctly. To start the code just click the green circle with the play figure and it will start compiling. Once it finish the program will move the motors once the button is pressed.

Here's a video showing how I wired all and tested the code. This example with Arduino I wanted to use a motor controller because of the current.

Programing with ESP32

Now let's use the ESP32, as we can see in the pictures the ESP 32 is smaller and has more features like Wi-Fi connection and Bluetooth. The process to simulate with aESP32 in Wokwi is almost the same as the Arduino. In the main menu choose ESP32, now in ‘starter templates’ choose whichever is the closest to the ESP32 that will be using.

The code for the ESP32 is a little different from the Arduino, but the base is almost the same.

With the Esp32 its recomended to use a motor controller due to the current consuption but I wanted to test it directly as you can see in the next image. The Button must be connected with a pull-up configuration this time.

Here you can see a video of me doing the pull-up resistor and testing the code.

And the code works. This week was really interesting because I've never used this simulator so it's good to know that i can always test in this page before doing it in real life.