SparkFun LIS331 library not compatible with ESP32 library

Hi everybody

I am having problems with using the SparkFun H3LIS331DL accelerometer sensor with my Wemos Lolin32 Arduino board. I have correctly installed the espressif ESP32 board library so that I can use the correct arduino board (Wemos Lolin32) inside the Arduino IDE and I have also downloaded the correct library for the SparkFun H3LIS331DL accelerometer sensor which is the SparkFun LIS331 library as indicate on the SparkFun H3LIS331DL Hookup guide.

The problem I am having is that when compiling any of the examples included in the SparkFun LIS331 library in the Arduino IDE for my Arduino board (Wemos Lolin32) I get compiling errors (The errors is included below). When I use any other board outside of the Espressif ESP32 board library (e.g. Arduino/Genuino Uno) I do not get these compiling errors. Thus, these errors only exists when I use an Arduino board that is based on the ESP32 (E.g. ESP32 Dev Module, Nano32, Wemos Lolin32). My guess is that this something to do with the fact that the Espressif ESP32 board library comes with its own hardware library which inlude SPI.h, Wire.h etc.

Can anyone help me with this problem? I am stuck with the fact that I MUST use the Wemos Lolin32 Arduino board.

Included below is the compiling errors I receive.

I have attached the example code in the SparkFun LIS331 library that causes these compiling errors and also the SparkFun_LIS331.h and SparkFun_LIS331.ccp file

[COMPILING ERRORS]==================================================

Arduino: 1.8.0 (Windows 10), Board: “WEMOS LOLIN32, 80MHz, Default, 921600”

In file included from C:\Users\Johann\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.0\cores\esp32/esp32-hal.h:53:0,

from C:\Users\Johann\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.0\cores\esp32/Arduino.h:35,

from sketch\LIS331_I2C_Example.ino.cpp:1:

C:\Users\Johann\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.0\cores\esp32/esp32-hal-gpio.h:39:27: error: expected identifier before numeric constant

#define OPEN_DRAIN 0x10

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:39:28: note: in expansion of macro ‘OPEN_DRAIN’

typedef enum {PUSH_PULL, OPEN_DRAIN} pp_od;

^

C:\Users\Johann\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.0\cores\esp32/esp32-hal-gpio.h:39:27: error: expected ‘}’ before numeric constant

#define OPEN_DRAIN 0x10

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:39:28: note: in expansion of macro ‘OPEN_DRAIN’

typedef enum {PUSH_PULL, OPEN_DRAIN} pp_od;

^

C:\Users\Johann\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.0\cores\esp32/esp32-hal-gpio.h:39:27: error: expected unqualified-id before numeric constant

#define OPEN_DRAIN 0x10

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:39:28: note: in expansion of macro ‘OPEN_DRAIN’

typedef enum {PUSH_PULL, OPEN_DRAIN} pp_od;

^

In file included from C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\Examples\LIS331_I2C_Example\LIS331_I2C_Example.ino:1:0:

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:46:10: error: expected unqualified-id before ‘)’ token

LIS331(); // Constructor. Defers all functionality to .begin()

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:47:14: error: variable or field ‘begin’ declared void

void begin(comm_mode mode);

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:47:14: error: ‘comm_mode’ was not declared in this scope

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:51:21: error: variable or field ‘setPowerMode’ declared void

void setPowerMode(power_mode pmode);

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:51:21: error: ‘power_mode’ was not declared in this scope

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:52:15: error: variable or field ‘setODR’ declared void

void setODR(data_rate drate);

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:52:15: error: ‘data_rate’ was not declared in this scope

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:56:25: error: variable or field ‘setHighPassCoeff’ declared void

void setHighPassCoeff(high_pass_cutoff_freq_cfg hpcoeff);

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:56:25: error: ‘high_pass_cutoff_freq_cfg’ was not declared in this scope

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:60:19: error: variable or field ‘intPinMode’ declared void

void intPinMode(pp_od _pinMode);

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:60:25: error: expected ‘)’ before ‘_pinMode’

void intPinMode(pp_od _pinMode);

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:72:3: error: expected unqualified-id before ‘private’

private:

^

C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h:79:1: error: expected declaration before ‘}’ token

};

^

exit status 1
Error compiling for board WEMOS LOLIN32.

[COMPILING ERRORS]==================================================

LIS331_I2C_Example.ino (2.78 KB)

SparkFun_LIS331.h (2.7 KB)

SparkFun_LIS331.cpp (8.13 KB)

The problem is a name conflict between the OPEN_DRAIN macro defined in the ESP32 core library and the enum of the same name defined in the Sparkfun_LIS331 library.

To fix it, modify the library to use a unique name, following these instructions:

  1. Open C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.h in a text editor.
  2. Change line 39 from:
    typedef enum {PUSH_PULL, OPEN_DRAIN} pp_od;
    
    to:
    typedef enum {PUSH_PULL, SPARKFUN_LIS331_OPEN_DRAIN} pp_od;
    
  3. Save the file.
  4. Open C:\Users\Johann\Documents\Arduino\libraries\SparkFun_LIS331\src/SparkFun_LIS331.cpp in a text editor.
  5. Change line 39 from:
    if (_pinMode == OPEN_DRAIN)
    
    to:
    if (_pinMode == SPARKFUN_LIS331_OPEN_DRAIN)
    
  6. Save the file.
1 Like

Thank you very much Pert. It worked perfectly. I honestly didn't think the solution would have been that easy. Thank you for your quick reply as well.

You’re welcome. I’m glad to hear it’s working now. Enjoy!
Per