My Cart

Tutorials with Arduino: Digital thermometerSeptember 16, 2011

Finally we are doing something else than our SM-1 Ikea Robot Lamp. It´s been refreshing doing some other projects and tutorials, because these last days have been everything about the robot. See it in action on this video (and watch out for the fire part!)

So, let´s get to the action! Here we have a simple example of the Shift Register for controlling a display. We are using the HD74LS164P (check the datasheet here) for this case (it´s a lot cheaper than the MAX used in almost all the tutorials, i bought 5 from ebay for a couple of euros) and also we are using a 4 digit 7 segment led display from SparFun. [Cooking Hacks] Also available here

Parts list:

1 x HD74LS164P – 8-Bit Parallel-Out Serial-in Shift Register
1 x 4 character 7 segment led display
1 x 4,7k thermistor
1 x 4,7k resistor
4 x 470ohm resistor
1 x Arduino UNO board.

Explaining the 4 digit 7 segment Led Display:

As you can see in the image above, a 7 segment led display is nothing more than some leds arranged in a particular position. You have 7 of them, and with that you can arrange to display the numbers from 0 to 9, a minus sign, and a few letters. See how each led it´s called by a letter, from A to G (we will be using this later).

Our 4 digit led display is common anode, what does this means? it says each digit and all of the 7 leds on each digit have a common positive (anode) and a separate negative (cathode). For our case, it´s better this way, so we will only need 4 resistors, one for each common annode on each character of the display.

Se the schematic for the 4 digit display:

Using the shift register:

(From Wikipedia) “The shift registers  are commonly attached to the output of microprocessors when more output pins are required than are available. This allows several binary devices to be controlled using only two or three pins – the devices in question are attached to the parallel outputs of the shift register, then the desired state of all those devices can be sent out of the microprocessor using a single serial connection. Similarly, PISO configurations are commonly used to add more binary inputs to a microprocessor than are available – each binary input (i.e. a switch or button, or more complicated circuitry designed to output high when active) is attached to a parallel input of the shift register, then the data is sent back via serial to the microprocessor using several fewer lines than originally required.”

This is the Pinout for the shift register:

we are connecting it with the arduino and the display as follows:

1.- +5v
2.-  Digital Out 11 (In)
3.- Display Pin 14 (A)
4.- Display Pin 16 (B)
5.- Display Pin 13 (C)
6.- Display Pin 3 (D)
7.- Ground
8.- Digital Out 12 (Clock)
9.- +5v
10.- Display Pin 5 (E)
11.- Display Pin 11 (F)
12.- Display Pin 15 (G)

We are also using the Thermistor to check the temperature and send an analog value to our arduino UNO board, who will transform this analog data in a digital value and store it on a variable. We use this value to determine the number to be displayed. We are attaching it to Analog 0 in our Arduino Board.


The Code:


Code extracted from the Arduino Forum and mixed 
with Temperature wby colors on

4 Digit 7 Segment display from Sparkfun
 1: Digit 1    16: B
 2: Digit 2    15: G
 3: D      14: A
 4: Colon Anode    13: C
 5: E      12: Colon Cathode
 6: Digit 3    11: F
 7: Decimal Point  10: Apostrophe Anode
 8: Digit 4    9:  Apostrophe Cathode

 Display's Cathode goes to ground via resistor
 Display's Anode goes to digital out
 Digit pins go to digital out via resistor
 Segment pins (A-G) go to digital out or shift register out (0 is on)

Modified by @mrlndr for


int clockPin = 12;  //Pin connected to SH_CP 
int dataPin = 11;  //Pin connected to DS 

int digit1Pin = 5;
int digit2Pin = 2;
int digit3Pin = 3;
int digit4Pin = 4;

byte data;
byte dataArray[13];

int tempPin = 0;     // the thermistor and 4,7k resistor

int temp=0;
int tempread;

const int MINUS_IDX = 10;
const int CELCIUS_IDX = 11;
const int FARENHEIT_IDX = 12;

void setup(){
  pinMode(digit1Pin, OUTPUT);
  pinMode(digit2Pin, OUTPUT);
  pinMode(digit3Pin, OUTPUT);
  pinMode(digit4Pin, OUTPUT);

  pinMode(latchPin, OUTPUT);


  dataArray[0] = B11000000;
  dataArray[1] = B11111001;
  dataArray[2] = B10100100;
  dataArray[3] = B10110000;
  dataArray[4] = B10011001;
  dataArray[5] = B10010010;
  dataArray[6] = B10000010;
  dataArray[7] = B11111000;
  dataArray[8] = B10000000;
  dataArray[9] = B10010000;

  //temperature specific characters
  dataArray[MINUS_IDX] = B10111111;  // minus sign
  dataArray[CELCIUS_IDX] = B11000110;  // C
  dataArray[FARENHEIT_IDX] = B10001110;  // F


void loop(){
  tempread = analogRead(tempPin);
  // Serial.print("Temp = ");
  //Serial.println(temp); // reading the values
  setTemp(temp, 'C');

void setTemp(int temp, char scale){
  //temp must be between -99 and 999 in either scale to fit the display
  //put in a check here later
  boolean negative = false;
  if (temp < 0)
    negative = true;
  temp = abs(temp);

  if (scale == 'F'){
    setDigit(digit4Pin, FARENHEIT_IDX);
  } else if (scale == 'C'){
    setDigit(digit4Pin, CELCIUS_IDX);

  setDigit(digit3Pin, temp % 10);
  temp /= 10;
  if (temp >= 1){
    setDigit(digit2Pin, temp % 10);
    temp /= 10;
    if (temp >= 1){
	setDigit(digit1Pin, temp % 10);
  if (negative){
    setDigit(digit1Pin, MINUS_IDX);

void setDigit(int digitPin, int value){

    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, dataArray[value]);
    digitalWrite(latchPin, 1);

    digitalWrite(digitPin, HIGH);
    digitalWrite(digitPin, LOW);

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
	pinState= 1;
    else {
	pinState= 0;

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin 
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);

  //stop shifting
  digitalWrite(myClockPin, 0);

Via and Source: Arduino Arts

Leave a Reply