Arduino – Temperature Displayed on 4 Digit 7 segment (common anode)

In this project I’ll display the temperature in a 4 digit 7 segment display (common anode).

The sensor is the cheapest you can find so actually the temperature changes pretty easily which makes the display to show always different temperatures. But the idea is to apply this code to other projects with 7 segment displays that I might do later. You can also read more about 7 segment displays in this post.

new

This project is great to learn more about:

  • Reading sensors (in this case temperature)
  • 7 segment displays (4 digit 7 segment displays)
  • 8 bit Shift Registers (74HC595)
  • Pratice wiring

Parts required

IMG_0287

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

circuit diagram

This can also help you wiring because the Schematics I’ve made using fritizing turned out a bit confusing. This is the 7 segment display internal circuit diagram

images

7 segment 4 digit common anode

Basically  the pin 11 connects to the QA, the pin 7 to the QB and so one…

Upload the code below

/*
 * Temperature Sensor Displayed on 4 Digit 7 segment common anode
 * Created by Rui Santos, https://randomnerdtutorials.com
*/

const int digitPins[4] = {
  4,5,6,7};                 //4 common anode pins of the display
const int clockPin = 11;    //74HC595 Pin 11 
const int latchPin = 12;    //74HC595 Pin 12
const int dataPin = 13;     //74HC595 Pin 14
const int tempPin = A0;     //temperature sensor pin
const byte digit[10] =      //seven segment digits in bits
{
  B00111111, //0
  B00000110, //1
  B01011011, //2
  B01001111, //3
  B01100110, //4
  B01101101, //5
  B01111101, //6
  B00000111, //7
  B01111111, //8
  B01101111  //9
};
int digitBuffer[4] = {
  0};
int digitScan = 0, flag=0,  soft_scaler = 0;
;
float tempK, tempC, tempF, temp;
 
void setup(){                
  for(int i=0;i<4;i++)
  {
    pinMode(digitPins[i],OUTPUT);
  }
  pinMode(tempPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(tempPin, INPUT);
}
 
//writes the temperature on display
void updateDisp(){
  for(byte j=0; j<4; j++)  
    digitalWrite(digitPins[j], LOW);
 
  digitalWrite(latchPin, LOW);  
  shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
  digitalWrite(latchPin, HIGH);
 
  delayMicroseconds(100);
  digitalWrite(digitPins[digitScan], HIGH); 
 
  digitalWrite(latchPin, LOW);  
  if(digitScan==2)
    shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | B10000000)); //print the decimal point on the 3rd digit
  else
    shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
 
  digitalWrite(latchPin, HIGH);
  digitScan++;
  if(digitScan>3) digitScan=0; 
}
 
void loop(){ 
  tempK = (((analogRead(tempPin)/ 1023.0) * 5.0) * 100.0);  
  //Converts Kelvin to Celsius minus 2.5 degrees error
  tempC = tempK - 273.0;   
  tempF = ((tempK - 2.5) * 9 / 5) - 459.67;
  //Celsius temperature display
  tempC = int(tempC*100);
  digitBuffer[3] = int(tempC)/1000;
  digitBuffer[2] = (int(tempC)%1000)/100;
  digitBuffer[1] = (int(tempC)%100)/10;
  digitBuffer[0] = (int(tempC)%100)%10;
  updateDisp();
  delay(2);
 
  /*
  //Fahrenheit temperature display
  tempF = int(tempF*100);
  digitBuffer[3] = int(tempF)/1000;
  digitBuffer[2] = (int(tempF)%1000)/100;
  digitBuffer[1] = (int(tempF)%100)/10;
  digitBuffer[0] = (int(tempF)%100)%10;
  updateDisp();
  delay(2);
  */
}

View raw code

Watch the video demonstration

Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

