I2C ADC


99 posts   Page 1 of 4   1, 2, 3, 4
by morphy_richards » Wed May 23, 2012 12:05 pm
Has anyone used anything like this? If I had a collection of 8 analogue values input into this I2C ADC how (in general) could/would the Pi receive the serial digital data?
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by domesday » Wed May 23, 2012 1:30 pm
There is an I2C driver but you will need to re-comple the kernel , see this theread viewtopic.php?f=45&t=2129
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by morphy_richards » Wed May 23, 2012 1:58 pm
I remember seeing that now - a lot of this is really new as far as I'm concerned which is why I didn't put two and two together and go back to it.

Thanks for pointing me in the right direction.
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by Gert van Loo » Wed May 23, 2012 2:54 pm
Gertboard2 demo on youtube shows me reading an ADC and display the value in a 'bar'.
It also shows me using the same input method to control the speed of a motor.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2036
Joined: Tue Aug 02, 2011 7:27 am
by morphy_richards » Wed May 23, 2012 3:04 pm
So would a simpler method be to just use a bog standard SPI say something like this and use that directly with the SPI on the GPIO? I'm not finding too much on the forums that someone like me who hasn't even touched a Karnaugh map for over 15 years can comprehend very easily. viewtopic.php?f=5&t=5125
If something like this chip gets a general nod then I'll get a few and start bodging along with it for the Skutter project and writing up as I go along so other folks can have a go too.

[edit - thanks for that Gert that popped up just as I was posting this comment]
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by domesday » Wed May 23, 2012 3:05 pm
Just for clarity the ADC on the Gertboard uses SPI rather than I2C. Both are serial interfaces, the difference between the two is essentially SPI has higher full-duplex communication speeds where as I2C is slower but slightly easier to implement and uses less wires.
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by error404 » Wed May 23, 2012 8:18 pm
I2C is maybe easier to implement from a system design point of view, but it's a much more complicated and difficult interface to understand/debug/implement at the hardware level.

The hardest part of using an ADC properly is going to be the analog front end. If you want something easy and simple, and don't need a lot of speed, I'd go for an I2C device with an integrated reference much like the one you originally linked. Just take care to condition the input signal properly. It will need to be biased around Vref/2, not exceed Vref/GND and be at least a little filtered to avoid aliasing effects and minimize noise.

Microchip has MCP3008, a cheap 8 channel 10-bit SPI ADC that I've used before, they're even pretty fast, and they come in DIP for easy prototyping. Not I2C, so will need more wires, but if you only need one it shouldn't be anymore complicated to implement. You'll need a reference if you want accurate absolute measurement though.
Posts: 351
Joined: Wed Dec 21, 2011 11:49 pm
by domesday » Wed May 23, 2012 8:35 pm
Personally I would say that I2C is easier to debug as well as I2C also supports slave acknowledgment which means that you can be absolutely sure that you’re actually communicating with something. With SPI, a master can be sending data to nothing at all and have no way to know that.

But at the end of the day I think for the proposed application that is to read sensors on a robot arm I believe then there is little to choose between them other than how well implemented the OS drivers are.
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by morphy_richards » Wed May 30, 2012 7:27 am
domesday wrote:But at the end of the day I think for the proposed application that is to read sensors on a robot arm I believe then there is little to choose between them other than how well implemented the OS drivers are.


In that case I assume that SPI would have the best drivers given that it's the "official" serial interface for the RPi?
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by morphy_richards » Wed May 30, 2012 9:33 am
I'm just trying to work through the datatsheet for the IC suggested by error404...

Image

Firstly, for the analogue inputs on pins 1 to 8 the datasheet recommends using a 1uF bypass cap (which I've added on my diagram, is that correct with them tied between the input and ground like that? Secondly, given that the values I'm using are going to be changing very slowly... glacially slow in fact, are they even necessary?)

Next, using the clock etc. What would be the best way to create a clock signal? Would it be an idea to use a crystal oscillator circuit of some kind? There is a fairly complicated relationship between the pins for CLK, DIN and CS. I've included an image of the timing diagram for this ADC below.

Image

So, we have a constant square wave going into CLK from (some source).

*1 At a time specified by the Pi and coinciding with the trailing edge of a clock pulse we have to drop CS to low to initiate a START command.
*2 On the trailing edge of the next clock pulse we need to drop DIN to low to signify that we want to use single ended input mode.
*3 After this, the next trailing edge clock pulse and DIN specifies D2, then D1 then D0. These 3 bits are used to tell the ADC which analogue input we want to sample.

Not exactly straightforward is it?

My questions are -

Could I do away with a separate oscillator circuit and use three GPIOs to create the clock pulse and to activate CS and DIN as needed? (If so this would make life easy and I can make a fairly straightforward tutorial. (Remember that the sensor / analogue values will be changing with relatively glacial speeds (ie. really really slowly)) and so having quite a slow clock frequency is not really an issue)

