Trying to read DS1820 connected to analog port

Hi all,

I have always used it with DS18B20 temperature sensor withou any problem (in both arduino uno or mega), but now I need to read DS1820 (with arduino mega) and the people who assembled the sensor did it in the wrong place and plugged it to an analog port (particularly A2 which correspond to 56 digital line, in case of arduino mega). In any case, as far as I understood I can configure any analog port as digital, so I did it but I always get 0 value. It is important to comment that Im using DallasTemperature library, and here is my code:

void setup()
{

Serial.begin(9600);
//Temperature sensor pin A2 (ONE_WIRE_BUS), as input
pinMode(ONE_WIRE_BUS, INPUT);
//Activate pull up resistor
digitalWrite(ONE_WIRE_BUS, HIGH);
sensors.begin();
}

void loop()
{
sensors.requestTemperatures();

float temp = sensors.getTempCByIndex(0);

Serial.print(temp);

delay(1000);
}

And the physical connection is VDD connected to 5V with a resistor and a capacitor (1 uF), DQ connected to A2 pin and ground pin to ground.

Do you have any idea why could this be happening? :frowning:

It is really weird as with the same code, and connecting the sensor to a digital line, the DS18S20 works properly.

Thank you very much in advance,

Never saw anyone, or tried myself to, utilize the internal pullup for this. I tried to find a reference but failed -- I seem to recall only using external pullup would work for some reason. Maybe the internal pullup cannot provide sufficient current?

Maybe " the people who assembled the sensor" are not really telling you what they did, so check the connections

pos to 5v
neg to gnd
sig to analogue pin and to 5v via a 4k7 pullup.

The DS18B20 works fine on an analogue pin - provided it is wired correctly.

If you are connecting the DS18B20 to the analog port
Is it possible to get the temperature reading without using the library

If so...pl post the codes

DS18B20 uses a "one wire " serial protocol, you need the Wire library as a minimum, don't know if it will work on an analog input pin.

Just tried it on analog pin A1 (pin 15 on UNO) and does work. Didn't know that.

1 Like

Is it possible to put simple conversion factors for the value obtained from the analog port.

The DS18B20 does not output an analog signal, only digital, you still need the Wire library to communicate with it, what kind of conversion factor do you mean?
NOTE: The Wire library is included in the Arduino IDE.

EDIT: I should have said OneWire.

The data sheet:

datasheets.maximintegrated.com/en/ds/DS18B20.pdf

An example sketch:

#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(2);  // on pin 2

void setup(void) {
  Serial.begin(9600);
  Serial.println("DS18x20 \n");
  delay(5000);
}

void loop(void)
{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr))
  {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("ROM =");
  for( i = 0; i < 8; i++)
  {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7])
  {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0])
  {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
  
  delay(5000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++)
  {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // convert the data to actual temperature

  unsigned int raw = (data[1] << 8) | data[0];
  if (type_s)
  {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10)
    {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else
  {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
}

DS18x20.ino (2.76 KB)

That code must be about the most useless junk in the Arduino galaxy. Even the people who wrote it know it's junk, but the line therein

// The DallasTemperature library can do all this work for you!

is worth noting and acting upon.

You will get something sensible by people who know what they are doing here

Any digital device can be used on an analogue pin but it still works digitally. You can use the DS18B20 without the one-wire facility but, other than having a greater need to save memory than pins, I don't think it's a good idea.

Analog is only an extra function of the pin just like I2C and PWM is on some others. Except when it explicitly stated it's only analog (like A7 on a Nano/Pro Mini).

So the fact it's connected to A2 is not a problem at all if you can miss a analog port.

Nick_Pyner:
That code must be about the most useless junk in the Arduino galaxy. Even the people who wrote it know it's junk, but the line therein

// The DallasTemperature library can do all this work for you!

is worth noting and acting upon.

You will get something sensible by people who know what they are doing here

Arduino 1-Wire Tutorial

Any digital device can be used on an analogue pin but it still works digitally. You can use the DS18B20 without the one-wire facility but, other than having a greater need to save memory than pins, I don't think it's a good idea.

One or more DS18B20 temperature sensors on Arduino- ar3ne1tt2.htm

Thanks Nick_Pyner for your link

I made use of the link that does not make use of the library and I was successful.
Also withe LoRa module, I was able to send the data wireless from the arduino to remote computer
(The LoRa module and the interfaceq board on which it is mounted do not support One Wire or any arduino libraries.....your link was quite helpful)