XIAO ESP32-C3 ESP and SW-420 Vibration Sensor

Hi all
Completely new to the ESP32. I have had some experience with arduino. I am trying to find some example code showing how to use the SW-420 vibration sensor with the ESP32. I find a lot of arduino examples but none for the ESP32. My goal is to transmit via ethernet when movement starts and stops.
But I think getting the sensor working is the first step. Can someone point me to some example code for this.

Thank you

After more research it seems that the ESP32 is supposed to be able to run arduino sketches. I have successfully installed and ran sample programs for the ESP32 but have not been able to run arduino sketches. I have this sketch to run the vibration sensor for the arduino

int Vibration_signal = B10;
int Sensor_State = 1;

void setup() {
  pinMode(Vibration_signal,INPUT);
 Serial.begin(9600);
}

void loop() {
  Serial.print("Vibration Status: ");
 Sensor_State = digitalRead(Vibration_signal);
 if (Sensor_State ==1){
 Serial.println("Machine Running");
  }

  else {
    Serial.println("Machine Stopped");
  }
  delay(50);
}

But When I try to compile and run the code I receive the following error. Any suggestions as to what to do to fix it would be appreciated.


“C:\Users\Tim\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.7/tools/gen_esp32part.exe” -q “C:\Users\Tim\AppData\Local\Temp\arduino_build_501080/partitions.csv” “C:\Users\Tim\AppData\Local\Temp\arduino_build_501080/ESP32_Test_3.ino.partitions.bin”

cmd /c if exist “C:\Users\Tim\AppData\Local\Temp\arduino_build_501080\libraries\Insights” “C:\Users\Tim\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.7/tools/gen_insights_package.exe” “C:\Users\Tim\AppData\Local\Temp\arduino_build_501080” ESP32_Test_3.ino “C:\Users\Tim\Documents\Arduino\ESP32_Test_3”

cmd /c IF 0==1 COPY /y “C:\Users\Tim\Documents\ArduinoData\packages\esp32\tools\openocd-esp32\v0.11.0-esp32-20221026\share\openocd\scripts\board\esp32c3-builtin.cfg” “C:\Users\Tim\Documents\Arduino\ESP32_Test_3\debug.cfg”

cmd /c IF 0==1 COPY /y “C:\Users\Tim\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.7\tools\ide-debug\esp32c3.json” “C:\Users\Tim\Documents\Arduino\ESP32_Test_3\debug_custom.json”

cmd /c IF 0==1 COPY /y “C:\Users\Tim\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.7\tools\ide-debug\svd\esp32c3.svd” “C:\Users\Tim\Documents\Arduino\ESP32_Test_3\debug.svd”

“C:\Users\Tim\Documents\ArduinoData\packages\esp32\tools\riscv32-esp-elf-gcc\esp-2021r2-patch5-8.4.0/bin/riscv32-esp-elf-size” -A “C:\Users\Tim\AppData\Local\Temp\arduino_build_501080/ESP32_Test_3.ino.elf”

Sketch uses 229042 bytes (17%) of program storage space. Maximum is 1310720 bytes.

Global variables use 15820 bytes (4%) of dynamic memory, leaving 311860 bytes for local variables. Maximum is 327680 bytes.

REMOVE/esptool.exe --chip esp32c3 --port COM3 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x0 C:\Users\Tim\AppData\Local\Temp\arduino_build_501080/ESP32_Test_3.ino.bootloader.bin 0x8000 C:\Users\Tim\AppData\Local\Temp\arduino_build_501080/ESP32_Test_3.ino.partitions.bin 0xe000 C:\Users\Tim\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.7/tools/partitions/boot_app0.bin 0x10000 C:\Users\Tim\AppData\Local\Temp\arduino_build_501080/ESP32_Test_3.ino.bin

java.io.IOException: Cannot run program “REMOVE/esptool.exe”: CreateProcess error=2, The system cannot find the file specified

at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)

at processing.app.helpers.ProcessUtils.exec(ProcessUtils.java:26)

at cc.arduino.packages.Uploader.executeUploadCommand(Uploader.java:129)

at cc.arduino.packages.uploaders.SerialUploader.runCommand(SerialUploader.java:383)

at cc.arduino.packages.uploaders.SerialUploader.uploadUsingPreferences(SerialUploader.java:197)

at cc.arduino.UploaderUtils.upload(UploaderUtils.java:77)

at processing.app.SketchController.upload(SketchController.java:732)

at processing.app.SketchController.exportApplet(SketchController.java:703)

at processing.app.Editor$UploadHandler.run(Editor.java:2061)

at java.lang.Thread.run(Thread.java:748)

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified

at java.lang.ProcessImpl.create(Native Method)

at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)

at java.lang.ProcessImpl.start(ProcessImpl.java:137)

at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)

... 9 more

An error occurred while uploading the sketch

Hi Timk5000,
I tried this with a new unit just received, Seems to work OK.
HTH
GL:-)
PJ

// Test code to get chip ID and STUFF	
uint32_t chipId = 0;
int Vibration_signal = B10;
int Sensor_State = 1;
void setup() {
	Serial.begin(9600);
  delay (2000);

  for(int i=0; i<17; i=i+8) {
	  chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
	}
  pinMode(Vibration_signal,INPUT);
 Serial.begin(9600);
	Serial.printf("ESP32 Chip model = %s Rev %d\n", ESP.getChipModel(), ESP.getChipRevision());
	Serial.printf("This chip has %d cores\n", ESP.getChipCores());
  Serial.print("Chip ID: "); Serial.println(chipId);
}