If I cant use the GPIO like this ... how the heck can I synchronize CLK, DIN and CS properly?

Ta. :?
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by rurwin » Wed May 30, 2012 9:50 am
Generally, yes, that is how you would do it. But what you describe is just SPI, and if there are drivers for that in the kernel, or even in the GPU, then you don't need to bother with it. It just works. You might have to set Chip Select yourself, but clocking the data out and in will be done for you. Given that there are drivers available, I2C would be even simpler, since it uses addresses sent on the data lines rather than a chip select line per chip.
User avatar
Forum Moderator
Forum Moderator
Posts: 2904
Joined: Mon Jan 09, 2012 3:16 pm
by morphy_richards » Wed May 30, 2012 10:30 am
Thanks Rurwin.

I haven't used an ADC since the mid 1990's at university and this newfangled SPI/I2C is completely alien to me. I'm kind of trawling through until I get an understanding of how it all works. I haven't really done any kind of electronics for about 15 years and it's just the Raspberry Pi and the opportunities given by the GPIO and these various types of interface for making integrated computer and hardware projects which is even more exciting than the old "user port" was on the good old BBC Micro which have rekindled this. That and as an ICT teacher since 1999/ head of department who originally did electronics and computer science who is absolutely delighted at the opportunity to teach real computing and mix it with my other old love which is the kind of electronics I used to do before going to university and being overwhelmed with the amount of signal processing and calculus it involved. And I get to call it "work" which is great! And if these computing and control schemes of work I am working on are inspiring and exciting as well as good I share them all for free under whatever licensing is most appropriate to let people just use it. :D But I am pretty noobish now and you guys who work with this stuff day in day out (I assume) perhaps overestimate my understanding.

Respectfully, you say "You might have to set Chip Select yourself, but clocking the data out and in will be done for you" but I have no idea how that would work. To my uninitiated mind the first port of call is to examine the data sheet for a SPI ADC (or A2D which is just as obscure as far as I'm concerned) and then try to work out a way to enable the requirements.

If you are saying I don't need to worry about the clock and "DIN", as the kernel provides them, then how do I do the manual wiring up bit? Surely there needs to be a clock input from, somewhere on the Pi, but where does it come from? And if it's part of the Pi's SPI (Or A2D if I use that method) how do I seperate it out to feed to the appropriate pin?
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by Gert van Loo » Wed May 30, 2012 10:59 am
Sorry to repeat myself but if you look at the Gertboard demo code there is C-code which talks to an MCP3002 over the BCM2835 SPI interface. (No need to do any bit-banging, just make sure you use the correct chip select pin as there are two). That is very, very similar to the MCP3008. Both take 5 command bits . There is just a slight difference in how these bits are interpreted as one is a 2 channel and the other is an 8 channel ADC. Just put the data sheets side-by-side (MCP3002 page 17 and MCP3008 page 20) and you will see what I mean.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2036
Joined: Tue Aug 02, 2011 7:27 am
by rurwin » Wed May 30, 2012 11:35 am
morphy_richards wrote:If you are saying I don't need to worry about the clock and "DIN", as the kernel provides them, then how do I do the manual wiring up bit? Surely there needs to be a clock input from, somewhere on the Pi, but where does it come from? And if it's part of the Pi's SPI (Or A2D if I use that method) how do I seperate it out to feed to the appropriate pin?


Take a look at the bottom of this diagram of the GPIO connector.
Image

On the left there are the Input and output data and the clock, and on the right are two signals to be used as chip select.
User avatar
Forum Moderator
Forum Moderator
Posts: 2904
Joined: Mon Jan 09, 2012 3:16 pm
by morphy_richards » Wed May 30, 2012 12:23 pm
Thank you for your patience gentlemen.

Now I see why I2C should be so much simpler to use. And I can just, in essence, just have as many interfaces as I want in parallel.Image And it just ... works ...
(as long as I have installed / recompiled whatever drivers etc as necessary)
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by Gert van Loo » Wed May 30, 2012 12:57 pm
yes, that is why a lot of people use I2C: minimum number of wires.
SPI has a the advantage of speed but if you don't need that I2C is simpler.

The part where I am rather unhappy with is the I2C identifier system. What I read from it, it is rather broken in that most of the devices your are dependent on the ID set by the manufacturer. You can add one or two ID bits but that is it. There are some 'agreement' on I2C device types but that seem rather ad-hoc.

Last note: Do not add pull-up resistors. The Raspberry-Pi boards already has them on GPIO0 and GPIO1.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2036
Joined: Tue Aug 02, 2011 7:27 am
by morphy_richards » Wed May 30, 2012 2:51 pm
Gert van Loo wrote:The part where I am rather unhappy with is the I2C identifier system. What I read from it, it is rather broken in that most of the devices your are dependent on the ID set by the manufacturer. You can add one or two ID bits but that is it. There are some 'agreement' on I2C device types but that seem rather ad-hoc.


Looking at the pin layout for a ADS7830 I see 2 pins, A0 and A1 which in the data sheet are tied to ground. I see these are referred to as "slave address". Would these be what you are referring to? You can use these to set up to 4 "slave addresses" for this particular model.

If you wanted more than 4 of these devices, you would need to rely on whatever the manufacturers address is. Given that I would be using 4 or more of these ADS7830 devices which would all have the same ID set by the manufacturer (I assume) then collisions would be inevitable unless we make the software do something clever.?
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by domesday » Wed May 30, 2012 3:04 pm
I would imagine that the 2 slave address pins could be configured to give 4 distinct addresses, as they will be binary selectors i.e numbers 0,1,2,3.
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by morphy_richards » Wed May 30, 2012 3:40 pm
But if there are more than 4 devices and they are all the same device type we run into a sticky patch.
(actually working it out I will only need 2 devices so its a moot point but still important to be aware of it)
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by yeahbox » Wed May 30, 2012 8:15 pm
domesday wrote:I would imagine that the 2 slave address pins could be configured to give 4 distinct addresses, as they will be binary selectors i.e numbers 0,1,2,3.


Yes and no :) I briefly read the posts above and I assume that you are talking about the SPI interface. Chip select in SPI terms really physically uses one dedicated line/signal/pin for each slave device. So with two pins you can select two different slave devices. What you are suggesting can of course be done but you need some logic circuits connected after these two pins then to make your binary math stuff. Secondly you'll definitively would need to change the SPI driver in the linux kernel so that it would be aware of your new number of available "chip select pins". But if you really want to go there then it is probably easier to use some of the other GPIO pins as chip select. Again that would probably mean that the SPI driver needs some work.
Posts: 36
Joined: Sun May 27, 2012 10:35 pm
Location: Manchester, UK
by Gert van Loo » Wed May 30, 2012 10:15 pm
We are talking I2C addresses. So yes, you can add 4 devices and that is it.
What I understand from the I2C documents is devices addresses are in 'groups'. So all I2C EEPROMS have the same top ID bits (plus again one or two LS ID pins), then all ADC's have the same ID etc.
So you have to problems:
The one above where you can add a number of devices and that is it.
At some point you will run out of top bits for the 'classifications'.

