Arduino controlling a 4 digit seven segment display.

Arduino controlling a 4 digit seven segment display.

I recently wrote some code to drive a 4 digit seven segment display using the arduino and 74HC595 shift register chip. The display is a seven segment common anode display with decimal dots. The pin configuration can be found here. I have connected a DS18S20 temparature sensor to read the temperature and display the reading on the seven segment display.

Here is how it looks:

Here is the arduino sketch:

#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 = 5;//Pin connected to ST_CP of 74HC595
const int clockPin = 6;//Pin connected to SH_CP of 74HC595
const int dataPin = 7;//Pin connected to DS of 74HC595

const int digitPins[4] = {
  8,9,10,11}; //pins to control the 4 common anode pins of the display

const byte digit[10] = //seven segment digit bits
{
  B00111111, //0
  B00000110, //1
  B01011011, //2
  B01001111, //3
  B01100110, //4
  B01001111, //5
  B01111101, //6
  B00000111, //7
  B01111111, //8
  B01101111  //9
};

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

void setup()   {                
  TCCR2A = 0;
  TCCR2B = (1<//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(400);
  digitalWrite(digitPins[digitScan], HIGH); 

  digitalWrite(latchPin, LOW);  
  if(digitScan==2)
  {
    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*100);
  digitBuffer[3] = tmp/1000;
  digitBuffer[2] = (tmp%1000)/100;
  digitBuffer[1] = (tmp%100)/10;
  digitBuffer[0] = (tmp%100)%10;

  //digitalWrite(ledPin, LOW);
  delay(20);
}


Download the sketch from the link below: 

SevenSegmentTest.pde (2.27 kb)

If you have any questions, please feel free to ask me via the contact link.

6 thoughts on “Arduino controlling a 4 digit seven segment display.

  1. kristian

    Hi. Did you do any other projects with that display? maybe clock or something else? if you do, please answer and send some… because i have a problem, i cant find information about 12pins display, all of them are 16
    My email: buzuko@gmail.com
    Thanks :)

  2. Tadpole

    Thanks for this example. I was searching *forever* for something like this to help. One little correction that I noticed however. Numer five is listed as B01001111 in the downloadable code instead of B01101101.
    thanks again!

  3. Adam

    Amazing. I’ve always wondered about how to get a temperature sensor to read the correct value. I didn’t realise there was a library for it!

  4. Lars

    This code worked great but has anyone been able to get negative numbers to work in the code? I was able to remove the hundredth’s decimal and have only tenth’s decimal to make way for a minus sign but it leaves a leading zero. Please advise.
    Kindest regards.

Leave a Reply

Your email address will not be published. Required fields are marked *


6 × seven =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>