Dallas DS18B20 Temp sensor questions

Greetings all.

I’v been playing around with these Dallas temp sensors and have followed the example codes. However, i’v managed to narrow down the code to an almost simplistic state of what i’m needing but i’m having a little hurdle i hope someone can help me with.

I’m not the best at programming yet :stuck_out_tongue: I know what i’m trying to do though. Uisng the following code:

/*

      Dallas DS18B20 experiments
      
      Taking the example 'one sensor code' and minimising it to what's needed for my purposes.
      
      Plan is to take the sensor reading and use that info to be able to be called,  to output on
      an LCD or 7-segment display.
      
*/


#include <DallasTemperature.h>    //dallas library

                                      
static const int ONE_WIRE_PIN = 12;  //data wire on pin 12

NewOneWire oneWire(ONE_WIRE_PIN);      //sets up a onewire instance
DallasTemperature tempSensor(oneWire);  //passes onewire reference to DallasTemperature

void setup(void) {
    // initialize inputs/outputs

    // start serial port
    Serial.begin(9600);

    tempSensor.begin();  //begin reading from sensor
    Serial.println("Dallas library test, minimising what's needed");
}


//function for siplaying the temperature on serial monitor

void display(DallasTemperature &sensor) {
    
    // getTemperature returns a float. 
    float temperature = sensor.getTemperature();     //gets temperature reading from sensor in degrees
    Serial.print(temperature);
    Serial.print("C\t");              // adds 'C' for celcius
    
}

//main loop

void loop(void) {
    display(tempSensor);  //displays temperature in serial monitor in celcius
    Serial.println();    //
}

Now, this works perfectly reading the temperature in degrees on the serial monitor. What i’m trying to do, (in pseudo code) is for example:

If temp = 24C
output 24C on either an LCD or 7segment.

I have the code for these respectively, i just can’t seem to pin point the variable i need to use to do this. I’v tried loads and i think it’s something small and silly.

I tried something like:

If (temperature >= 24) {

show 24 on 7-segment

}

Can someone show me the way?

:slight_smile:

output 24C on either an LCD or 7segment.
I have the code for these respectively

…so can you show the code you use to output to LCD or 7segments (so we can see why it is difficult to combine it with temperature readings)

just write lcd.print(temp); that does it for me!

This is my full code, i’m pretty sure this is a simple thing.

/*

      Dallas DS18B20 experiments
      
      Taking the example 'one sensor code' and minimising it to what's needed for my purposes.
      
      Plan is to take the sensor reading and use that info to be able to be called,  to output on
      an LCD or 7-segment display.
      
*/


#include <DallasTemperature.h>    //dallas library

                                      
static const int ONE_WIRE_PIN = 12;  //data wire on pin 12

NewOneWire oneWire(ONE_WIRE_PIN);      //sets up a onewire instance
DallasTemperature tempSensor(oneWire);  //passes onewire reference to DallasTemperature


//7-segment set up

int a = 0;            // segment a. LSB
int b = 1;            //segment b
int c = 2;            //segment c
int d = 3;            //segment d
int e = 4;            //segment e
int f = 5;            //segment f
int g = 6;            //segment g
int dp = 7;            //decimal point. MSB

int dig1 = 8;        //common anode for 1st digit
int dig2 = 9;        //common anode for 2nd digit


void setup(void) {
    // initialize inputs/outputs

    // start serial port
    Serial.begin(9600);

    tempSensor.begin();  //begin reading from sensor
    Serial.println("Dallas library test, minimising what's needed");
    
    // initialize the digital pins as an output:
  pinMode(a, OUTPUT);   
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);  
  pinMode(dig1, OUTPUT);
  pinMode(dig2, OUTPUT);
}


//function for siplaying the temperature on serial monitor

void display(DallasTemperature &sensor) {
    
    // getTemperature returns a float. 
    float temperature = sensor.getTemperature();     //gets temperature reading from sensor in degrees
    Serial.print(temperature);
    Serial.print("C\t");              // adds 'C' for celcius
    
}

//main loop

void loop(void) {
    display(tempSensor);  //displays temperature in serial monitor in celcius
    Serial.println();    
    //this is the if statement i was trying to display on 7segment, i get "temperature not declared in this scope" error.
    if (temperature >= 24) {
      
      number2A();
      number4B();
    }
}




////////////////////////Functions for various characters 1st digit  //////////////////////////////////////

