TMP36 Inconsistent Analog Readings via MCP3008

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
daxw
 
Posts: 4
Joined: Fri Sep 28, 2012 5:57 pm

TMP36 Inconsistent Analog Readings via MCP3008

Post by daxw »

I'm unable to get consistent analog readings from your TMP36 sensor. I am using an MCP3008 ADC and the driver you provided online. I have the temp sensor wired to the second pin (adc num 1).

I have also followed your guidelines here and have read both recommended threads when reading from two sensors here and here. I believe I am following all the recommend suggestions.

I have tried inserting a resistor as well.

So, here's the read code. The code comments explain the process.

Code: Select all

# read sensor nine times with a 10 ms delay
for i in range(9):
        self.analogreadings.append(self.mcp.ReadADC(self.adcnum))
        time.sleep(0.01)

# sort and grab the median value to remove outliers
self.analogreadings.sort()
self.adc = self.analogreadings[4]
Here is a snippet from the log. I ran it with a resistor and with out. Either way, I got the same results. See the Analog Readings item for read results.

Code: Select all

Read Ambient Temperature (TMP36)
Write Ambient Temperature (TMP36) in F to COSM
Analog Readings: [211, 212, 214, 214, 214, 215, 218, 219, 219]
Median Analog Reading: 214
Volts: 0.6896484375
Millivolts: 689.6484375
Temp C: 18.96484375
Temp F: 66.13671875

Read Ambient Temperature (TMP36)
Write Ambient Temperature (TMP36) in F to COSM
Analog Readings: [211, 212, 213, 214, 215, 215, 217, 218, 218]
Median Analog Reading: 215
Volts: 0.69287109375
Millivolts: 692.87109375
Temp C: 19.287109375
Temp F: 66.716796875

Read Ambient Temperature (TMP36)
Write Ambient Temperature (TMP36) in F to COSM
Analog Readings: [211, 213, 213, 214, 214, 215, 217, 218, 218]
Median Analog Reading: 214
Volts: 0.6896484375
Millivolts: 689.6484375
Temp C: 18.96484375
Temp F: 66.13671875
Here is another odd thing that is happening. I also am using an analog temp sensor from AtlasScientific. It's a submersible sensor. When both sensor are wired up and running and when the AtlasScientific sensor touches water, the TMP36 sensor readings drop. I also noticed this when the AtlasScientific sensor touches something metallic.

Here is a snippet from the log when both sensors are running.

Code: Select all

Read Beer Temperature (ENV-TMP)
Write Beer Temperature (ENV-TMP) in F to COSM
Analog Readings: [250, 251, 251, 251, 251, 251, 251, 252, 252]
Median Analog Reading: 251
Volts: 0.80888671875
Millivolts: 808.88671875
Temp C: 20.9022
Temp F: 69.62396

Read Ambient Temperature (TMP36)
Write Ambient Temperature (TMP36) in F to COSM
Analog Readings: [180, 183, 185, 185, one eighty six, 189, 199, 201, 202] # apparently that number is considered spam???
Median Analog Reading: one eighty six
Volts: 0.5994140625
Millivolts: 599.4140625
Temp C: 9.94140625
Temp F: 49.89453125 # dropped 15 degrees!!!!!
Lastly, I attached two pics of my setup.

Any suggestions?
Attachments
Both sensors attached with no resistor
Both sensors attached with no resistor
both sensors attached (no resisitor).jpg (340.53 KiB) Viewed 1840 times
TMP36 sensor only with a resistor
TMP36 sensor only with a resistor
tmp36 sensor only (with resistor).jpg (307.86 KiB) Viewed 1840 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by adafruit_support_rick »

The readings you get from the TMP36 alone do not look particularly inconsistent. They do, however, look as if they are steadily increasing each time to go into your read loop.

What happens if you extend the sampling period - either by increasing the delay between reads or by increasing the number of reads? Do the individual readings eventually level off, or do they continue to increase?

What's happening with your second sensor sounds like it has a short, which is pulling down the system's operating voltage and thus the reference voltage on the TMP36.

