Step-by-Step Guide For Coding with Command Terminal & Espressif

Step 1: Set Up Your Environment

  1. Install the Required Software:
  2. Ensure you have the Espressif IDF installed on your machine.
  3. Make sure Python and the required packages are installed.

  1. Set Up the ESP-IDF:

Open your command terminal and set up the ESP-IDF environment:

C:\\Espressif\\frameworks\\esp-idf-v5.3.1\\export.bat

Step 2: Create a New Project

  1. Create a New Project Directory:
  2. Navigate to your project folder:
    cd C:\\Espressif\\frameworks\\esp-idf-v5.3.1\\blink\_led\\my\_esp32c3\_project
  3. Generate a New Project:
  4. You can create a sample project:

    idf.py create-project led\_tilt\_switch cd led\_tilt\_switch

Step 3: Write the Code

  1. Open the Main Code File:
  2. Navigate to the main folder:
    cd main

  3. Edit the main.c File:

  4. Open main.c in a text editor of your choice and replace the code with the following:
    c #include <stdio.h\> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" #define LED _PIN GPIO\_NUM\_2 // D0 #define TILT _SWITCH\_PIN GPIO\_NUM\_3 // D1 void init _gpio() { gpio\_set\_direction(LED\_PIN, GPIO\_MODE\_OUTPUT); gpio\_set\_direction(TILT\_SWITCH\_PIN, GPIO\_MODE\_INPUT) gpio\_pullup\_dis(TILT\_SWITCH\_PIN); // Disable pull-up } void app\_main(void) { init\_gpio(); printf("Starting LED blink on GPIO 2 (D0) controlled by tilt switch on GPIO 3 (D1)...\\n"); while (1) { int tilt\_switch\_state \= gpio\_get\_level(TILT\_SWITCH\_PIN); if (tilt\_switch\_state \== 0) { // Tilt switch is activated gpio\_set\_level(LED\_PIN, 1); // Turn LED on printf("LED ON\\n"); vTaskDelay(pdMS\_TO\_TICKS(2000)); // Delay for 2000 ms gpio\_set\_level(LED\_PIN, 0); // Turn LED off printf("LED OFF\\n"); vTaskDelay(pdMS\_TO\_TICKS(2000)); // Delay for 2000 ms } else { gpio\_set\_level(LED\_PIN, 0); printf("LED OFF (Tilt switch not activated)\\n"); vTaskDelay(pdMS\_TO\_TICKS(500)); // Short delay } } }
  5. Save the File:
  6. Save your changes and exit the text editor.

Step 4: Configure Your Project

  1. Navigate Back to Project Root:
    cd ..
  2. Run the Configuration Tool:
    idf.py menuconfig
  3. Navigate through the menu to set your target as ESP32-C3 and configure other settings as needed.

  4. Save the Configuration:

  5. Press Q to exit and save changes.

Step 5: Compile and Flash the Code

  1. Compile the Project:
    idf.py build

  2. Connect Your Xiao ESP32-C3 to Your Computer:

  3. Ensure the device is connected via USB.

  1. Flash the Code to the Device:
    idf.py -p COMX flash
  2. Replace COMX with the correct COM port for your device.

  3. Monitor the Output:
    idf.py -p COMX monitor

Step 6: Connect the Hardware

  1. Connect the LED:
  2. Connect the anode (longer leg) of the LED to GPIO 2 (D0).
  3. Connect the cathode (shorter leg) to ground through a resistor (220Ω).
  4. Connect the Tilt Switch:
  5. Connect one terminal of the tilt switch to GPIO 3 (D1).
  6. Connect the other terminal to ground.

Step 7: Test Your Setup

  • After flashing the code, tilt the switch to see the LED blink every 2 seconds when activated. If the switch is not activated, the LED should remain off.

Summary

You have now programmed the Xiao ESP32-C3 to control an LED based on the state of a tilt switch using the command terminal.

Flash ESP32 Using esptool (Windows)

Step 1️⃣: Install Python and esptool

  1. Download Python from python.org.
  2. Install Python and tick the option “Add python.exe to PATH” during installation.

Open Command Prompt (cmd) and install esptool:
pip install esptool

Verify installation by running:
esptool --version


Step 2️⃣: Identify the ESP32's COM Port

  1. Connect the ESP32 via USB.
  2. Open Arduino IDE and go to Tools > Port to find your ESP32’s port (e.g., COM6).
  3. If unsure, unplug the ESP32, check the ports, then plug it back in and see which new port appears.

Step 3️⃣: Create the .bin Files (Firmware)

  1. Open Arduino IDE.
  2. Enable Verbose Output:
  3. Go to File > Preferences.
  4. Check "Show verbose output during compilation".

Modify the Blink sketch to keep the LED on for 3 seconds and off for 0.5 seconds:
void setup() {

pinMode(2, OUTPUT);

}

void loop() {

digitalWrite(2, HIGH);

delay(3000);

digitalWrite(2, LOW);

delay(500);

}

  1. Upload the sketch using Arduino IDE to verify it works.
  2. Click Verify (✔) instead of uploading again.
  3. Find the .bin file paths in the compilation output (scroll up and locate Blink.ino.bin, Blink.ino.bootloader.bin, and Blink.ino.partitions.bin).

Step 4️⃣: Locate esptool.exe

Open Command Prompt and run:
where esptool

This will return a path like:
C:\Users\YourUser\AppData\Local\Programs\Python\PythonXX\Scripts\esptool.exe

Change the directory to this path (without esptool.exe at the end):
cd C:\Users\YourUser\AppData\Local\Programs\Python\PythonXX\Scripts


Step 5️⃣: Flash the ESP32 Using esptool

esptool --chip esp32 --port COM6 erase_flash

2️⃣ Flash the Bootloader

esptool --chip esp32 --port COM6 --baud 460800 write_flash 0x1000 C:\path\to\Blink.ino.bootloader.bin

3️⃣ Flash the Partition Table

esptool --chip esp32 --port COM6 --baud 460800 write_flash 0x8000 C:\path\to\Blink.ino.partitions.bin

esptool --chip esp32 --port COM6 --baud 460800 write_flash 0x10000 C:\path\to\Blink.ino.bin

📌 Replace C:\path\to\ with the actual paths to your .bin files.


Step 6️⃣: Reset the ESP32

  • Press the RESET button on the ESP32 after flashing.
  • If it doesn’t start, hold BOOT, press RESET, then release BOOT.

Final Steps

  • Your ESP32’s LED should blink with the new timing (ON for 3s, OFF for 0.5s).
  • If not, check the Serial Monitor (baud rate 115200) for errors.

Summary of Steps

1️⃣ Install Python and pip install esptool.
2️⃣ Find ESP32's COM port using Arduino IDE.
3️⃣ Enable verbose compilation and get the .bin files.
4️⃣ Locate esptool.exe path using where esptool.
5️⃣ Move to the esptool.exe directory in cmd before flashing.
6️⃣ Erase flash and write firmware using esptool.
7️⃣ Press RESET on ESP32 to start execution.

Using PlatformIO

Steps:

  1. Install PlatformIO:

  1. Create a New Project:

  1. Write Your Code:

  1. Upload the Code:
  2. Click the upload button in PlatformIO to compile and upload the code to the XIAO ESP32C.