-1

I'm new to ESP32 development and I'm trying to compile a program in the Arduino IDE to record the vibrations by an ADXL345 sensor. However, during compilation, I encounter a "ModuleNotFoundError: No module named 'serial'" error which seems to be related to serial communication.

I'm using an ESP32 Dev Module with the Arduino IDE version 1.18.19 on Ubuntu 22.04 Jammy Here's the code I'm trying to compile :

/***************************************************************************
* Example sketch for the ADXL345_WE library
*
* This sketch shows how use SPI for the basic data sketch. Please apply the 
* same steps in the other sketches if you want to use SPI.  
*  
* Further information can be found on:
* https://wolles-elektronikkiste.de/adxl345-teil-1 (German)
* https://wolles-elektronikkiste.de/en/adxl345-the-universal-accelerometer-part-1 (English)
* 
***************************************************************************/

#include<ADXL345_WE.h>
#include<SPI.h>
#define CS_PIN 5   // Chip Select Pin

#define NUM_SAMPLES 3200

bool spi = true;    // flag indicating that SPI shall be used

xyzFloat samples[NUM_SAMPLES];
ADXL345_WE myAcc = ADXL345_WE(CS_PIN, spi);

const int int2Pin = 22;  // interrupt pin
volatile bool dataReady = false;

void setup(){
  Serial.begin(921600);
  Serial.println("ADXL345 Demo");
  if(!myAcc.init()){
    Serial.println("ADXL345 not connected!");
  }

  // Prepare int pin for input
  pinMode(int2Pin, INPUT);

  /* You can set the SPI clock speed. Default is 5 MHz. */
  //  myAcc.setSPIClockSpeed(4000000);
    
  /* Choose the data rate         Hz
      ADXL345_DATA_RATE_3200    3200
      ADXL345_DATA_RATE_1600    1600
      ADXL345_DATA_RATE_800      800
      ADXL345_DATA_RATE_400      400
      ADXL345_DATA_RATE_200      200
      ADXL345_DATA_RATE_100      100
      ADXL345_DATA_RATE_50        50
      ADXL345_DATA_RATE_25        25
      ADXL345_DATA_RATE_12_5      12.5  
      ADXL345_DATA_RATE_6_25       6.25
      ADXL345_DATA_RATE_3_13       3.13
      ADXL345_DATA_RATE_1_56       1.56
      ADXL345_DATA_RATE_0_78       0.78
      ADXL345_DATA_RATE_0_39       0.39
      ADXL345_DATA_RATE_0_20       0.20
      ADXL345_DATA_RATE_0_10       0.10
  */
  myAcc.setDataRate(ADXL345_DATA_RATE_3200);

  // Safety delay
  delay(100);
  Serial.print("Data rate: ");
  Serial.print(myAcc.getDataRateAsString());

  /* In full resolution the size of the raw values depend on the
      range: 2g = 10 bit; 4g = 11 bit; 8g = 12 bit; 16g =13 bit;
      uncomment to change to 10 bit for all ranges. 
  */
  // myAcc.setFullRes(false);

  /* Choose the measurement range.
      ADXL345_RANGE_16G    16g     
      ADXL345_RANGE_8G      8g     
      ADXL345_RANGE_4G      4g   
      ADXL345_RANGE_2G      2g
  */
  myAcc.setRange(ADXL345_RANGE_4G);
  Serial.print("  /  g-Range: ");
  Serial.println(myAcc.getRangeAsString());
  Serial.println();

  // Define interrupt routine and move data_ready to adxl int2
  attachInterrupt(digitalPinToInterrupt(int2Pin), dataReadyISR, RISING);
  myAcc.setInterrupt(ADXL345_DATA_READY, INT_PIN_2); 
  myAcc.readAndClearInterrupts();

  // TODO add calibration routine
}


void loop() {

  int missed = 0;

  Serial.println("Beginning capture.");
  // Begin data acq
  for(int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
    // Wait for new data
    while(!dataReady) {}
    dataReady = false;
    samples[cnt] = myAcc.getGValues();
    if (dataReady) {
      // new data received before we were ready to process it => probable miss
      missed ++;
    }
  }
  Serial.println("Capture done.");

  Serial.println("Removing DC");
  processRemoveDC();

  Serial.println("DATA_START");
  Serial.println("X\tY\tZ");
  for(int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
    Serial.printf("%f\t%f\t%f\n", samples[cnt].x,samples[cnt].y,samples[cnt].z);
  }
  Serial.println("DATA_END");
  Serial.printf("%d Sample(s) missed\n", missed);

  printRMS();
  
  delay(5000);
  
}