But as I said, I may be wrong as I have always avoided using I2C, just came across some datasheets.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2036
Joined: Tue Aug 02, 2011 7:27 am
by mahjongg » Wed May 30, 2012 10:26 pm
True the address of a I2C device is divided in groups, one group is assigned internally to the device, and is normally the same for all similar devices, but the other group of bits can be selected through pins on the device, normally there are two or three of these pins named A0, A1, and sometimes A2.
If you want to address more than four I2C ADC chips, you will need one with an A2 pin, so you can select eight such ADC chips.
There are probably also I2C chips with multiple ADC,s.
User avatar
Forum Moderator
Forum Moderator
Posts: 5328
Joined: Sun Mar 11, 2012 12:19 am
by wallabybob » Wed May 30, 2012 11:28 pm
domesday wrote:I would imagine that the 2 slave address pins could be configured to give 4 distinct addresses, as they will be binary selectors i.e numbers 0,1,2,3.

Some I2C devices provide for additional address bits by allowing more than two options for the address input on the device: for example, the address input might be left floating OR connected to ground OR connected to the supply rail. Another variant I have seen is to allow the address inputs to be connected to I2C SDA OR SCK OR ground OR supply.
Posts: 17
Joined: Thu Aug 18, 2011 2:08 am
by kaos » Sun Jun 03, 2012 11:24 am
morphy_richards wrote:Firstly, for the analogue inputs on pins 1 to 8 the datasheet recommends using a 1uF bypass cap (which I've added on my diagram, is that correct with them tied between the input and ground like that? Secondly, given that the values I'm using are going to be changing very slowly... glacially slow in fact, are they even necessary?)


I think this is some misunderstanding. Bypass caps of this caliber are usually applied to the power supply pins of a device. In this case maybe also the Vref pin. In your case, with very slowly changing values, using 1uF caps on the analogue inputs will probably not hurt anything, and may indeed help with filtering out transients, but be aware that they will affect measurement of even moderate frequency signals. To what degree will depend on the signal source impedance and frequency.

--
Kári.
Posts: 102
Joined: Mon Mar 26, 2012 8:14 pm
by morphy_richards » Thu Jun 28, 2012 2:30 pm
Hi,
Just need to check something ... if I'm using a ribbon cable a'la Gert's video to use the GPIO does this translate the GPIO to this?

Image
So, for I2C I would need to use 3 and 5.

To supply power to, say, a potentiometer and a pcf8591p 8bit I2C A-Dconverter I would take the power from 2 and ground at 6?
User avatar
Posts: 860
Joined: Mon Mar 05, 2012 3:26 pm
Location: London