OneWire Library + ESP32

Hello, I am currently trying to get a DS18B20 temperature sensor to work on my esp32. However I get a compilation error.
here is the code :

#include <OneWire.h>
#include <DallasTemperature.h>

// GPIO where the DS18B20 is connected to
const int oneWireBus = 32;     

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup() {
  // Start the Serial Monitor
  Serial.begin(115200);
  // Start the DS18B20 sensor
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
  delay(5000);
}

Here is the error :

In file included from c:\Arduino\libraries\OneWire\OneWire.cpp:144:
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h: In function 'uint32_t directRead(uint32_t)':
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:134:17: error: 'GPIO' was not declared in this scope
  134 |         return (GPIO.in >> pin) & 0x1;
      |                 ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:136:17: error: 'GPIO' was not declared in this scope
  136 |         return (GPIO.in1.val >> (pin - 32)) & 0x1;
      |                 ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h: In function 'void directWriteLow(uint32_t)':
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:149:9: error: 'GPIO' was not declared in this scope
  149 |         GPIO.out_w1tc = ((uint32_t)1 << pin);
      |         ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:151:9: error: 'GPIO' was not declared in this scope
  151 |         GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32));
      |         ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h: In function 'void directWriteHigh(uint32_t)':
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:162:9: error: 'GPIO' was not declared in this scope
  162 |         GPIO.out_w1ts = ((uint32_t)1 << pin);
      |         ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:164:9: error: 'GPIO' was not declared in this scope
  164 |         GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32));
      |         ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h: In function 'void directModeInput(uint32_t)':
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:187:13: error: 'GPIO' was not declared in this scope
  187 |             GPIO.enable_w1tc = ((uint32_t)1 << pin);
      |             ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:189:13: error: 'GPIO' was not declared in this scope
  189 |             GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32));
      |             ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h: In function 'void directModeOutput(uint32_t)':
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:213:13: error: 'GPIO' was not declared in this scope
  213 |             GPIO.enable_w1ts = ((uint32_t)1 << pin);
      |             ^~~~
c:\Arduino\libraries\OneWire\util/OneWire_direct_gpio.h:215:13: error: 'GPIO' was not declared in this scope
  215 |             GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32));
      |             ^~~~

exit status 1

Compilation error: exit status 1

I am using esp32 dev module.
I tried different versions of oneWire.h and dallastemperature.h without success.

Does anyone have an idea to help me ?

I moved your topic to a more appropriate forum category @pistache351.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

For my 30-pin ESP32 and DS18B20 seonsor, your sketch is compiled alright, uploaded fine, and shows correct room temperature readings on Serial Monitor.

31.50ºC
88.70ºF
31.50ºC
88.70ºF
31.50ºC
88.70ºF

Hi @pistache351.

Explanation

Some significant changes were made in the recently released version 3.0.0 of the "esp32" boards platform that adds support to Arduino IDE for your ESP32 Dev Module board.

One of those changes caused the new version of the "esp32" platform to be incompatible with the "OneWire" library.

The bug in the "OneWire" library has already been fixed:

However, there hasn't been a release of the "OneWire" library since the time of that fix, so the release version of the library you get from the Arduino IDE Library Manager still contains the bug.

Resolution

For now, the most simple solution will be to use the development version of the "OneWire" library that contains the bug fix.

I'll provide instructions you can follow to do that:

A. Uninstall the release version of the "OneWire" library

  1. Select Sketch > Include Library > Manage Libraries... from the Arduino IDE menus to open the "Library Manager" view in the left side panel.
  2. Type OneWire in the "Filter your search..." field.
  3. Scroll down through the list of libraries until you see the "OneWire" entry.
  4. Hover the mouse pointer over the "OneWire" entry.
  5. You will see a ●●● icon appear near the top right corner of the library entry. Click on that icon.
    A context menu will open.
  6. Select "Remove" from the context menu.
    An "Uninstall" dialog will open.
  7. Click the "YES" button in the "Uninstall" dialog to confirm that you want to uninstall the library.
    The dialog will close.
  8. Wait for the uninstall process to finish, as indicated by a notification at the bottom right corner of the Arduino IDE window:

    ⓘ Successfully uninstalled library ...

B. Install development version of "OneWire" library

  1. Click the following link to open the library's GitHub repository homepage in your web browser:
    https://github.com/PaulStoffregen/OneWire
  2. Click the "Code ▾" button you see on that page.
  3. Select Download ZIP from the menu.
    A download of the ZIP file of the library will start.
  4. Wait for the download to finish.
  5. Select Sketch > Include library > Add .ZIP Library from the Arduino IDE menus.
    The "Select the zip file containing the library you'd like to add" dialog will open.
  6. Select the downloaded file from the dialog.
  7. Click the "Open" button.
    The dialog will close.
  8. Wait for the installation process to finish, as indicated by a notification at the bottom right corner of the Arduino IDE window:

    ⓘ Successfully installed library from ...

Now try compiling or uploading your sketch again. Hopefully this time you won't encounter any errors and everything will work as expected.

What is the actual rate of issue-reports per week for this ESP32-core-Version 3.0.0 ?

thanks a lot for your help.

You are welcome. I'm glad if I was able to be of assistance.

Regards,
Per

For the benefit of anyone else who experiences this problem and finds this topic during their research for a solution, I have an update on the subject:

I had made a comment on one of the related bug reports over on the "OneWire" library earlier while I was investigating the problem reported by @pistache351. That comment prompted the maintainer of the library (Paul Stoffregen) to make a new release of the library with the fix I mentioned previously.

So if anyone else experiences this problem, you can now simply use the Arduino IDE Library Manager to install version 2.3.8 of the library to solve it, instead of performing the more complicated development version installation procedure that was necessary at the time I wrote the instructions in post #4.


@pistache351 the 2.3.8 release version is identical to the development version you installed by following those instructions, so you don't need to take any action to update the development version installation.

Thank you for providing this information! This saved me quite a bit of time today.