CapacitiveSensor library with esp8266 (node mcu)

I am having some issues with the capacitive sensor library and my node MCU board. (I believe that it is the v 1.0, and possibly a clone).
I got some errors that I don't quite understand how to fix while attempting to verify the code.

Here is the code I used:

#include <CapacitiveSensor.h>

CapacitiveSensor   rightPad = CapacitiveSensor(12,13);        // 2M resistor between pins 12 & 13, pin 13 is sensor pin 
CapacitiveSensor   leftPad = CapacitiveSensor(12,15);        // 2M resistor between pins 12 & 15, pin 15 is sensor pin
const int rightSensorThreshold = 450; //threshold at which to to recognize input
const int leftSensorThreshold = 450;

void setup() {
  Serial.begin(9600);
}

void loop() {
  long total1 = rightPad.capacitiveSensor(30);
  long total2 = leftPad.capacitiveSensor(30);
  if (rightSensorThreshold > 450) { //prints R if right sensor is touched
    Serial.println("R");
  } 
  if (leftSensorThreshold > 450) { //prints L if left sensor is touched
    Serial.println("L");
  }
  
}

Here is the error message I got:

Arduino: 1.8.15 (Mac OS X), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"











/Users/<myusername>/Documents/Arduino/libraries/CapacitiveSensor/CapacitiveSensor.cpp: In member function 'long int CapacitiveSensor::capacitiveSensor(uint8_t)':
/Users/<myusername>/Documents/Arduino/libraries/CapacitiveSensor/CapacitiveSensor.cpp:77:75: error: call of overloaded 'abs(long unsigned int)' is ambiguous
   77 |   if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total  - leastTotal) < (int)(.10 * (float)leastTotal) ) {
      |                                                                           ^
In file included from /Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/cstdlib:75,
                 from /Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/stdlib.h:36,
                 from /Users/<myusername>/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/Arduino.h:27,
                 from /Users/<myusername>/Documents/Arduino/libraries/CapacitiveSensor/CapacitiveSensor.cpp:13:
/Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/stdlib.h:74:5: note: candidate: 'int abs(int)'
   74 | int abs (int);
      |     ^~~
In file included from /Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/cstdlib:77,
                 from /Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/stdlib.h:36,
                 from /Users/<myusername>/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/Arduino.h:27,
                 from /Users/<myusername>/Documents/Arduino/libraries/CapacitiveSensor/CapacitiveSensor.cpp:13:
/Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/Users/<myusername>/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/3.0.4-gcc10.3-1757bed/xtensa-lx106-elf/include/c++/10.3.0/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).


This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

After staring at this for a few minutes, it appears that it has some sort of issue with the capacitive sensor library, which strikes me as odd because it says in the documentation that capacitive sensor is compatible with all architectures, and I have seen other people be successful with this board.

If anyone knows a workaround/method to get this library to work with this board, it would be much appreciated.

Thanks for the help!

Posting a link to the datasheet of the sensor might interest more helpers.

A desperate guess would be to insert a line like this:

#include <math.h>

Sorry if I was unclear here. I am not using an off-the-shelf sensor for this project, but rather the CapacitiveSensor library by Paul Stoffregen. This allows me to create my sensors using a piece of metal and a couple of resistors.

So no datasheets whatsoever. Good luck. I'm signing off.

I think my post was poorly phrased, but I'm not sure how to fix that. The issue that I'm having is either with the library or the MCU that I am using, not the stuff connected to it, as I've gotten this to work with an Arduino Uno. Thank you for your help.

Hello cf98.
I managed to get it compiled by opening the library file "CapacitiveSensor.cpp" and removing the "abs" text from line 77. Save it and try again. Maybe you will have to close and reopen the Arduino IDE.
It should look like this:

if ( (millis() - lastCal > CS_AutocaL_Millis) && (total - leastTotal) < (int)(.10 * (float)leastTotal) ) {

2 Likes

So you have not come across the "Capsense" library for the Arduino.

It, or something like it, is the basis of the Ardutester, of which I have a few of the ready-made versions (which naturally do not in any way resemble a UNO :grin:).

They work quite well.

I did try writing some capacitive sensing programs just in the basic Arduino IDE but it proves to be hopeless. You (not surprisingly) must use assembler for this sort of thing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.