void loop() {
  Serial.print("Vibration Status: ");
 Sensor_State = digitalRead(Vibration_signal);
 if (Sensor_State ==1){
 Serial.println("Machine Running");
  }

  else {
    Serial.println("Machine Stopped");
  }

	delay(3000);
}

I get this Output AOK

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4200ec40
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5810,len:0x438
load:0x403cc710,len:0x91c
load:0x403ce710,len:0x25b0
entry 0x403cc710
ESP32 Chip model = ESP32-C3 Rev 3
This chip has 1 cores
Chip ID: 4196588
Vibration Status: Machine Stopped
Vibration Status: Machine Stopped
Vibration Status: Machine Stopped

:grin: :+1:

Here is the Build Log info, just FYI

FQBN: esp32:esp32:deneyapkartg
Using board 'deneyapkartg' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8
Using core 'esp32' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8
"C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.bin" "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.elf"
esptool.py v4.5.1
Creating esp32c3 image...
Merged 2 ELF sections
Successfully created esp32c3 image.
"C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.8/tools/gen_esp32part.exe" -q "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F/partitions.csv" "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.partitions.bin"
cmd /c if exist "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F\\libraries\\Insights" "C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.8/tools/gen_insights_package.exe" "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F" GetChipID.ino "D:\\Arduino_projects\\GetChipID"
cmd /c IF 0==1 COPY /y "C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\openocd-esp32\\v0.11.0-esp32-20221026\\share\\openocd\\scripts\\board\\esp32c3-builtin.cfg" "D:\\Arduino_projects\\GetChipID\\debug.cfg"
cmd /c IF 0==1 COPY /y "C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.8\\tools\\ide-debug\\esp32c3.json" "D:\\Arduino_projects\\GetChipID\\debug_custom.json"
cmd /c IF 0==1 COPY /y "C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\2.0.8\\tools\\ide-debug\\svd\\esp32c3.svd" "D:\\Arduino_projects\\GetChipID\\debug.svd"

"C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\riscv32-esp-elf-gcc\\esp-2021r2-patch5-8.4.0/bin/riscv32-esp-elf-size" -A "C:\\Users\\Dude\\AppData\\Local\\Temp\\arduino\\sketches\\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.elf"
Sketch uses 230134 bytes (17%) of program storage space. Maximum is 1310720 bytes.
Global variables use 15856 bytes (4%) of dynamic memory, leaving 311824 bytes for local variables. Maximum is 327680 bytes.
(Trucated_PJG)...
"C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\4.5.1/esptool.exe" --chip esp32c3 --port "COM23" --baud 921600  --before default_reset --after hard_reset write_flash  -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x0 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.bootloader.bin" 0x8000 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.partitions.bin" 0xe000 "C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.8/tools/partitions/boot_app0.bin" 0x10000 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\B0BFC6AD928EE0E6D13E03AD142A923F/GetChipID.ino.bin" 
esptool.py v4.5.1
Serial port COM23
Connecting...
Chip is ESP32-C3 (revision v0.3)
Features: WiFi, BLE
Crystal is 40MHz
MAC: a0:76:4e:40:08:ec
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00003fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x0004afff...
Compressed 13152 bytes to 9473...
Writing at 0x00000000... (100 %)
Wrote 13152 bytes (9473 compressed) at 0x00000000 in 0.2 seconds (effective 448.9 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 146...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (146 compressed) at 0x00008000 in 0.0 seconds (effective 636.2 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 639.7 kbit/s)...
Hash of data verified.
Compressed 241664 bytes to 135200...
Writing at 0x00010000... (11 %)
Writing at 0x0001ab45... (22 %)
Writing at 0x000226cd... (33 %)
Writing at 0x000284d7... (44 %)
Writing at 0x0002e661... (55 %)
Writing at 0x0003464d... (66 %)
Writing at 0x0003c2a9... (77 %)
Writing at 0x00042ff8... (88 %)
Writing at 0x000495a3... (100 %)
Wrote 241664 bytes (135200 compressed) at 0x00010000 in 2.1 seconds (effective 923.7 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Sorry for the delay in reply. It seems that my Arduino IDE may have been to old or damaged. After refreshing the install of the Arduino IDE was able to compile and load the code you provide. Thanks for your help.

Hi @PJ_Glasso ,

I see a power profile kit in the photo, is it worth it to measure the total current measurement of modules/PCB?

I have designed a PCB with has a xiao esp32s3 and some sensors connected, I want to calculate total power consumption so that I can use a suitable battery according to the power consumption.

In a word YES, $99 will tell you if your on the right track for battery and power savings or BLOTED, It helped me find some code that was leaving allot of wasted juice. (BLE)
It’s also a cheep way to get 9 0r 10 channels for logic analyzing as a bonus so to speak.
None of the power stuff of Seeed studios is to be trusted as FACT so VERIFY, VERIFY, and RE-Verify when they update board files & spec’s . IMO YMMV
HTH
GL :slight_smile: PJ
I use this Battery calc to START

1 Like

Thanks @PJ_Glasso now I am clear to order one.