User avatar
scottchiefbaker
Posts: 20
Joined: Tue Jan 10, 2012 9:52 pm
Location: Canby, OR
Contact: Website

GPIO Analog pins?

Tue Aug 07, 2012 10:43 pm

Are all of the GPIO pins digital? I'd like to read an analog signal from a photoresistor, or a potentiometer, and that will require an analog input.

User avatar
jbeale
Posts: 3266
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: GPIO Analog pins?

Tue Aug 07, 2012 10:54 pm

Yes, they are all digital. BUT, you can use a simple circuit as a "poor man's A/D" for example, by using a resistor and a capacitor in parallel. You turn on the GPIO pin as an output to charge up the capacitor, then you reconfigure the GPIO output to an input (high impedance) and time how long it takes for the voltage on the capacitor to drain down (due to the resistor) and cross the logic threshold from 1 to 0. That time value is proportional to C and inversely proportional to R, so you've done an analog measurement using only a digital I/O. The R might be a CdS photocell, for example, in which case you've measured a light level. Or instead of a resistor, it could be a current into a phototransistor. For example, the Pololu #959 QTR-1RC reflectance sensor is used in this way, see the schematic at http://www.pololu.com/catalog/product/959

If you prefer to use a traditional ADC chip you can certainly do that, connecting to the R-Pi via SPI or I2C. For example, I like the Microchip MCP3424 (I2C interface) which has 4 differential inputs and can be configured for one of four different data rates & resolutions:
- 3.75 SPS (18 bits)
- 15 SPS (16 bits)
- 60 SPS (14 bits)
- 240 SPS (12 bits)
YOu would need to write the control software though. I have used this chip with the Arduino, but not yet for the R-Pi. You can get one assembled on a board here: http://shop.moderndevice.com/products/j ... nalog-plug
Last edited by jbeale on Tue Aug 07, 2012 11:01 pm, edited 1 time in total.

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:01 pm

thats very inventive - I would be very interested to see if anyone can demonstrate it working on the pi

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9563
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:08 pm

Its a technique very common on many microcontrollers, but may be less suited for one running Linux!, as its not a "real time OS", it can happen that your code stops running for many milliseconds, as the scheduler gives other tasks priority.

The PI surely doesn't have any true analog inputs, for that you are required to use I2C or perhaps SPI interface chips. (the former for slow but multiple I'O's the latter for faster I'O's). If you want to connect a real (I2S or AC97) codec, that can be done too, but requires you to solder wires to the board for an I2S interface. for most using USB peripherals will prove to be less challenging.

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:10 pm

good point about linux timing - it would be an ideal experiment for something like DexBasic

I'm trying to get this rinky dinky board working

Image

- it has analoge inputs via I2C

(note to self - google I2S & AC97)
Last edited by pygmy_giant on Tue Aug 07, 2012 11:20 pm, edited 1 time in total.

User avatar
jbeale
Posts: 3266
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:19 pm

The all-digital "analog input" should work on the R-Pi, except that the timing is not as good as a microcontroller due to the available Linux versions not being true real-time. If you make your time constants longer than 10 milliseconds or so (not microseconds) I think it should work. It would likely work best as a kernel-driver level, rather than in user space. My experiments with NTP-PPS timing in the 3.1.9 kernel with a relatively unloaded system indicate that my R-Pi has an interrupt latency ranging between 10 and 80 microseconds, and occasionally gets as long as 150 microseconds (see below). Timing delay is random but you can just try it several times, and filter out the outliers. The distribution is decidedly skewed (not a symmetric gaussian) since you can have short delays, or very long delays sometimes, but of course the delay is never less than 0. The plot shows offsets around a long term average "0 mark" arbitrarily set to zero, but true zero delay on this graph would probably be near -0.04 msec.
RPi-PPS-timing.png
RPi-PPS-timing.png (19.63 KiB) Viewed 74121 times

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:31 pm

I think you can reduce this unpredictability by doing something in bash to give the program high priority - haven't tried it though.

User avatar
jackokring
Posts: 815
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:39 pm

Theoretically, if the output of one pin drove a filter, and its voltage float was compared to the voltage to sample using an op-amp/comparator, then absolute timing is not as essential. If the sampling of the input bit, was synchronized with the output bit being filtered, and within the interrupt handler also close in time, an actual fast clock tick was sampled, then using a calculation, the digitally estimated voltage which is being compared, can be simulated on chip, and knowing the input bit, an accurate estimate of the actual input voltage can be made. Very high low frequency accuracy!!
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9563
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: GPIO Analog pins?

