Issues displaying DS18B20 Temps to OLED

MC: Arduino Mega 2560 REV3
OLED: Waveshare 1.5inch RGB OLED 128x128
Sensors: Gikfun DS18B20 (2)

Libraries:

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

Relevant Code:

Sensor addresses, defines, etc.

#define OneWireBus 48

// Temp sensor Gikfun DS18B20
OneWire temp(OneWireBus);
DallasTemperature sensors(&temp);

// Temp sensors addr
// Probe01 - 0x28, 0x7E, 0x38, 0x95, 0xF0, 0x01, 0x3C, 0x1B
DeviceAddress Probe01 = { 0x28, 0x7E, 0x38, 0x95, 0xF0, 0x01, 0x3C, 0x1B };
// Probe02 - 0x28, 0x89, 0xED, 0x95, 0xF0, 0x01, 0x3C, 0x60
DeviceAddress Probe02 = { 0x28, 0x89, 0xED, 0x95, 0xF0, 0x01, 0x3C, 0x60 };

Setup:

void setup(void) {
    Serial.begin(9600);
    tft.begin();
    sensors.begin();
    sensors.setResolution(Probe01, 10);
    sensors.setResolution(Probe02, 10);

    tft.fillScreen(BLACK);
    tft.setTextColor(WHITE, BLACK);
}

Loop:

void loop() {
    Serial.println();
    Serial.print("Num Probes: ");
    Serial.print(sensors.getDeviceCount());
    Serial.println();

    sensors.requestTemperatures();

    char probe01Buffer[4];
    char probe02Buffer[4];
    char avgBuffer[4];
    
    /////////////////MY ISSUE IS HERE/////////////////
    // get probe01 temp
    float temp1 = getTemperature(Probe01);
    char* temp1Char = dtostrf(temp1, 4, 1, probe01Buffer);
    Serial.println(temp1);
    /////////////////MY ISSUE IS HERE/////////////////
    
    // get probe02 temp
    float temp2 = getTemperature(Probe02);
    char* temp2Char = dtostrf(temp2, 4, 1, probe02Buffer);
    Serial.println(temp2);
        
    // calculate avg temp
    float tempAvg = (temp1 + temp2) / 2;
    char* tempAvgChar = dtostrf(tempAvg, 4, 1, avgBuffer);
    Serial.println(tempAvg);
    
    tft.setCursor(0,0);
    tft.print("Temp1: ");
    tft.print(temp1Char);
    
    tft.setCursor(0,8);
    tft.print("Temp2: ");
    tft.print(temp2Char);
    
    tft.setCursor(0,16);
    tft.print("TempAvg: ");
    tft.print(tempAvgChar);
    
    delay(5000);
}

getTemperature:

float getTemperature(DeviceAddress deviceAddress) {
    float tempF = sensors.getTempF(deviceAddress);
    if (tempF == -127.00) {
        Serial.print("Temp Error");
    } else {
        return tempF;
    }
}

Issue:
So, everything is working just the way I want it to right now. All I'm doing is taking temperatures from the two sensors I have connected, and displaying them on the OLED, along with an average of the two readings.

The problem is that the first temperature reading just doesn't appear on the OLED at all - despite the fact that it appears in my Serial Monitor perfectly. The other temperature reading shows, and the average also shows.

I'm sure this is just some confusion on my part about how I am using character arrays/pointers (i.e. probe01Buffer, temp1Char) to send the information to the display, but I'm a bit lost on how to get it working.

Any help would be much appreciated, thank you!

Welcome

Your arrays are too small to store 5 characters "XX.X\0", and possibly 6 characters if the value can be negative

You don't need to store the return value of dtostrf in a new char *

char probe01Buffer[6];
...
float temp1 = getTemperature(Probe01);
dtostrf(temp1, 4, 1, probe01Buffer);
Serial.println(probe01Buffer);
...
tft.setCursor(0,0);
tft.print("Temp1: ");
tft.print(probe01Buffer);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.