DS18S20 wrong

Hi !

I had 5 DS1820 working well and I bought 10 new pieces sold as DS18S20 (marked DS1820)

They all give very wrong values. I carefully read the datasheet and found no explanations.

At room temp (about 20°C) I have a deep neg value (here about -38°C)

byte 0 CRC OK
11001101 11111111 00000000 00000000 11111111 11111111 01000000 00001000 00011001

Value read with the old one
10010100 00000000 11010010 01100010 11111111 11111111 10000000 00001000 10011010

Is it just bad units (and bad seller) ?

Thank You for help

Neither of those can be valid outputs from DS18S20 or DS18B20 because, for both of them, the CRC is incorrect and the byte immediately before the CRC is hardwired in both devices to send 0x10, not 0x08.

Pete

Thx for your interest

In fact lsb is left due to shift method of data acquisition, so crc is ok and byte 7 of scratchpad is ok too.

There is no DS18B20 here the new one (with wrong values) was sold as 18S20, old one was sold as 1820 ; both are marked with DS1820.

In fact lsb is left due to shift method of data acquisition

It would have saved me some time if you'd mentioned that in your first post. Which library/software are you using to read the DS18S20?

Pete

Sorry...

I just made a copy from the Serial.print bits outputs. I use my own software.
That is working well with the old units and CRC is OK in both cases, so ...

Here is cpp, if you need .h file I can copy it too :slight_smile:

/*   
    Interrupt-free, small memory wasting ds1820 interface (about 1200 bytes)
    this is working with any pin who allow digitalRead and digitalWrite.
    Interrupt suspend should not be longer than 85 uSec for each written bit and
    less than 10uSec for the other occurencys.

    2 steps : first converting (could be as long as 750 mSec) then get value.

    convertDs(uint8_t pin) returns 1 or error codes (see below)
    readDs(uint8_t pin) returns float value between -55 and +125 or error codes (see below)
    
    getDs(uint8_t pin,uint8_t* frameout,uint8_t nbbyteout,uint8_t* framein,uint8_t nbbytein)
    return a status (codes below) and fill framein with data provided by Ds in accordance
    with the frameout data transmitted to Ds.
    (see datasheet)
    
    pin is the Arduino pin number used to connect Ds unit
*/

#include "Arduino.h"
#include "Ds1820.h"

// possible return status for getDs() function
// values between -55 to +125 are valid data

#define TOPRES -100
#define CRC_ERR -101 // CRC check not implemented

#define T_WAIT_PRESENCE 240

Ds1820::Ds1820()  
{
}

byte calcBitCrc(byte shiftReg, byte data_bit);
static uint8_t maskbit[]={1,2,4,8,16,32,64,128};  //  128,64,32,16,8,4,2,1


void writeDs(uint8_t pin,uint8_t nbbyteout,uint8_t* frameout)
//write command nbbyteout @ nbbit/byte
  
{  
  boolean pinstat=HIGH;
  uint8_t i=0,j=0,delaylow,delayhigh;
    
  digitalWrite(pin,HIGH);
  pinMode(pin,OUTPUT);
  for(i=0;i<nbbyteout;i++){//Serial.print(frameout[i],HEX);
    for(j=0;j<8;j++){
      delaylow=80;delayhigh=0;pinstat=LOW;if((frameout[i] & maskbit[j])!=0){pinstat=HIGH;delaylow=0;delayhigh=80;}
      noInterrupts();
      digitalWrite(pin,LOW);
      // if bit =1 low state about 2,7 uSec long (UNO)
      delayMicroseconds(delaylow);digitalWrite(pin,pinstat);delayMicroseconds(delayhigh);
      digitalWrite(pin,HIGH);
      interrupts();
    }
  }
  pinMode(pin,INPUT);
  //Serial.println();
}

int Ds1820::convertDs(uint8_t pin)
{
  uint8_t h[2]={0xCC,0x44},r[2];
  return getDs(pin,h,2,r,0); 
}

float Ds1820::readDs(uint8_t pin)
{
  uint8_t cmd[2]={0xCC,0xBE},rep[9];
  int v;
  float th=0,th0=0;
  
  v=getDs(pin,cmd,2,rep,9);
  //Serial.print((int)rep[0]/2);Serial.print(" ");Serial.print(v);Serial.print(" ");Serial.print((int)rep[7]);Serial.print(" ");Serial.print((int)rep[6]);Serial.print(" ");Serial.println((float)(rep[7]-rep[6])/rep[7]);
  if(v<-100){return v;}
  else{th=(float)((int)(rep[0]/2)-0.25+(float)(rep[7]-rep[6])/rep[7]);}
  Serial.print(th);
  th0=th*10-(int)(th*10);if(th0>0.5){th+=0.1;}th=(int)(th*10);th=(float)(th/10);
  Serial.print(" ");Serial.print(th0);Serial.print(" ");Serial.println(th);
  return th;
}

