DS18B20 OneWire GPIO Not Declared In Scope

Hi there, I am trying to test out the DS18B20 temperature sensor and I come across this GPIO error when I am trying to test it.
Here is my code:

#include <DallasTemperature.h>

#include <OneWire.h>

// Data wire is conntec to the Arduino digital pin 13
#define ONE_WIRE_BUS 13

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

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

void setup(void)
{
  // Start serial communication for debugging purposes
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
}

void loop(void){ 
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  
  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  Serial.print(sensors.getTempCByIndex(0)); 
  Serial.print(" - Fahrenheit temperature: ");
  Serial.println(sensors.getTempFByIndex(0));
  delay(1000);
}

Here is the error:

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

exit status 1

Compilation error: exit status 1

Pretty new to arduino and everything and I've not found anything similar when googling - any help would be greatly appreciated!

Mushlove

Go into tools and select the board you are using.

Ah yep, so I changed my board and it compiles thank you!! But now it is giving bogus results... :frowning:

Celsius temperature: -127.00 - Fahrenheit temperature: -196.60

No matter how much I warm the sensor up, it doesn't change. Any clue on why this might be?

You changed it from what to what?

From Adafruit ESP32 Feather to ESP32 Dev Module

I have a 10k in place of the 4.7k which is standard. I read that 10k should be fine in most situations

Are you using 30-pin ESP32 Board? If not, please post the picture of your ESP32 Board.

Please see the pic attached

Good tutorial here. I think 10k is too
High as you are running from 3v, 3k9 ??

Here

Try running the following sketch which seems to be working with my 30-pin ESP32 and 4k pull-up.

//12-bit default resolution; external power supply

#include<OneWire.h>

OneWire ds(13);
byte addrs[8];         //to hold 64-bit ROM Codes of DS1
float celsius;
byte data[9];

void setup()
{
  Serial.begin(9600);
  ds.reset();  //sensor reset
  ds.search(addrs);  //collect 64-bit ROM code from sensor (DS1)
}

void loop()
{
  ds.reset();       //bring 1-Wire into idle state
  ds.select(addrs); //slect with DS-1 with address addr1
  ds.write(0x44);    //conversion command
  delay(750);   //maximum conversion delay
  //---------------------------
  ds.reset();
  ds.select(addrs);  //selectimg the desired DS18B20
  ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
  ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
  //---------------------------------
  //data[0] contains lower 8-bit of temperaure
  //data[1] contains upper 8-bit of temperature

  float fracTemp = bitRead(data[0], 3) * 0.5 + bitRead(data[0], 2) * 0.25
                   + bitRead(data[0], 1) * 0.125 + bitRead(data[0], 0) * 0.0625;
  int integerTemp = bitRead(data[1], 2) * 64 + bitRead(data[1], 1) * 32
                    + bitRead(data[1], 0) * 16 + bitRead(data[0], 7) * 8
                    + bitRead(data[1], 6) * 4 + bitRead(data[1], 5) * 2 + bitRead(data[1], 0) * 1;

  celsius = (float)integerTemp + fracTemp;
  //int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  //celsius = (float)raw / 16.0;  //12-bit resolution
  Serial.println(celsius, 4);
}

Thank you very much, as it turns out it was my ESP32! I'm not sure why, but when I swapped it out for another, it worked both with a 10k and I also luckily found a 4.7k which worked.

Thank you for your help, although I managed to get it working now with the same script, just by swapping out ESP32s lol

Possibly a bad connection to the bread board.