LM35 to BCD to 7 segment display

Hi guys I'm new to the Arduino World, actually I know nothing I just need it for a College Project. So if you could help I'd be very thankful. So I was trying to convert the Analog signal from the LM35 by a ADC0804 but the output is in 8bit and it's very hard and not worth it to decode an 8bit output to a BCD and then to 7 segment displays only using ICs. So my teacher said it was ok to use arduino and do this by software, so I started to look for some codes out there but I only found codes that convert it directlly to the 7 segment display, but I just want the output from the Arduino to be in BCD format, like 4bits BCD for the units, and 4 bits for the tens, and so on.

TL;DR: I need a code that converts the analog output from the LM35 to BCD 4 bits for units and 4 bits to Tens.

You should be able to read the LM35 directly with an analog pin on an Arduino if you are measuring temperatures in the range of 2 to 150 degrees centigrade. There is a simple formula to convert the analog value you get into a voltage and then into a temperature and then displaying the results on a multi digit 7 segment display.

Yeah yeah, I found some of those formulas, but I don't know how to separate the value like a said before, 4bits for the units and 4 bits for the tens.

Start by reading the LM35 from an analog pin and print the results to the serial monitor. Then attempt to convert the result to a temperature and print that.

What 7 segment LED display are you using ? Number of digits? common anode/cathode ? post a link to the data sheet.

You separate bits by bit masking and bit shift operators.

You can get 7 segment displays (eg TM1637) where you have a library function to directly display a number and it handles all the other conversion etc.

SergioNeto:
Yeah yeah, I found some of those formulas, but I don't know how to separate the value like a said before, 4bits for the units and 4 bits for the tens.

If you break it down into steps and write them on paper, the answer will present itself. Take a look.
You need to take 23 and get four bits for the 2 and four for the 3.

  1. First get a variable with the tens value and a variable value for the ones place (use /10 and %10)
  2. Then combine two variables into a single byte, You know it must be ttttoooo (t=tens o=ones). You have 0000tttt and 0000oooo, so shift the tttt left 4 bits.
  3. Now you have tttt0000 and 0000oooo. Add the variables to a final output value of ttttoooo.

SergioNeto:
[...] So my teacher said it was ok to use arduino and do this by software, so I started to look for some codes out there but I only found codes that convert it directlly to the 7 segment display, but I just want the output from the Arduino to be in BCD format, like 4bits BCD for the units, and 4 bits for the tens, and so on.

TL;DR: I need a code that converts the analog output from the LM35 to BCD 4 bits for units and 4 bits to Tens.

That means you want to use external BCD-to-7 segment decoder like 4511. If so, your setup might become like this:
lm35dec7seg.png
Figure-1: Non-multiplexed display unit

You may now follow these steps to acquire temperature signal from LM35 sensor and show only the integer part of the temperature onto DP0-DP1 positions of the 7-segment display unit.
1. First of all, upload the following codes to see the temperature value on Serial Monitor.

void setup() 
{
  Serial.begin(9600);
  analogReference(INTERNAL);
}

void loop() 
{
  unsigned int tempC;
  tempC = (int)100*(1.1/1024)*analogRead(A5); //tempC contains value in binary format
  Serial.println(tempC);  //print() command converts the binary value of tempC into decimal/BCD format
  delay(1000);
}

2. Assume that the Serial Monitor shows: 23; it means that the value of the variable tempC = 000010111b (0x17 = 23 decimal). Now, to show the temperature value on DP0-DP1 positions of the 7-segment display unit, we have to convert the binary value 00010111b into 00100011 (23). How can we do it? The conversion can be done using modulus(%) and division (/) operators as has been suggested by @adwsystems in his Post#4.

tempC = 00010111; //assume
Let us divide tempC by 10 (0x0A); we will get 8-bit remainder (R1) as 00000011 (0x03 = 3 = 0011) and 8-bit quotient (Q1) as 00000010 (0x02 = 0010). The following operation will give us the R1: byte R1 = tempC%10. Now, send the value of R1 onto PB3 - PB0 by executing this instruction: PORTB = R1. The 4511 will automatically decode this BCD pattern and will show 3 at DP1 position of the display.

