Arduino temperature reading using a DS18B20 with 74HC595 / 4x Seven Segment displays

Thread Starter

Remembermyname

Joined Sep 6, 2015
91
Greetings,
Recently, I had been trying to build an Arduino temperature reader using a DS18B20 and display the output on 4x Seven Segment Displays. With a lot of trial and error (mostly errors), this was completed. This project was set up using an Arduino Nano driving a single multiplexed 74HC595 to drive the displays. It is capable of reading the full range of the DS18B20 (-55°C to +125°C (-67°F to +257°F)). The code is currently set up to read Fahrenheit degrees but can easily be changed to read Celsius. It has been tested on its low end with an inverted can of keyboard duster to -48.0F to its high end with boiling water to ~210.0F. I had no intention of displaying an F or a C in the display since I knew what I which temperature it is being used for otherwise, I'd have to add a 5th digit.

Arduino_DS18B20_temperature_reader.jpg

This was built using a breadboard. Granted, it would have looked better with less wiring using a single 4 digit display. The 4 individual digits were wired for the same purpose as this is what I had on hand.

Below is the schematic:

Arduino_DS18B20_temperature_reader_schematic.png

I wanted to build this as a basic starting platform for other projects such as a deep freezer controller with LED readout. The use of the single 74HC595 rendered the Arduino with free pins to work with control relays, etc. The readout is formatted to display numbers without leading zeros and to the tenth's decimal place. The code involved was taken from http://www.pial.net/arduino-controlling-a-4-digit-seven-segment-display/ and modified. The code uses the DallasTemperature and OneWire libraries. This code uses 5054 bytes (16% of program storage space).

Information about OneWire can be found here:
https://www.pjrc.com/teensy/td_libs_OneWire.html

The Dallas temperature control library can be found here:
https://milesburton.com/Dallas_Temperature_Control_Library (Down at this moment)

The code is pasted below:

C:
/*
*
* This code was taken and modified from the original code which can be found at:
* http://www.pial.net/arduino-controlling-a-4-digit-seven-segment-display/
*
*/

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

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;

const int ledPin =  13;// LED connected to digital pin 13
const int latchPin = 8;// Pin connected to ST_CP of 74HC595
const int clockPin = 9;// Pin connected to SH_CP of 74HC595
const int dataPin = 10;// Pin connected to DS of 74HC595

const int digitPins[4] = {
  3,4,5,6}; //pins to control the 4 common anode pins of the display

const byte digit[12] = //seven segment digit bits + blank + minus
{
  B00111111, //0
  B00000110, //1
  B01011011, //2
  B01001111, //3
  B01100110, //4
  B01101101, //5
  B01111101, //6
  B00000111, //7
  B01111111, //8
  B01101111, //9
  B00000000, //Blank
  B01000000  //-
};

int digitBuffer[4] = {
  1};
int digitScan = 0;
int soft_scaler = 0;
float tempC, tempF;
int tmp;
boolean sign = false;

void setup()  {
  TCCR2A = 0;
  TCCR2B = (1<<CS21);
  TIMSK2 = (1<<TOIE2);
  TCNT2 = 0;

  pinMode(ledPin, OUTPUT);
  for(int i=0;i<4;i++)
  {
  pinMode(digitPins[i],OUTPUT);
  }
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  sensors.begin();
  sensors.getAddress(insideThermometer, 0);
}

ISR(TIMER2_OVF_vect) {
  soft_scaler++;
  if(soft_scaler==15)
  {
  refreshDisplay();
  soft_scaler = 0;
  }
};

void refreshDisplay()
{
  for(byte k=0;k<4;k++)
  {
  digitalWrite(digitPins[k], LOW);
  }
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
  digitalWrite(latchPin, HIGH);
  delayMicroseconds(50);
  digitalWrite(digitPins[digitScan], HIGH);

  digitalWrite(latchPin, LOW);
  if(digitScan==1)
  {
  shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | B10000000)); //inserting the dot
  }
  else
  {
  shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
  }
  digitalWrite(latchPin, HIGH);
  digitScan++;
  if(digitScan>3) digitScan=0;
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  sensors.requestTemperatures();
  tempC = sensors.getTempC(insideThermometer);
  tempF = DallasTemperature::toFahrenheit(tempC);
  tmp = int(tempF*10);
  if (tempF < 0){
  sign = true;
  tmp = abs(tmp);
  }
  else{
  sign = false;
  }

  if (int(tmp)/1000 == 0){
  digitBuffer[3] = 10;
  if (sign){
  digitBuffer[3] = 11;
  }
  }
  else{
  digitBuffer[3] = int(tmp)/1000;
  }
  if (int(tmp)/1000 == 0 && (int(tmp)%1000)/100 == 0) {
  digitBuffer[2] = 10;
  if (sign){
  digitBuffer[2] = 11;
  digitBuffer[3] = 10;
  }
  }
  else{
  digitBuffer[2] = (int(tmp)%1000)/100;
  }
  digitBuffer[1] = (int(tmp)%100)/10;
  digitBuffer[0] = (int(tmp)%100)%10;
  digitalWrite(ledPin, LOW);
  delay(500);
}
Many thanks to @ErnieM for your help in making this possible.
 
Last edited:

ErnieM

Joined Apr 24, 2011
7,996
Congratulations. I may have helped but you did the heavy lifting here. That's why I would point you in the direction but not give the complete answer: it's better for you when you figure these things out.

This looks like a candidate for the completed projects section!
 

JohnInTX

Joined Jun 26, 2012
3,810
I think so too. We would want to host the schematic, source code and library code here on AAC so that in the future, someone would have all they need to replicate the project without the problems of broken or outdated links.
Nicely done!
 

Thread Starter

Remembermyname

Joined Sep 6, 2015
91
Congratulations. I may have helped but you did the heavy lifting here. That's why I would point you in the direction but not give the complete answer: it's better for you when you figure these things out.
Absolutely right. And I'm glad that I wasn't just given the complete answer. I'll have a solution but I won't learn anything from it. :)

This looks like a candidate for the completed projects section!
I think so too. We would want to host the schematic, source code and library code here on AAC so that in the future, someone would have all they need to replicate the project without the problems of broken or outdated links.
Nicely done!
Excellent deal! Thanks! I'm reading over the 'How to post a project to the collection' thread to make certain that I have everything in order. I'm putting the software together and will edit the first post to meet the criteria.
 
Last edited:
Top