# Temperature Sensor Verilog

Hello I have been working on an analog temperature sensor TMP36 and I am trying to get the temperature and display it on the NEXYS3 SSD. I am aware the NEXYS3 does not have an analog pin and I built an analog to digital converter using the TI ADC0804 an 8-bit ADC. The part I need help with is particularly getting the input from the temperature sensor. I do not understand the data sheet well enough to come up with the formula in verilog. The only example I found was for an arduino in the link below

DATA SHEET

http://www.analog.com/static/imported-files/data_sheets/TMP35_36_37.pdf

I am powering the ADC with 5 volts and need help converting their formula to verilog.

Voltage at pin in milliVolts = (reading from ADC) * (5000/1024) Centigrade temperature = [(analog voltage in mV) - 500] / 10

Those are the two formulas I need help with. I need an formula equivalent in Verilog. The output to the SSD is an 8-Bit number in other words am only interested in temperatures 0-99 for my project. Thanks any help would be appreciated.

Simple algebra. Plug one of them into the other and simplify:

mv = code * 5000/1024

temp = (mv - 500) / 10

temp*10 = mv - 500

temp*10 = code * 5000/1024 - 500

temp*10 = (code * 5000) / 1024 - 500

(add divisor/2 before performing an integer division for rounding)

temp*10 = (code * 5000 + 512) / 1024 - 500

temp*10 = ((code * 5000 + 512) >> 10) - 500

One multiply, one shift right by 10, one addition, and one subtraction. Then convert to BCD and display it.

However, if you have an 8-bit ADC, you're probably going to want to divide by 256 instead of 1024 (shift right by 8 instead of 10) and add 128 instead of 512.

And here is an algorithm for converting binary to BCD: http://www.eng.utah.edu/~nmcdonal/Tutorials/BCDTutorial/BCDConversion.html

Just run that, split into groups of 4 bits, and drive each digit in the display with one group (after decoding to the proper 7 segment signals, of course).

• I'm going to be picky and say that you should include a rounding offset before doing the division, but otherwise that's exactly the way I'd do it. – markt Nov 27 '14 at 23:44
• Good point, I will add that. – alex.forencich Nov 27 '14 at 23:44

Thank you so much for the help I used the code above to help me and I ended up with what I have below if anyone needs it.

assign tempV = (tempin * 16'b0001001110001000 + 8'd128) / 9'b100000000;
assign tempC = (tempV - 9'd500) / 8'd10;


I thought your formula was wrong but I realized the problem that I had was with the input voltage which was not precise and needed some tuning. My temperature was off by a couple degrees but I figured it out. Thank you it was my first time working with ADC and did not fully understand how they work but I have a really good understanding with some searching.