The following operation will give us the Q1: byte Q1 = tempC/10. Perform this modulus operation on Q1 to get R0 (00000010 = 0x02 = 0010): byte R0 = Q1%10. Now, send the value of R0 onto PC3 - PC0 by executing this instruction: PORTC = R0. The 4511 will automatically decode this BCD pattern and will show 2 at DP0 position of the display.

The codes are (untested): (you need to set the directions of PB3-PB0 and PC3-PC0 lines as outputs and copy other codes as needed from Step-1.)

unsigned tempC;
tempC = (int)100*(1.1/1024)*analogRead(A5);
byte R1 = tempC%10;
PORTB = R1;

byte Q1 = tempC/10;  //or  tempC = tempC/10;
byte R0 = Q1%10; 
PORTC = R0

BTW: As you have been asked to use Arduino and software, you (probably) have to remove the 2x4511 decoders from the circuit of Fig-1. Now, the necessary cc-codes for the 4-bit BCD codes of unit/ten positions are to be collected from a lookup table. Are you familiar with the concept of lookup table? Something else more -- the display will become a multiplexed one of the following type.
lm35Mux.png
Figure-2: Multiplexed display unit

lm35dec7seg.png

lm35Mux.png

Yes just like the Figure 1 you showed, thats exactly what I needed. And it's not that I've been asked to use arduino, actually its "ok" to use it only for the conversion step. The orther parts I must use ICs. And this may sound rude but, if I only copy and paste this code and upload it to the board it will work? I mean doing the necessary changes. Thanks a freaking lot for doing this for me, and please don't, at least try to hahaha, not see me as a douchebag who doesnt want to his work, I'm asking this for you guys because I don't have the time to learn arduino, I do want to learn it but now isn't the moment, so sorry and thank you.

@OP

From the description of Post#5, please try to write some codes using setup()/loop() functions and post the codes. The Forum Members will help you by correcting/modifying your posted codes.

Alright, gonna run some tests on proteus and try to show you something.

So hey, it worked pretty well, but I thought that using the anlag outputs might bring me problems in the real world, because I don't know if it will always be a 5V output that is what a IC "understand"or it will be too noisy, don't know, so I tried using the "PORTD" for digital outputs, but the TX was always 1, then a friend told me to remove de "Serial.begin" from the code and it worked, at least at the simulation. So is it ok to remove it, I mean, in the real build will it cause any trouble or anything? Or there is another better whey to not use the analog outputs?

So the code didn't change much, but it is like this now:

void setup() 
{
  //Serial.begin(9600);
  analogReference(INTERNAL);
}

void loop() 
{
  unsigned tempC;
  tempC = (int)100*(1.1/1024)*analogRead(A5);
  byte R1 = tempC%10;
  PORTB = R1;

  byte Q1 = tempC/10;  //or  tempC = tempC/10;
  byte R0 = Q1%10; 
  PORTD = R0;
}

@OP

Leave the instruction Serial.begin(9600); active (do not keep it under comment). Before uploading, remove jumper connections temporarily from TX and RX (DPin-1 and DPin-0) pins; after uploading, put the jumpers back on their original positions. You will be able to see temperature readings both on 7-segment display unit and Serial Monitor.

Thanks for the good work with K++.

It's been a while since I replied this topic, but I just run in another problem that I coundn't solve or understand, so I just started bulding the projetc and when I connect the output from the IC 4511 in the 7 seg display with a 330ohms resistor on the COM connector the read on the serial display is right, but the number doesnt show up on the 7 seg display, but when I remove the resistor and just use a Jumper to connect the COM of the display the numbers are shown precisely well but the read isnt right, its varies a lot, it looks like the jumper is intefering in the read. Any ideas?