Skip to main content

Week4 - Microcontroller Simulation

Simulation Environment

Simulation is a process where I can test my code without building a real circuit. Here I choose Wokwi as my simulator.

Wokwi is a web-based simulator for Arduino, ESP32 or other microcontrollers.

Then, we can go to here and find the simulator of ESP32C3 with XIAO series.

I will do the simulation by ESP32C3 which I choosed base on group assignment.

Simulate blinking LED at ESP32C3. This is a simple example to test the simulation environment.

Let us create a new project in Wokwi.

  • Choose XIAO ESP32C3.
  • Remove yellow LED and green LED.

Then, write program as below.

void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
}

void loop() {
digitalWrite(D2, HIGH);
delay(500);
digitalWrite(D2, LOW);
delay(500);
}

Then, the result of simulation is shown as below.

The LED will turn on 0.5 seconds and turn off 0.5 seconds repeatedly.

Example2 - OLED Testing

Refer to my classmate Hongtai, online project by nilutpolkashyap and other online project, I test the ESP32C2 with OLED screen.

Let us create a new project in Wokwi

  • Choose XIAO ESP32C3.
  • Choose SSD1306 OLED Display.

According the pinout diagram which is shown as above, D4 and D5 are SDA and SCL pin, respectively. SDA and SCL pin are I2C pin and SSD1306 OLED screen is operated by I2C. The circuit diagram is shown as below.

Then, write a program as below to show "hello, fab academy" on the OLED screen.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED width in pixels.
#define SCREEN_HEIGHT 64 // OLED height (pixels)
#define OLED_RESET -1 // reset pin (-1 means share Arduino reset pin)
#define I2C_ADDRESS 0x3C // Common I2C address (0x3C or 0x3D)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
Wire.begin(SDA, SCL); // Initialize I2C, default pins of XIAO ESP32S3 are D4(SDA)/D5(SCL).

// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, I2C_ADDRESS)) {
Serial.println(F("SSD1306 failed to allocate"));
for(;;); // stuck loop
}

display.clearDisplay(); // clear display cache
display.setTextSize(1); // set the text size (1:1 ratio)
display.setTextColor(SSD1306_WHITE); // draw white text

display.setCursor(0, 0); // start coordinate (0,0)
display.println("hello, fab academy"); // draw white text.

display.println("hello, fab academy");
display.display(); // output the cached content to the screen
}

void loop() {
// Empty loop, only need to display once
}

Then, the simulation result is shown as below.

Resources of project