0
votes

When measuring negative temperatures does not show me the correct values on my led output. I see something like 4983. I need advice with my code.

I using arduino uno. Type of senzor: DS18B20

Code:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display (OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp [] =
{B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000};

#if (SSD1306_LCDHEIGHT! = 64)
#error ("Error, Adafruit_SSD1306.h!");
#endif
#include <OneWire.h>
OneWire ds (2);

void setup () {
display.begin (SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay ();

}

void loop () {


byte i;
byte present = 0;
byte data[12];
byte addr[8];

if (! ds.search (addr)) {
Serial.print ("\ n");
ds.reset_search ();
return;
}

ds.reset ();
ds.select (addr);
ds.write (0x44,1); 
delay (1000); 
ds.reset ();
ds.select (addr);
ds.write (0xBE); 

for (i = 0; i <9; i ++) {
Data [i] = ds.read ();
}

Serial.print ("CRC ="); 
Serial.print (OneWire :: CRC8 (data, 8), HEX);
Serial.println ();
float Tc_100;
Tc_100 = (data [1] * 256. + data [0]) / 16 .;

Serial.print (Tc_100);


display.clearDisplay ();
display.setCursor (5,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("TEMPERATURE"); 
display.setCursor (65,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("MOTORA"); 
display.setCursor (109,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("O"); 
display.setCursor (115,5);
display.setTextSize (1);
display.setTextColor (WHITE);
display.println ("C"); 
display.setCursor (30,28);
display.setTextSize (4);
display.setTextColor (WHITE);
display.print (Tc_100, 1); 
display.display ();
delay(1000);
}
1
Welcome to Stack Overflow! You might want to check out how to ask a question. Formatting your question correctly will go a long way to getting you the answer you are looking for.Gary Storey

1 Answers

0
votes

First, change:

Data [i] = ds.read ();

to

data[i] = ds.read();

And then see what kind of results you get.

I'm not sure about your calculations after that. I use the following code to convert the nine hex values to temperature for the DS18B20:

  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) { // negative
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;


  if (SignBit) { // If its negative
     Serial.print("-");
  }
  Serial.print(Whole);
  Serial.print(".");
  if (Fract < 10) {
     Serial.print("0");
  }
  Serial.print(Fract);

  Serial.print("\n");

See OneWire Temperature