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.

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

Parts required

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

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

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

``````/*
* 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.

[eBook] Build Web Servers with ESP32 and ESP8266 (2nd Edition)

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols 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!

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

1. hi, can you share the fritzing file? 🙂

• thnx 🙂
I’m trying to use your project as a part of mine – wireless themrometer with display, but as usual I have problems with displaying the numbers 😀
tomorrow morning I’ll try to fixup some wires 😉

• Good luck!
if you have any troubles that I can help you with, just let me know

• Friend need a program to help me on and off 2 leds with arduino, android and bluetooth module can help me?

• if you’re portuguese you can comment in portuguese.
Tell me what you really want to do and what arduino, bluetooth module you have…

• i tried to use your program for display on a common cathode, but it is not working even though i switch 0 to 1 and 1 to 0, any help….

2. 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?

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

• Could that loop has any delays?

void loop()
{
sensors.requestTemperatures();
t = sensors.getTempCByIndex(0)*100;
displayNum(t, 20);
}

• 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

• I think that delay is from that I’m using digital Dallas 18B20 temp sensor… How do you think?

• 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.

• 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!

• 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!

• Oh, and if it helps, this appears to be the datasheet for my 4-digit, 7-segment display:

foryard-led.ru/pdf/fylq-5641ax_bx.pdf

3. 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.
Marvin

• 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

• 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

• 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…

• What you mean?
Thanks for commenting…

4. hi.. Rui .

how about if im using LM35DZ ?

thank you 🙂

• They will both work just fine I guess!
but I’ve searched a bit and I found this:

“The difference between an LM335 and LM34 and LM35 temperature sensors is the LM335 sensor gives out the temperature in degrees Kelvin, while the LM35 sensor gives out the temperature in degrees Celsius and the LM34 sensor gives out the temperature in degrees Fahrenheit. ”

I hope this helps! Good Luck with your project!

• thanks… i’ll try and work 🙂

• Awesome! Thanks for commenting! Let me know if it worked for you!

5. hi i want to use this segment with HY-srf05 ultrasound sensor to displayed the distance

6. Too bad so much content is locked, defeats purpose of tutorials for those of us that don’t do social networking…

• Hi Dave,
I’m sorry for some of my content being locked.
But you don’t need any social networking.

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

7. thank’s for this tutorial. i’ll try it.

Cheers,
Rui

8. 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?

• 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

• Hi
Thanks for the quick response, i dont see a transistor in the schematic? That may be the problem…
Where does it go?

• You’re right there’s no transistor I was confused with another project! sorry :S

9. 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

• 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.

• Hi
thanks for the help, what does the display show if you remove the temperature sensor? do they all light up or is it blank?

• 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.
and you’ll see if your sensor is working or not…

10. may i know the code for the temp sensor

Please click the like button or another sharing button and the code will unlock.

11. 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

• 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:

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

12. 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!
thanks
Regards

• 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?

13. 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!!

14. 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

• 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 .

• 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!! 🙂

• Thanks ali!
Might try that when I have more free time!

All the best,
Rui

• That’s what happens when we don’t comment every line, now I don’t remember exactly why I did that xD

15. which part of your program should i use if i want to display say 3489 on a common cathode segment display??

• How are you wiring the circuit?
You need to tell me a bit more about what you’re doing…

17. 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?

• 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?

• Hi Enrico,
I’m already using a shift register… what you exactly mean?
Use another one?

• You have connected the common cathode on Arduino, not to the 74HC595. It is possible to connect the common cathode to the 74HC595?

• Yeah I think so… but it wouldn’t only save you an extra pin I think.
But that’s something that you can try

18. 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…

• sorry i said somehing stupid..haha…i mean its connected to the ground and + too..but..the first pin is not connected anywhere..

• Thank you! 😀 yes…it helped! 😀

19. Thank you a buncch ffor sharing this with all of us you actujally recognize

20. Great job!
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

21. I need to show the engine RPM in 5 segment display.
So can you help me with the code?

• 5 7-segment display*

22. 5 7-segment display

• Here’s a similar project for what you’re looking for:
You just have to merge that project with mine!

23. 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,

24. 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!

• 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!!!

25. 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?

• 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

26. 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 !

27. 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!

• Hi Michael,
Sorry I don’t have time to draw the circuit for you, but it should be really easy.
There are plenty of tutorials using Eagle on the Internet

28. 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!

• Looks great Ronald,
Thank you for sharing!
Rui

29. Hi Rui

From where can I get the green 7 Segment displays you have used ? I need that beautiful sea green display.

• Hi Jayanth D,
I’ve ordered them through eBay. I’ve tried to find the original source, but I couldn’t.
Have a nice day,
Rui Santos

30. Please let me know what is to be done if we want to use different sensor for eg pt-100 or pt-1000?

31. How to display the temperature reading on serial monitor with this sketch? please tell?

• You can use the command Serial.println();

32. Use

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

33. Use

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

34. Very good i will help much. Thanks

• Thanks 🙂

35. 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?

• Hi.
Thanks for asking. But, unfortunately we don’t have any tutorials on that subject.
Regards,
Sara 🙂

36. 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.

37. 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

38. Hi Rui, Sara,
Would you happen to have a code and schematics for a 6 digit clock using seven segment displays, common cathode?

Best Regards,
ALBERTO FRANCISCO