I am using an Arduino Mega to read a DS18B20 temperature sensor using the OneWire library in the code below which I got here:

#include <OneWire.h>

#define DS18S20_Pin 20 //DS18S20 Signal pin on digital 20

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 20

 #define DS18S20_ID 0x10
 #define DS18B20_ID 0x28
 float temp;

void setup(void) {

void loop(void) {
  boolean tempTest = getTemperature();

  delay(1000); //just here to slow down the output so it is easier to read


 boolean getTemperature(){
 byte i;
 byte present = 0;
 byte data[12];
 byte addr[8];
 //find a device
 if (!ds.search(addr)) {
 return false;
 if (OneWire::crc8( addr, 7) != addr[7]) {
 return false;
 if (addr[0] != DS18S20_ID && addr[0] != DS18B20_ID) {
 return false;
 // Start conversion
 ds.write(0x44, 1);
 // Wait some time...
 present = ds.reset();
 // Issue Read scratchpad command
 // Receive 9 bytes
 for ( i = 0; i < 9; i++) {
 data[i] = ds.read();
 // Calculate temperature value
 temp = ( (data[1] << 8) + data[0] )*0.0625;
 return true;

When I use this code with the sensor attached to pins digital pins 20 or 21 it reads successfully, ok great, somehow by the grace of god I chose pin 20 on the first try. So then I move on to integrating my next sensor which utilizes the I2C and the Wire library, I should note that I am using this shield so I was able to connect right to the I2C without realizing I was using pins 20 and 21. Now, when I combined the two programs the I2C sensor was initialized second so it ended up getting the pins and the DS18B20 would fail to read, after pulling out my hair for a couple of hours I looked at a Mega pinout and realized it was on the I2C pins and thinking I finally figured it out I went to go and move the DS18B20 to one of the 52 other digital pins, this is when I came to the cold discovery that my DS18B20/OneWire will not work on any pins except pins 20 and 21 which I NEED for I2C. I have not checked every single digital pin but I have checked about half of them and nothing works except pins 20 and 21, I cant figure it out, nothing I can find mentions that OneWire uses or conflicts with the I2C connection.

More Info:

  • Using latest OneWire

  • I am not using DallasTemperature and would prefer not to, I could not get it to work whether on pins 20/21 or not

  • I am using a resistor on the sensor

  • I am currently only trying to run the code above to debug, no references to I2C sensor or Wire library whatsoever.

Thanks for reading and for any help, I am totally stumped here and thought I was avoiding some of these dumb double use pin issues by using the Mega instead of an Uno...

  • it is unclear why grace of god was required to choose pin 20 ... the comment in the code listing clearly specifies pin 20
    – jsotola
    Aug 2 '19 at 6:14
  • Original code at link did not specify a pin number it only included a function. So I just chose pin 20, which ended up being one of the two working ones. Aug 2 '19 at 18:39
  • The difference between pin 20, 21 and the rest of the pins is the pullup resistor on the mega board. I assume that there is something wrong with your pullup resistor for the 1-Wire bus. Its value is far too large or it is not connected.
    – Jot
    Aug 3 '19 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.