void number0A() {      //number 0
  
        PORTD = B11000000; //'0'
        PORTB = B00000001; // 1st digit
        delay(10);        //10ms delay
}

void number1A() {  //number 1
  
     PORTD = B11111001; //'1'
     PORTB = B00000001; //1st digit
     delay(10); //10ms delay
}

void number2A() {  //number 2
  
    PORTD = B10100100;  //'2'
    PORTB = B00000001;  //1st digit
    delay(10);    //10ms delay
}

void number3A() {  //number 3

    PORTD = B10110000;  //'3'
    PORTB = B00000001; //1ST DIGIT
    delay(10);  //10ms delay
}

void number4A() {  //number 4

    PORTD = B10011001;  //'4'
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //10ms
}

void number5A() {  //number 5

    PORTD = B10010010;  //'5'
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //10ms
}

void number6A() {
  
    PORTD = B10000010;  //'6'
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //10ms
}

void number7A() {  //number 7

    PORTD = B11011000;  //'7'
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //delay10ms
}

void number8A() {   //number 8

    PORTD = B10000000;  //'8'
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //10ms delay
}

void number9A() {  //number 9

    PORTD = B10011000;  //'9'
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //10ms
}

void decimalp1() {  //decimal point 1

    PORTD = B01111111;  //DP1
    PORTB = B00000001;  //1ST DIGIT
    delay(10);  //delay 10ms
}



//Functions for various characters 2nd digit

void number0B() {      //number 0
  
        PORTD = B11000000; //'0'
        PORTB = B00000010; // 2nd digit
        delay(10);        //10ms delay
}

void number1B() {  //number 1
  
     PORTD = B11111001; //'1'
     PORTB = B00000010; //2nd digit
     delay(10); //10ms delay
}

void number2B() {  //number 2
  
    PORTD = B10100100;  //'2'
    PORTB = B00000010;  //2nd digit
    delay(10);    //10ms delay
}

void number3B() {  //number 3

    PORTD = B10110000;  //'3'
    PORTB = B00000010; //2nd DIGIT
    delay(10);  //10ms delay
}

void number4B() {  //number 4

    PORTD = B10011001;  //'4'
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //10ms
}

void number5B() {  //number 5

    PORTD = B10010010;  //'5'
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //10ms
}

void number6B() {
  
    PORTD = B10000010;  //'6'
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //10ms
}

void number7B() {  //number 7

    PORTD = B11011000;  //'7'
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //delay10ms
}

void number8B() {   //number 8

    PORTD = B10000000;  //'8'
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //10ms delay
}

void number9B() {  //number 9

    PORTD = B10011000;  //'9'
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //10ms
}

void decimalp2() {  //decimal point 1

    PORTD = B01111111;  //DP1
    PORTB = B00000010;  //2nd DIGIT
    delay(10);  //delay 10ms
}

I’m also aware that my 7-segment code isn’t particularly efficient either. :stuck_out_tongue:

void loop(void) {
    float temperature = tempSensor.getTemperature();
    
    if (temperature >= 24) {
      number2A();
      number4B();
    }
}

Do you get something using the code above? Why don't you want to display temperatures below 24? To get a brighter display you may need to refresh the 7segment display repeatedly for a while and only occasionally get a new temperature reading.

The "scope" problem was because the temperature variable was declared inside another function. You should store the bit patterns for each digit in an array and have a single function that fetches the pattern for the digits you want to display.

I don't get anything except that scope error using the above code. I did think of tying arrays. But isn't what i'm doing just a longer way of doing it? And so, would i not still get the same type of problem.

I only used the >=24 as an example to see if i was going to get anything.

I still don't quite understand how i 'fetch' the temperature info, is my above if function going along the right lines?

:)

you 'fetch' the temperature by calling tempSensor.getTemperature(). That probably works fine already.

But isn't what i'm doing just a longer way of doing it?

Your code works fine as long as you want to display "24" for temps above 24 and nothing otherwise, but it will be cumbersome if you want to display an arbitrary value on the 7segment display.

There should be no scope error if you replace the loop you had with the one I posted earlier. Please try again.

AH! My apologies, i read your last post with the code as a part of my own, i didn't realise you had changed it. Thanks very much! :D

There is now action on the display but i see i am going to have to make an array for the digits to display properly. I'm getting glitches which i think are to do with the delays of my number functions.

Thanks again.

:)