Step-by-Step Guide For Coding with Command Terminal & Espressif¶
Step 1: Set Up Your Environment¶
- Install the Required Software:
- Ensure you have the Espressif IDF installed on your machine.
- Make sure Python and the required packages are installed.


- 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¶
- Create a New Project Directory:
- Navigate to your project folder:
cd C:\\Espressif\\frameworks\\esp-idf-v5.3.1\\blink\_led\\my\_esp32c3\_project - Generate a New Project:
-
You can create a sample project:
idf.py create-project led\_tilt\_switch cd led\_tilt\_switch¶
Step 3: Write the Code¶
- Open the Main Code File:
-
Navigate to the
mainfolder:
cd main -
Edit the
main.cFile: - Open
main.cin 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 } } } - Save the File:
- Save your changes and exit the text editor.
¶
Step 4: Configure Your Project¶
- Navigate Back to Project Root:
cd .. - Run the Configuration Tool:
idf.py menuconfig -
Navigate through the menu to set your target as ESP32-C3 and configure other settings as needed.
-
Save the Configuration:
- Press
Qto exit and save changes.
Step 5: Compile and Flash the Code¶
-
Compile the Project:
idf.py build -
Connect Your Xiao ESP32-C3 to Your Computer:
- Ensure the device is connected via USB.

- Flash the Code to the Device:
idf.py -p COMX flash -
Replace
COMXwith the correct COM port for your device. -
Monitor the Output:
idf.py -p COMX monitor
¶
Step 6: Connect the Hardware¶
- Connect the LED:
- Connect the anode (longer leg) of the LED to GPIO 2 (D0).
- Connect the cathode (shorter leg) to ground through a resistor (220Ω).
- Connect the Tilt Switch:
- Connect one terminal of the tilt switch to GPIO 3 (D1).
- 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¶
- Download Python from python.org.
- 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¶
- Connect the ESP32 via USB.
- Open Arduino IDE and go to Tools > Port to find your ESP32’s port (e.g.,
COM6). - 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)¶
- Open Arduino IDE.
- Enable Verbose Output:
- Go to File > Preferences.
- 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);
}
- Upload the sketch using Arduino IDE to verify it works.
- Click Verify (✔) instead of uploading again.
- Find the
.binfile paths in the compilation output (scroll up and locateBlink.ino.bin,Blink.ino.bootloader.bin, andBlink.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¶
1️⃣ Erase the ESP32 Flash (Recommended)¶
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

4️⃣ Flash the Blink Firmware¶
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:¶
- Install PlatformIO:

- Create a New Project:


- Write Your Code:

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