int Ds1820::getDs(uint8_t pin,uint8_t* frameout,uint8_t nbbyteout,uint8_t* framein,uint8_t nbbytein)
{

  uint8_t crc=0;
  uint8_t i=0,j=0,v=0;
  long micr=0;
  uint8_t delaylow=0,delayhigh=0;
  boolean pinstat=HIGH;
  uint8_t buff[128];for(i=0;i<128;i++){buff[i]=0;}
  
  //reset pulse
  noInterrupts();
  digitalWrite(pin,HIGH);
  pinMode(pin,OUTPUT);
  interrupts();
  delay(1);
  digitalWrite(pin,LOW);
  delayMicroseconds(500);
  pinMode(pin,INPUT);
  delayMicroseconds(65);
  
  //presence detect
  micr=micros();
  while(1){
    if(micros()>(micr+T_WAIT_PRESENCE)){return TOPRES;} // pas de réponse
    if(digitalRead(pin)==LOW){delayMicroseconds(400);break;}
  }
  // write command
  writeDs(pin,nbbyteout,frameout);
  
  //read response nbbytein @ 8 bits/byte
  
  if(nbbytein!=0){ 
    memset(framein,0,nbbytein);
    for (i=0;i<nbbytein;i++){
      for (j=0;j<8;j++){
        v=i*8+j;
        noInterrupts();
        digitalWrite(pin,LOW);
        pinMode(pin,OUTPUT);
        // delay between low and read about 5,2 uSec (UNO) + delayMicroseconds
        // delayMicroseconds is n-1 micros ; pinMode about 3,1 uSec
        pinMode(pin,INPUT);delayMicroseconds(0);buff[v]=digitalRead(pin);
        interrupts();
        if(buff[v]!=0){framein[i]+= maskbit[j];}
        delayMicroseconds(50);//Serial.print(buff[v]);
      }//Serial.print(" ");
    }//Serial.println();
  }
  // check CRC
  byte shift_reg=0;
  for(i=0;i<nbbytein;i++){
    for(j=0;j<8;j++){shift_reg=calcBitCrc(shift_reg,buff[i*8+j]);
    Serial.print(buff[i*8+j]);
      }
    Serial.print(" ");
    } 
    Serial.print(" CRC ");Serial.println(shift_reg);
  if(shift_reg==0){return 1;}else{return CRC_ERR;} 
}

byte Ds1820::calcBitCrc (byte shiftReg, byte data_bit)
{
  byte fb;
  
  fb = (shiftReg & 0x01) ^ data_bit;
   /* exclusive or least sig bit of current shift reg with the data bit */
   shiftReg = shiftReg >> 1;                  /* shift one place to the right */
   if (fb==1){shiftReg = shiftReg ^ 0x8C;}    /* CRC ^ binary 1000 1100 */
   return(shiftReg); 
}

...and that's why you must put your code inside [ code ] tags.

Sorry again...

Didnt found tutorial for posting ... Should I do it again ?

Read How to post code properly. Then edit message #4 and use the </> icon to create

[code]...[/code]

tags around your code so that it is easier to read.

I would suggest that you use an established library first, such as the Dallas Semiconductor library. If it can read whatever sensor you have then there's nothing wrong with the sensors.

Pete

Dallas says .... -43,71°C ... I'm frozen

and 18,81°C with the old piece... In May that seems usual...

Try this simple sketch, for two sensors with one common 4k7 pull up resistor.
Leo..

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

OneWire oneWire(6); // datapin D6
DallasTemperature sensors(&oneWire);
float temp1, temp2;

void setup() {
  Serial.begin(115200);
  Serial.println("DS18B20 thermometer");
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  temp1 = sensors.getTempCByIndex(0);
  temp2 = sensors.getTempCByIndex(1);
  
  Serial.print("Temp1 is ");
  Serial.print(temp1, 1); // one decimal place
//Serial.print(temp1, 4); // four decimal places
  Serial.println(" C");
  Serial.print("Temp2 is ");
  Serial.print(temp2, 1);
  Serial.println(" C\n");
}

Thanks for fixing up your code.

Try this code. It identifies the onewire devices on the bus and prints their ROM address.

#include <OneWire.h>

// Identify DS18X20 sensors

// Define the OneWire bus number here
OneWire  ds(4);

// address data
byte addr[8][8];

void print_rom(byte *p)
{
  Serial.print("ROM =");
  for(int i = 0; i < 8; i++) {
    Serial.write(' ');
    if(*p < 16)Serial.print("0");
    Serial.print(*p++, HEX);
  }
  Serial.println("");  
}