/* Removes the continuous (dc) component
    This will operate inplace and edit the acquired data !
*/
void processRemoveDC() {
  float meanX, meanY, meanZ = 0;
  for (int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
    meanX += samples[cnt].x;
    meanY += samples[cnt].y;
    meanZ += samples[cnt].z;
  }
  meanX /= (float) NUM_SAMPLES;
  meanY /= (float) NUM_SAMPLES;
  meanZ /= (float) NUM_SAMPLES;
  for (int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
    samples[cnt].x -= meanX;
    samples[cnt].y -= meanY;
    samples[cnt].z -= meanZ;
  }
}
/* Computes and print the RMS per axis
*/
void printRMS() {
  float rmsX, rmsY, rmsZ = 0;
  for (int cnt = 0; cnt < NUM_SAMPLES; cnt++) {
    rmsX += pow(samples[cnt].x, 2);
    rmsY += pow(samples[cnt].y, 2);
    rmsZ += pow(samples[cnt].z, 2);
  }
  rmsX = sqrt(rmsX/(float)NUM_SAMPLES);
  rmsY = sqrt(rmsY/(float)NUM_SAMPLES);
  rmsZ = sqrt(rmsZ/(float)NUM_SAMPLES);

  Serial.printf("RMS : X %f\tY %f\tZ %f\n", rmsX, rmsY, rmsZ);
}

void dataReadyISR(){
  dataReady = true;
}

I've checked that I've selected the right serial port in the Arduino IDE and that the USB drivers for the ESP32 are correctly installed on my computer. I've also tried restarting my computer and updating the ESP32 package in the Arduino IDE, but the problem persists.

How can I resolve this error and successfully compile my program for ESP32?

Thanks in advance for your help!

here is the error:

WARNING: Category '' in library ESP Insights is not valid. Setting to 'Uncategorized'
WARNING: Category '' in library ESP RainMaker is not valid. Setting to 'Uncategorized'
WARNING: Category '' in library WiFiProv is not valid. Setting to 'Uncategorized'
Traceback (most recent call last):
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool.py", line 31, in <module>
    import esptool
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/__init__.py", line 41, in <module>
    from esptool.cmds import (
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/cmds.py", line 14, in <module>
    from .bin_image import ELFFile, ImageSegment, LoadFirmwareImage
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/bin_image.py", line 14, in <module>
    from .loader import ESPLoader
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/loader.py", line 30, in <module>
    import serial
ModuleNotFoundError: No module named 'serial'
exit status 1
Error compiling for board ESP32 Dev Module.

Update with blink example:

/*
 * https://circuits4you.com
 * ESP32 LED Blink Example
 * Board ESP23 DEVKIT V1
 * 
 * ON Board LED GPIO 2
 */

#define LED 2

void setup() {
  // Set pin mode
  pinMode(LED,OUTPUT);
}

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

Returns:

WARNING: Category '' in library ESP Insights is not valid. Setting to 'Uncategorized'
WARNING: Category '' in library ESP RainMaker is not valid. Setting to 'Uncategorized'
WARNING: Category '' in library WiFiProv is not valid. Setting to 'Uncategorized'
Traceback (most recent call last):
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool.py", line 31, in <module>
    import esptool
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/__init__.py", line 41, in <module>
    from esptool.cmds import (
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/cmds.py", line 14, in <module>
    from .bin_image import ELFFile, ImageSegment, LoadFirmwareImage
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/bin_image.py", line 14, in <module>
    from .loader import ESPLoader
  File "/home/ubuntu/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/loader.py", line 30, in <module>
    import serial
ModuleNotFoundError: No module named 'serial'
exit status 1
Error compiling for board ESP32 Dev Module.
5
  • ModuleNotFoundError doesn't sound like C++ error and you don't have any serial in your code (only Serial and as C++ is case sensitive, it's not the same)
    – KIIV
    Commented May 3 at 10:49
  • Please include the full output in your question. This sounds like a problem with the ESP32 tools, which handle the upload.
    – chrisl
    Commented May 3 at 10:55
  • copy the exact error here. I guess it is an error from the upload tool. it uses python. so it is a problem with the python installation. It has nothing to do with your sketch. did you try to upload Blink example? as a Stack Overflow user you should already know to do some basic troubleshooting.
    – Juraj
    Commented May 3 at 11:20
  • Justdonit @chrisl ;) Commented May 3 at 11:55
  • You're right @Juraj I tried with the blink example and it's definitely something different that the code that creates this error. I didn't thought it could be the upload... Maybe its my IDE Version? Commented May 3 at 12:00

1 Answer 1

0

Try pip install pyserial

arduino-esp32 uses esptool which is written in Python and uses pyserial. So you are probably getting this error on upload, not compiling. Maybe it is neither packaged in the esp32 tool files nor installed on your user/system python packages.

3
  • please search for duplicates before writing an answer
    – Juraj
    Commented May 3 at 15:25
  • @Juraj if you found one, close it as a duplicate.
    – timemage
    Commented May 3 at 21:16
  • this one was really easy to find
    – Juraj
    Commented May 4 at 3:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.