99 thoughts on “Arduino – Temperature Displayed on 4 Digit 7 segment (common anode)”

  1. OK, I’ve got it working… quite a lot of changes, but it works… now I have a problem that the digits are blinking – it seems that reading temp makes them blink… only forst digit is ok, the rest are not… any ideas?

    Reply
    • I guess it’s a delay problem… if you have some delays in your loop it will make the display to blink!

      Reply
        • I’ve used a really small delay on the loop function and it worked, but it was like delay(2); Actually i think that i don’t even need that delay, but I’ve used it… but I remember that I tried with delay(10) and delay(20) and It kept my display blinking

          Reply
        • That can be the problem I guess, my temperature sensor just reads the voltage is going trough the sensor and then I convert the voltage to temperature, I don’t know exactly how your sensor does the reading… but if it’s taking too much time to read the temperature, for instance if your using a library that has a function to read the temperature that takes too much time, that will make the display to blink.

          Reply
    • In the schematics above if you check them carefully it’s something like that
      top pins:
      12 – 11(R) -10 (R)- 9 – 8 – 7(R)
      1(R) – 2(R) – 3(R) – 4(R) – 5(R) – 6 –
      I hope this helps you!

      Reply
      • Hi Rui!

        I’m a total beginner with Arduino, and am trying to adapt your project here to my 4-digit, 7-segment display (which appears to be the same as Ameen’s).

        Unfortunately, I really don’t understand this stuff well enough yet to know what I need to change… even with your last comment there. Would you mind giving a little more guidance?
        Thanks!

        Reply
  2. Hi Maciunio, I would like to build a wireless thermometer with the arduino board. interessting for me is which Hardware do you use for the wireless temperature sensor.
    Thanks in Advance
    Marvin

    Reply
    • Hi,
      It depends on how you want to read the temperature and where you want the temperature to be displayed.
      You can’t do that using a bluetooth module and receive that data.
      Or you can use an wireless shield or ethernet shield and display the temp on a webserver, (in a html page)
      And then you can acess all that information with your computer/smartphone.
      I hope I’ve anwesered your question!
      Good Luck

      Reply
      • Hi Rui, thanks for your quick response. The Plan is to record the temperatur with some kind of wireless temerature sensor. And this sensor transferred the Datas over approximate 10meters to the Arduino Board where the temperatur should be displayed.
        So my questions is which sensor could I use. Perfect would be a sensor without power supply just using batteries. But this is not a “Must Have”.
        Many Thanks.
        Bye Marvin

        Reply
        • Hi,
          Actually the best and cheap way that I thought It was possible to do that (I recommend you to do more research about this) was with a RF signal and you will need two arduino boards.
          One arduino board will read the temperature (for example with this temp sensor LM335Z) and It will send the data via RF
          to the other arduino that will receive the data via RF. And then with that you can display the temperature.

          this guy made an awesome tutorial not about what you want but it explains how RF works…

          Reply
    • Hi Dave,
      I’m sorry for some of my content being locked.
      But you don’t need any social networking.
      simply go to this page: https://randomnerdtutorials.com/download enter your email and you’ll receive access to a secret page that I update everytime I release a new project. so you can download everything in just one place and you don’t need to share anything.

      And btw in few days I’ll remove that feature. and I’ll my code will be available in my posts.
      I just need to finish some projects first.

      Sorry for that.
      All the best,
      Rui Santos

      Reply
  3. Hi
    I’ve wired everything up the way you have and for some reason i only get all the segments lighting up so it read “8888”, i hooked a potentiometer in place of the thermometer and the digits flicker when i change the voltage.
    Am i missing a library or something?
    thanks for your help

    Reply
    • Hi Baron,
      Thanks for trying my project.
      Just a quick question is your Display a Common Anode?
      Did you insert the transistor and the Shift Register in the right way?
      You don’t need any library… If you’re sure you’ve wired everything properly I don’t know exactly what can be wrong then :S

      Let’s see if it’s one of those problems I’ve mentioned above,
      Rui

      Reply
  4. also the shift register is correct i am sure, if i flip the arduino looses all power… the 7 segment display is common anode i am sure
    thanks again

    Reply
    • Seems to me that you’ve did everything correct…
      I have no idea what else can be wrong. I just tested this circuit again it works fine for me and for everyone who messaged me :S

      Sorry, I would need to be with the circuit on hands to see what’s missing.

      Reply
        • That’s a really good question… I’m not sure though.
          It will all depend on which values are you sending to the function that prints on the 7 Segments.

          If you upload exactly my code and remove the temperature sensor, it will output random values on the 7 segments.

          If you want to debug your project and be sure that your temperature sensor is working properly it’s easy.
          Simply insert some: Serial.println(“your values”);
          and you’ll see if your sensor is working or not…

          Reply
  5. Hi Rui!
    DUDE you are a freaking life saver!! I love the you have described everything so perfectly in this tutorial. I have but a couple of questions though. I am trying the same project with a load cell in stead of the temperature sensor. For my project I need to input an 8 bit pressure value from the computer, the ardiuno needs to read it and then adjust the load cell accordingly. Now using serial communication I know I can send data to the Ardiuno. How ever the data is going to be in ASCII. So how do I convert it to binary so I can display it on the 7 segment display and also use the data value to adjust the pressure being applied on the load cell accordingly.( i have a feeling its got something to do with the byte() function.
    My second question is the sensor is powered from an external power supply. As far as my understanding goes, the out put from the load cell can be connected directly to the analog pin and ground and it should work fine. But I would appreciate it if you could confirm this.
    Thanks alot for your help bro!! you are doing a great job. Please keep it up =D

    Reply
    • Thanks Ali,
      I’m glad you found my tutorial useful!
      first question:
      that’s true you’re sending and reading ASCII values from the arduino IDE serial monitor.
      One way to turn ASCII into binary as you said is by creating a new function that will do exactly what you want.7
      The Arduino supports some functions that might help you, It’s called atoi(). and it basically converts any string you receive into a number.
      http://www.cplusplus.com/reference/cstdlib/atoi/
      or this one might also be useful:
      http://arduino.cc/en/Reference/BitRead

      second question:
      you’re right.. It should work just fine, but you need to be sure how much voltage/current is going through the sensor.
      And then change the function that reads the analog pin and calculates the temperature.

      I hope this helps!
      More tutorials coming soon,
      stay tuned!
      Rui

      Reply
  6. thanks Rui for that informative reply!
    I was successful in writing a conversion for ascii to binary.
    I was going through your code again and wanted to ask a few quick questions about the code you have written.
    1. you have defined flag=0 and soft_scaler = 0 but have not used them any where in the code. why is that.
    2. secondly you have inverted the bits in the code using ~. why do you do that?
    3. you have done conversions for Fahrenheit and Celsius. but which one is it that you display in your final display? and how do you chose which one to display!
    greatly appreciate your help!!
    thanks
    Regards

    Reply
    • I’m glad I could help.
      1) Those variables aren’t necessary, you can delete them and the code will work just fine. They weren’t necessary in the end, but I forgot to delete them.
      2) which lines of my code are you exactly referring to?

      Reply
  7. ignore the third silly question. lol just realized the Fahrenheit display is in the form of a comment. please if you could answer the first two that would be just awesome! thanks!!

    Reply
  8. Hi Rui!
    cheers!
    the lines of the code I am referring to are
    if(digitScan==2)
    shiftOut(dataPin, clockPin, MSBFIRST,~(digit[digitBuffer[digitScan]] | B10000000)); else

    shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);

    you have used “~” symbol. isnt it used to invert the bits?
    Regards
    Ali

    Reply
    • Yes it works inverted!
      I don’t recall exactly why is like that… I think is because I’m using a common anode 4 Digit 7 segment .

      Reply
      • hahaha! well it works like a charm! so I cant thank you enough for your public service 😉
        btw with regards to the flickering display. Smoothing the values (averaging them and then displaying them) might give you a better display!! 🙂

        Reply
  9. Please help me out with the code for a common cathode. Instead of having 1 digit lit up at time im getting 3 digits… please help

    Reply
  10. Hey…Thank you so much this is helping me a lot…The problem is I am using a & segment display with 14 pins. /could that be the reason it could not be working?

    Reply
    • Please send me a link to your Display… I have no idea which display you’re using apurva…
      If you’re using a different Display how are you wiring the circuit?

      Reply
  11. Hello Rui! 🙂 i have a question…the Temperature Sensor doesn’t have to connect to the ground? Because i see that the middle pin of it is connected to the + and tot he arduino…

    Reply
  12. Thank you a buncch ffor sharing this with all of us you actujally recognize
    what you’re speaking about! Bookmarked.

    Reply
  13. Great job!
    Though I would like to add some comments,
    The code is quite complex for a beginner, so I had really to debug, please add more comments, especially in the function you add (we are used to have only void setup and void loop….;-). The diagram is also something you should more comment since it is not clear from the picture.
    I am using instead of the temp sensor a sonar and it works perfect, but how do i get rid of the flickering? I tried to put delays between the readings but understood that it just delays the digits writings to the display.
    Thanks for sharing, I will keep following your projects
    Y3G

    Reply
  14. hello bro Rui.
    acctually i need your help. i want to change the temperature sensor with Load Cell sensor which is to measure the mass of an object. so which part should i change the coding part?

    thank you,

    Reply
  15. Hi Rui ,
    Im newbie in Arduino. Thank you for your sharing about Temperature Displayed on 4 Digit 7 segment.

    But I can not understand what means with your fritzing file.

    Im using 4 Digit 7 segment(SMA420564)、74HC595N and LM35

    But I dont know how to fixed them in breadboard.
    For example, there are 12pins(6pins for one side) in my display(SMA420564) but it seems 20 pins(10pins for one side) in your fritzing file.

    Can you tell me how to fix in my breadboard with SMA420564、74HC595N and LM35?

    Thank you so much!

    Reply
    • Sorry I mean following your fritzing file and code but it does not work 🙁

      Can you tell me where is a problem?

      I check my circuit and code. it is not different with you.

      thank you very much!!!

      Reply
  16. Hi there!
    I have a similar thermometer project like this. Instead of LM335Z Temp Sensor, I’m using THERMISTOR and applying voltage divider 🙂

    +5V o——-/\/\/\/\——–|——–/\/\/\/\—–|I- ground
    ( Thermistor) | ( Resistor)
    10k ohms | 10k ohms
    o
    (Analog pin)

    **QUESTIONS:
    1.) Would this circuit be applicable as a replacement of the LM335Z temp sensor?
    2.) How to eliminate flickers on the display?

    Reply
    • Hi Julyle,
      1) Yes, you can replace the LM335Z with a thermistor, you just need to change that part of code that reads the sensor.
      2) No… I have always new projects coming up, so unfortunately I’ve never had a change to fix that problem…
      Thanks for trying my project!
      Rui

      Reply
  17. Hi ! Thanks you so much for this !! I just have a question, i’m using a 4 digits 7 segments display but with common cathode, what did i have to change in the program or in the wiring ?
    Thanks you again !

    Reply
  18. Hi..
    Is there any way you can send me an eagle schematics for this project. i cannot import a fritzing file into my eagle and am doing my senior design and my professors are not teaching me anything they expect me to know everything am having a severe depression help me out please!

    Reply
  19. Hai Rui,

    Thanks a lot for this post, it helped me a lot to create my own thermometer. The approach that I took was interrupt-driven displaying and sampling of the sensor. You can find my code at dse.nl/~rbos/wordpress/2015/03/07/arduino-thermometer/.

    Best regards!

    Reply
  20. Use

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 255);
    digitalWrite(latchPin, HIGH);

    This can help you avoid flicking on the digits.

    Reply
  21. Use

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 255);
    digitalWrite(latchPin, HIGH);

    This can help you avoid flicking on the digits.

    Reply
  22. hello, great tutorial! thank you. i have a huge doubt that has to do with this. i’m presently building a metronome using a potentiometer to change the beats per minute. so far so good. now when i try to connect the 7segment display to show the bpm it will show the number correctly but just for the time that the beat lasts. my beat delay is getting in the way of the display function cycle. i did some research and there is a way to do functions in parallel with classes. but i can’t figure it out. do you have any advice?

    Reply
  23. Hi, I am trying to use the 4 digit 7 segment display to display heart rate using a heart rate sensor. I would like to know if it is possible.

    Reply
  24. Hola una consulta los display los 4 ánodos de cada digito van conectados a los pines del Arduino 4,5,6,7, estos pines del Arduino salen 5vcc para alimentar los displays ?
    Los segmentos se controlan con el 74hc595 , me lo puedes explicar como trabaja

    Reply
  25. Hi Rui, Sara,
    Would you happen to have a code and schematics for a 6 digit clock using seven segment displays, common cathode?
    I look forward to your reply.

    Best Regards,
    ALBERTO FRANCISCO

    Reply
  26. Hello, Fanstastic One…

    Plz help what will be code to shift Decimal after 3 digit and if we want all 4 digit without decimal.
    Thanks in advance.
    Sud Eiz

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.