i2c 4 channel ADC

greencardigan

Senior Member
Hello.

I have a MCP3424 which is a 4 channel ADC chip and it's on an arduino shield that I recently purchased.

Info re TC4 shield: http://www.mlgp-llc.com/arduino/public/arduino-pcb.html

MCP3424 Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22088c.pdf

I am trying to write to the configuration register to change the conversion resolution and gain factor but it doesn't seems to be working. When I read the data and config register back it has not changed. However, it sends back 3 data bytes then the config data which seems right for the 18bit conversion.

Here's my test code.

Code:
hi2csetup i2cmaster, %11010000, i2cfast, i2cbyte 'setup for MCP3424 ADC chip

bit7 = 1 'data ready bit N/A

bit6 = 0 'channel selection bits. 00 = channel 1
bit5 = 0

bit4 = 1 'Conversion mode bit. 1 = continuous

bit3 = 1 'Sample rate bits. 11 = 18 bit conversion. Default = 00
bit2 = 1

bit1 = 1 'PGA Gain selection bits. 11 = x8. Default = 00
bit0 = 1

hi2cout 0, (b0) 'write to configuration register
sertxd (#b0,13,10)

do
	hi2cin 0, (b0,b1,b2,b3) 'read 3 data bytes then config register byte
	sertxd (#b0," ",#b1," ",#b2," ",#b3,13,10)
	pause 1000
loop
When running this code I get the following in the terminal window.

159
0 0 16 144
0 0 16 144
.
.
.

144 is the default value for the configuration register.

Any ideas?
 

hippy

Ex-Staff (retired)
A quick look at the datasheet and it doesn't seem to have addressing, so perhaps to configure ...

hi2cout (b0)

And to read ...

hi2cin (b0,b1,b2,b3)

I'd also replace i2cfast with i2cslow in the HI2CSETUP until you have it working.
 

matherp

Senior Member
I haven't got an example for 18bit but the below definitely works using single shot. As per hippy I think your issue is using the address byte.

Code:
symbol ADC = %11010000

	hi2csetup i2cmaster,ADC,i2cfast_4,i2cbyte
' read in third thermocouple with channel 2
	hi2cout [ADC],(%10101011) ' one shot conversion channel 2, gain 8, 16 bit
	wordlow2=5
waitADC2:
	pause 75
	hi2cin [ADC],(wordhigh0,wordlow0,scratch)
	scratch =scratch & %10000000
	dec wordlow2
	if scratch != 0 and wordlow2 !=0 then waitADC2
 

greencardigan

Senior Member
Weird!

Ok, I tried without the address byte and that fixed one problem.

For some reason the 4th returned byte doesnt contain the config data. Instead it is in the 5th byte. I'm confused!?

The datasheet says for 18bit there will be 3 data bytes followed by the config byte.

This is what I'm getting now in the terminal with a K-Type thermocouple attached to channel 1. What is going on with the 4th byte????

159
0 0 0 159 159
0 0 93 31 159
0 0 112 31 159
0 0 124 31 159
0 0 131 31 159
0 0 136 31 159
0 0 141 31 159


Updated code

Code:
hi2csetup i2cmaster, %11010000, i2cslow, i2cbyte 'setup for MCP3424 ADC chip

bit7 = 1 'data ready bit N/A

bit6 = 0 'channel selection bits. 00 = channel 1
bit5 = 0

bit4 = 1 'Conversion mode bit. 1 = continuous

bit3 = 1 'Sample rate bits. 11 = 18 bit conversion
bit2 = 1

bit1 = 1 'PGA Gain selection bits. 11 = x8
bit0 = 1

hi2cout (b0) 'write to configuration register
sertxd (#b0,13,10)

do
	hi2cin (b0,b1,b2,b3,b4) 'read 3 data bytes then config register byte
	sertxd (#b0," ",#b1," ",#b2," ",#b3," ",#b4,13,10)
	pause 1000
loop
 

greencardigan

Senior Member
And the same code but with 16 bit conversion gives this in the terminal window.

155
0 0 155 155 155
0 28 27 155 155
0 33 27 155 155
0 36 27 155 155
0 37 27 155 155

According to the datasheet it should be 2 data bytes followed by the config byte.
 
Last edited:

hippy

Ex-Staff (retired)
According to the datasheet it should be 2 data bytes followed by the config byte.
and then configuration is repeatedly output looking at page 24 and 25, so what you have looks to be correct apart from the first reading. Perhaps it needs a delay after configuration ?
 

greencardigan

Senior Member
Huh???

In the 16 bit example I'm getting 3 bytes before the configuration byte from the 2nd reading onwards. The 3rd byte is not changing.
 
Last edited:

matherp

Senior Member
Which picaxe are you using and what value pullups? I'll try to replicate if I have the same picaxe available
 

matherp

Senior Member
Your code is working perfectly. In the 18 bit example the control byte should be 31 and is (bits 0-4 set). The next byte gives 159 because the not_ready bit is now set (128+31) as you have read the data once.
In the 16 bit example you are getting 27 then 27 +128 = 155, again correct.
best regards

Peter
 
Last edited:

Technical

Technical Support
Staff member
Aren't your later config byte reads invalid anyway, ie bit 7 of the config byte is set (ie RDY bit set, so data is not ready to be read anyway and should be disgarded).

Have you tried the known working code from post 4 for single shot mode yet, if this doesn't work either there is something not quite right.
 
Last edited:

matherp

Senior Member
Just to be absolutely clear, bit 7 is 0 when new valid data is there and 1 when the data is not yet ready. This links with its write use where you set it to 1 to initiate a single shot conversion
 

greencardigan

Senior Member
Arh, thanks for clearing that up. I'll be able to sleep tonight.

I am surprised at how stable the readings are from a thermocouple. Even with 18bit and x8 gain the readings are rock solid.

Now I need to work out how to convert to degrees. I just found out that TCs don't have a linear response. Is a lookup table the usual way to do this? I would like +- 1C between 0 and 350 C.
 

matherp

Senior Member
The MCP 342x range are great chips and always give very reliable readings. I've used them both for thermocouples and even strain gauges without any other signal conditioning.

I use a lookup table for thermocouple calcs and interpolation if required. You need to also measure the cold junction temperature to use as an offset. I've got a program for temps upto around 200C I'll post tomorrow as an example

best regards

Peter
 

greencardigan

Senior Member
I'm using a MCP9800 for ther cold junction compensation.

Last night I tried using the following formula to convert the ADC code to degrees C.
degC = code * 8 / 167

This works reasonably well for positive code values up to 8191 which is about 392 degrees C.

The thermocouple response actually looks fairly linear over the temps i'm interested in.

http://aims.biz/linked/type k thermocouple table.pdf
 
Last edited:
Top