daxw
 
Posts: 4
Joined: Fri Sep 28, 2012 5:57 pm

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by daxw »

Thanks driverblock for the response!

You're correct. I really meant that they slowly increase with each new read. I did what you recommended and here are the results.

Increased reading count to 20 at the same 10 ms gap.
Analog Readings: [189, 189, 190, 193, 193, 194, 194, 194, 196, 196, 196, 197, 204, 205, 205, 206, 206, 206, 207]

Increase reading count to 100 at the same 10 ms gap.
Analog Readings: [190, 190, 192, 193, 193, 193, 193, 194, 194, 194, 194, 194, 194, 195, 195, 195, 195, 195, 195, 195, 196, 196, 196, 196, 196, 196, 196, 196, 197, 197, 197, 197, 197, 197, 197, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 199, 199, 199, 199, 200, 200, 200, 200, 200, 200, 200, 200, 200, 201, 201, 202, 203, 203, 205, 205, 206, 206, 206, 206, 206, 206, 206, 207, 207, 207, 207, 207, 207, 208, 208, 208, 208, 208, 208, 208, 209, 209, 209, 209, 209, 209, 209, 209, 210, 210, 210, 210]

Increase reading count to 100 at a slower rate of 100 ms.
Analog Readings: [189, 191, 191, 192, 192, 193, 193, 193, 193, 193, 194, 194, 194, 194, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 196, 196, 196, 196, 196, 196, 196, 196, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 198, 198, 198, 198, 198, 199, 199, 200, 202, 202, 202, 203, 203, 204, 204, 205, 205, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 206, 207, 207, 207, 207, 207, 207, 208, 208, 208, 209, 209, 209, 209, 209, 209, 209, 210]

As you can see, regardless of what I do the analog reading increases with each new read.

The second sensor is rock solid. It continues to read the same temperature the entire time and it's accurate too. It's in water and validated with a mercury thermometer.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by adafruit_support_bill »

Can't tell from the code snippet posted. But are you printing the sample array before or after sorting it?

daxw
 
Posts: 4
Joined: Fri Sep 28, 2012 5:57 pm

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by daxw »

First off, I used the MCP3008 ADC driver from this learning system topic (specifically from Python Script section) http://learn.adafruit.com/send-raspberr ... m/overview.

I then call readadc and calculate temperature with the following code.

Code: Select all

def Read(self):
	"""
	Returns the current temperature as a tuple(C, F)
	or you can get temperatures from the TempC or TempF properties.
	"""
	
	if self.mcp == None:
	        return (self.c, self.f)
	
	self.analogreadings = []
	
	# read sensor nine times with a 10 ms delay
	for i in range(9):
	        self.analogreadings.append(self.mcp.ReadADC(self.adcnum))
	        time.sleep(0.01)
	
	# sort and grab the median value to remove outliers
	self.analogreadings.sort()
	self.adc = self.analogreadings[4]
	
	# convert analog reading to volts (ADC * Voltage / # of possible readings for a 10-bit sensor)
	self.v = self.adc * PlatformVoltage / (2**10)
	
	# convert volts to millivolts
	self.mv = self.v * 1000
	
	# convert millivolts to celsius (from adafruit)
	self.c = (self.mv - 500) / 10
	
	# convert celsius to fahrenheit 
	self.f = ( self.c * 9.0 / 5.0 ) + 32
	
	return (self.c, self.f)