byte numdev = 0;
void setup(void)
{
  Serial.begin(9600);
  Serial.println("");

  while(ds.search(addr[numdev])) {
    if (OneWire::crc8(addr[numdev], 7) != addr[numdev][7]) {
      print_rom(addr[numdev]);
      Serial.println("CRC is not valid! - ignored");
      continue;
    } 
    numdev++;
  }
  if(numdev == 0) {
    Serial.println("Nothing found");
    Serial.println();
    ds.reset_search();
    delay(250);
    while(1);
  }

  byte present = 0;
  byte type_s;
  float celsius, fahrenheit;
  
  for(int j=0;j<numdev;j++) {
    ds.depower();
    // the first ROM byte indicates which chip
    switch (addr[j][0]) {
    case 0x10:
      Serial.print("  Chip = DS18S20, ");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.print("  Chip = DS18B20, ");
      type_s = 0;
      break;
    case 0x22:
      Serial.print("  Chip = DS1822,  ");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      continue;
    } 
    print_rom(addr[j]);
  }
}

void loop(void)
{
}

Change line 6 "OneWire ds(4);" to correspond to the pin number of your onewire bus.

With 2 DS18B20 sensors on pin 4, that code prints this:

  Chip = DS18B20, ROM = 28 60 D5 00 04 00 00 CF
  Chip = DS18B20, ROM = 28 1E C2 00 04 00 00 69

Pete

How do you have those sensors wired to the Arduino? Parasite power? Pullup resistor on the data pin?

Pete

Chip = DS18S20, ROM = 10 EE 34 65 02 08 00 72

el_supremo:
Thanks for fixing up your code.

Try this code. It identifies the onewire devices on the bus and prints their ROM address.

#include <OneWire.h>

// Identify DS18X20 sensors

// Define the OneWire bus number here
OneWire  ds(4);

// address data
byte addr[8][8];

void print_rom(byte *p)
{
 Serial.print("ROM =");
 for(int i = 0; i < 8; i++) {
   Serial.write(' ');
   if(*p < 16)Serial.print("0");
   Serial.print(*p++, HEX);
 }
 Serial.println("");  
}

byte numdev = 0;
void setup(void)
{
 Serial.begin(9600);
 Serial.println("");

while(ds.search(addr[numdev])) {
   if (OneWire::crc8(addr[numdev], 7) != addr[numdev][7]) {
     print_rom(addr[numdev]);
     Serial.println("CRC is not valid! - ignored");
     continue;
   }
   numdev++;
 }
 if(numdev == 0) {
   Serial.println("Nothing found");
   Serial.println();
   ds.reset_search();
   delay(250);
   while(1);
 }

byte present = 0;
 byte type_s;
 float celsius, fahrenheit;
 
 for(int j=0;j<numdev;j++) {
   ds.depower();
   // the first ROM byte indicates which chip
   switch (addr[j][0]) {
   case 0x10:
     Serial.print("  Chip = DS18S20, ");  // or old DS1820
     type_s = 1;
     break;
   case 0x28:
     Serial.print("  Chip = DS18B20, ");
     type_s = 0;
     break;
   case 0x22:
     Serial.print("  Chip = DS1822,  ");
     type_s = 0;
     break;
   default:
     Serial.println("Device is not a DS18x20 family device.");
     continue;
   }
   print_rom(addr[j]);
 }
}

void loop(void)
{
}




Change line 6 "OneWire ds(4);" to correspond to the pin number of your onewire bus.

With 2 DS18B20 sensors on pin 4, that code prints this:


Chip = DS18B20, ROM = 28 60 D5 00 04 00 00 CF
 Chip = DS18B20, ROM = 28 1E C2 00 04 00 00 69




Pete

el_supremo:
How do you have those sensors wired to the Arduino? Parasite power? Pullup resistor on the data pin?

Pete

el_supremo:
How do you have those sensors wired to the Arduino? Parasite power? Pullup resistor on the data pin?

Pete

External pullup 4,7K resistor

I found a datasheet for the DS1820 here. Like the DS18S20, it was also identified by 0x10 as the first byte of its rom address. Maybe the timing in the original DS1820 is a bit different from the DS18S20 and that is causing the problem.

How do you power the sensors? Parasite power?

Pete

That is the datasheet I used to write the driver ; TRDV is 15uS on both units, so no change... be sure that the cause IS NOT a wrong reading. Dallas driver has the same pb.
I have only one sensor per line with 5V or 3,3V power supply and there is a pullup 4,7K resistor and 100nF capacitor directly on case pins.
Maybe I'm not te first in trouble

[/quote]

el_supremo:
I found a datasheet for the DS1820 here. Like the DS18S20, it was also identified by 0x10 as the first byte of its rom address. Maybe the timing in the original DS1820 is a bit different from the DS18S20 and that is causing the problem.

How do you power the sensors? Parasite power?

Pete

Do the chips measure at all?

max777:
That is the datasheet I used to write the driver

OK. So which on did you use? (link please)