Tue Aug 07, 2012 11:45 pm

All I/O devices that work on any desktop system must be able to overcome millisecond hitches, as otherwise they simply would not work on any such systems (whether Windows/ OSx or Linux based it doesn't matter).

So all "soundcard systems" and such have (FIFO) buffering buit in just to overcome this issue.

User avatar
jbeale
Posts: 3266
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 12:17 am

pygmy_giant wrote:I think you can reduce this unpredictability by doing something in bash to give the program high priority - haven't tried it though.
If you're talking about my PPS graph, that data is coming from an interrupt-driven kernel module (pps-gpio) which records the system time when a rising edge occurs on a GPIO pin, in this case the PPS signal from an external GPS unit. I don't know if a kernel module can be given any higher priority than it already has (?)

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Wed Aug 08, 2012 12:20 am

err... sorry - I meant upping the measuring program's process prioraty to stop it getting interrupted - I think its the linux 'chrt' command - something like chrt -r -p 99 [pid] ...?

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Wed Aug 08, 2012 1:03 am

thats in userspace

User avatar
Grumpy Mike
Posts: 873
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 12:44 pm

jackokring wrote:Theoretically, if the output of one pin drove a filter, and its voltage float was compared to the voltage to sample using an op-amp/comparator, then absolute timing is not as essential. If the sampling of the input bit, was synchronized with the output bit being filtered, and within the interrupt handler also close in time, an actual fast clock tick was sampled, then using a calculation, the digitally estimated voltage which is being compared, can be simulated on chip, and knowing the input bit, an accurate estimate of the actual input voltage can be made. Very high low frequency accuracy!!
Have you actually tried this? The explanation is a bit muddled, exactly what do you mean by:-
If the sampling of the input bit, was synchronized with the output bit being filtered,
And what do you mean by "voltage float"
From what I can gather of this suggested technique you do need accurate timing. Can you explain more clearly what you mean.

User avatar
TonyD
Posts: 439
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 1:05 pm

pygmy_giant wrote:good point about linux timing - it would be an ideal experiment for something like DexBasic

I'm trying to get this rinky dinky board working

- it has analoge inputs via I2C

(note to self - google I2S & AC97)
Cool looking board, what does it do?
Tony

User avatar
jackokring
Posts: 815
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: GPIO Analog pins?

Wed Aug 08, 2012 1:25 pm

Grumpy Mike wrote:
jackokring wrote:Theoretically, if the output of one pin drove a filter, and its voltage float was compared to the voltage to sample using an op-amp/comparator, then absolute timing is not as essential. If the sampling of the input bit, was synchronized with the output bit being filtered, and within the interrupt handler also close in time, an actual fast clock tick was sampled, then using a calculation, the digitally estimated voltage which is being compared, can be simulated on chip, and knowing the input bit, an accurate estimate of the actual input voltage can be made. Very high low frequency accuracy!!
Have you actually tried this? The explanation is a bit muddled, exactly what do you mean by:-
If the sampling of the input bit, was synchronized with the output bit being filtered,
Not on the pi which has not arrived, No. The GPIO input pin used needs sampling at a known time interval from the GPIO output bit used.
And what do you mean by "voltage float"
Any high impedance node in an electronic circuit "floats". Maybe the filtered GPIO output would have been better. If your asking these questions, then designing an IIR filter may be a step too far at the moment. Look up "delta sigma converter ADC schematic" for a rough principals introduction.
From what I can gather of this suggested technique you do need accurate timing. Can you explain more clearly what you mean.
Yes accurate timing is required. Knowing the exact time is not, finding the number of microseconds since the driver last ran is accurate enough. Maybe I should make a circuit based on a LM327CN when my Pi arrives, and download kernel source to make a custom device driver.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Wed Aug 08, 2012 2:59 pm

TonyD said:
Cool looking board, what does it do?
It does this: http://www.nsd.uk.com/miscellaneous/I2C_FLEXEL.pdf

User avatar
scottchiefbaker
Posts: 20
Joined: Tue Jan 10, 2012 9:52 pm
Location: Canby, OR
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 3:06 pm

Is that just a board that lets you talk i2c to all those different sensors? That's pretty cool if it is.

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Wed Aug 08, 2012 3:23 pm

Why yes - it will be if it stil works after I've soldered on all those itty bitty pins... it runs at 5v so it needs a logic level convrter so it does not fry the Pi... otherwise it will make a nice key ring or piece of jewllrey... it cost £12 off of ebay

User avatar
scottchiefbaker
Posts: 20
Joined: Tue Jan 10, 2012 9:52 pm
Location: Canby, OR
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 3:27 pm

Ultimately I just want to be able to read a couple of photoresistors, a DS18B20 temperature sensor, and DHT11 humidity sensor. I have an Arduino setup doing environmental monitoring with those sensors, and a Linux box that polls the Arduino once a second and puts the data on a web page. The pi would be a perfect replacement, because it already has ethernet and a web server.

Would this application be something that's more suited for the Gertboard?

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: GPIO Analog pins?

Wed Aug 08, 2012 3:33 pm

Not sure - I think the gertboard could do that but as it has not been officially unveiled I'm not certain of the spec - I think the gertboard is kinda like an old telephone switch board where you jumper things together in your desired configuration, rather than a circuit as such...

... why not connect the arduino to your pi ..?

User avatar
scottchiefbaker
Posts: 20
Joined: Tue Jan 10, 2012 9:52 pm
Location: Canby, OR
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 3:41 pm

That's an option too. Ultimately I was just trying to make my installation simpler... i.e. one platform. The adruino is great, but if I ever need to move a wire, I have to recompile the code and tell it to look at a new pin. The pi would be more flexible, in that I could update the code without standing right next to the thing with a USB cable to update the code.

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 4:20 pm

Not sure if this as been post but here goes
http://learn.adafruit.com/basic-resisto ... i/overview

I will convert that python code to DexBasic, it would be interesting to test both and see if theres much difference in out put.
Batteries not included, Some assembly required.

User avatar
Grumpy Mike
Posts: 873
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: GPIO Analog pins?

Wed Aug 08, 2012 6:30 pm

jackokring wrote:
Not on the pi which has not arrived, No. The GPIO input pin used needs sampling at a known time interval from the GPIO output bit used.
Then you should not be advising people on things you have no knowledge of.

Any high impedance node in an electronic circuit "floats". Maybe the filtered GPIO output would have been better. If your asking these questions, then designing an IIR filter may be a step too far at the moment. Look up "delta sigma converter ADC schematic" for a rough principals introduction.
Yes using the right words is often better when giving advice. You are still not using the right words but those are better. Oh and by the way I was in the design team that produced the audio filters for the Polaris Submarine and delta sigma converters I first encountered in the 70s doing PhD research.
Yes accurate timing is required. Knowing the exact time is not
This is not what you said in the original post.

When you get your Pi, and finally get to use it, and acquire a tiny bit of experience you will find that the one thing you can not do under Linux is to grantee precise timing intervals.

Why do you feel compelled to give advice on things you have no experience of?

clubcsl
Posts: 7
Joined: Sun Sep 23, 2012 6:54 pm

Re: GPIO Analog pins?

Sat Oct 06, 2012 10:59 pm

DexOS wrote:Not sure if this as been post but here goes
http://learn.adafruit.com/basic-resisto ... i/overview

I will convert that python code to DexBasic, it would be interesting to test both and see if theres much difference in out put.
Have you tried out this ?
I am thinking to try it as I want to get the input from a temp sensor.

Thanks

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9563
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: GPIO Analog pins?

Sat Oct 06, 2012 11:39 pm

This should work!

Its actually an old trick, used in many microcontroller boards, * and adafruit is a very reliable outfit, if they say it works you can be assured it does, and the language used should not matter so much, as long as it can access the GPIO's and is reasonably fast.

Obviously the latency problems generated by the multitasking nature of Linux prevents very fast and accurate timing, but for a signal that is slow enough this isn't a problem.

*one well known example are the famous "basic stamp" microcontroller boards, which even have a basic command ( RCTIME) for this function built in. Also on the Propeller microcontroller (where dedicated software running in on of its coders simulates all kinds of hardware interfaces) this trick is even used as an A/D converter that is able to convert sound this way.

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 9 guests