...and then I print the results after (as you've seen in previous posts) with this code.

Code: Select all

print 'Read %s' % self.tmp36Info.name
atTemp.Read()

print 'Write %s in F to COSM' % self.tmp36Info.name
print 'Analog Readings: %s' % atTemp.analogreadings
print 'Median Analog Reading: %s' % atTemp.adc
print 'Volts: %s' % atTemp.v
print 'Millivolts: %s' % atTemp.mv
print 'Temp C: %s' % atTemp.c
print 'Temp F: %s' % atTemp.f

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by adafruit_support_bill »

then I print the results after
So you measurements are not necessarily "steadily increasing". They are probably more random. But since you have sorted them, they are printing out in ascending order. If you leave out the sort step, you should see a more random pattern to the readings.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by adafruit_support_rick »

OK, so the analog readings are printing after they are sorted. I feel better now.

Using the TMP36 alone, I'm going to say that your readings look fine.

When you're switching between the TMP36 and the Atlas Scientific, it looks to me like the ADC needs more time to stabilize. Sorted or not, you can see that it's moving towards the 210-220 range, where it ought to be. This is a common situation with multiplexed ADCs.

A simple thing to try is to do your read from the TMP36 (to switch the ADC) and throw those values away, then delay for about 100ms. After the delay, do your normal read routine.

Using your existing code, it would look something like this:

Code: Select all

atTemp.Read()        # dummy read.  switches ADC (side-effect - allows ~90ms of settle time)
time.sleep(0.1)       # allow another 100ms for ADC to stabilize

print 'Read %s' % self.tmp36Info.name
atTemp.Read()       #the actual read

daxw
 
Posts: 4
Joined: Fri Sep 28, 2012 5:57 pm

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by daxw »

Sorry for the delayed results on this topic. I actually tried what driverblock suggested without any luck; however, it is good advice! Here's what I eventually did to get the TMP36 to have consistent and accurate readings. Simply stated, it needs to be connected very close to the MCP3008 chip. I tried lots of things first before trying this. I ended up ordering a new temp sensor thinking the one I had was bad. I soldered it to a homemade cable about foot long. I found that the readings were even more inconsistent and off from true temp. This is what gave me the idea to solder it as close to the ADC chip as I can. Once I did that then my temp readings were accurate and consistent.

I don't know enough about electronics to understand why exactly. I do know that analog sensors are sensitive to signal degradation when connected via a long wire. I haven't heard any complaints about the TMP36 sensor being so sensitive to distance but then again most of the examples I find are from an Arduino setup and not from a Raspberry Pi with an ADC chip.

In any case, my temp sensor is working well now. I don't know if my conclusion is sound or not. I'm just happy it's working. :-) Oh and I am using driverblock's advice about throwing away the first read and waiting 100ms before getting a true read.

Thanks for all the help!

User avatar
moallen
 
Posts: 13
Joined: Thu Nov 08, 2012 6:23 pm

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by moallen »

I'm also reading tmp36's connected to a mcp3008 with a similar python program but using the standard spi port pins. I've observed a little different phenomina with the tmp36's. When I noticed my calculated temperature reading was about 10% lower than expected, I decided to try different channels on the mcp3008. I soon discovered the same tmp36 sensor could report readings as much as 10% different from channel to channel but consistently the same on the same channel. Naturally I thought internal differences in the mcp3008 channels was to blame. Then I tried using a fixed resistor divider on each channel and obtained exactly the same readings on each channel. That's when I came across the Adafruit postings regarding odd tmp36 issues.

This morning I tried a Phidget 1124 temp sensor ($15.00) on each of the mcp3008's channels. Interestingly, this sensor reports the same reading on each channel. So, it seems my tmp36/mcp3008 issue is related to design characteristics of the tmp36 and how the mcp3008 accesses the tmp36. I suspect the mcp3008 acts somewhat like a multiplexor that cycles from channel to channel which must somehow confuse the tmp36. But still it is difficult to understand why each channel consistently reports a reading so much different from adjacent channels. I suppose once a tmp36 was permanently in place the formula could be customized for each mcp3008 channel.

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by adafruit »

You can also try adding a little more capacitcance between the analog pin and ground, maybe about 0.01 or 0.1uF

User avatar
moallen
 
Posts: 13
Joined: Thu Nov 08, 2012 6:23 pm

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by moallen »

Amazing - a .1uf capacitor between ground and the analog pin completely remedies my problem! With the capacitor directly across the sensor legs about 5" away from the mcp3008, each channel now reads exactly the same. Thanks for the tip :D

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: TMP36 Inconsistent Analog Readings via MCP3008

Post by adafruit_support_rick »

You were picking up electrical noise. The cap damps that out.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”