# Reading Array of Phototransistors With Raspberry Pi

For a project I'm working on, I need to read the values from a matrix of phototransistors (8x16) into the Raspberry Pi.

I'm not exactly sure how to go about doing this, since I know I need to include an ADC somewhere in there. If I use shift registers to read the values from the phototransistors, how can I incorporate the ADC in there?

The problem is the ADC interface. I'm looking at the MCP3008, which apparently uses an SPI style interface. If it was just a simple analog in -> digital out style interface, it wouldn't be a problem.

• Please elaborate on what you mean by digital out. SPI is digital out, so is I2C, as also parallel output interfaces (which will require n GPIO lines for n bit output). – Anindo Ghosh Apr 10 '13 at 21:53
• I mean that when I'm trying to read a value, I want to just be able to use one pin to read from the ADC. As in the ADC has a pair of pins analog in and digital out, without any control lines or whatnot – K. Barresi Apr 10 '13 at 22:05
• I don't understand this question. You need to multiplex over multiple phototransistors with a single ADC. But the main problem is the digital side serial interface of the ADC? Isn't the main problem (or at least the special problem) how to scan the array with a single ADC? And doesn't the Pi have some ADC's already? One dollar microcontrollers have ADC's. – Kaz Apr 11 '13 at 1:03
• A shift register is useless for the phototransistors because a shift register holds only 1's and 0's. To solve the multiplexing using a shift-register-like approach, you would need an "analog shift register": something that samples and holds the voltages and then shifts them toward the ADC. Analog shifters exist: they are called bucket brigades. I don't know of any that let you load the buckets in parallel! No what you want here is an analog switch: a device that takes in N inputs from phototransistors and selects one of them to pass through, based on digital inputs. – Kaz Apr 11 '13 at 1:06
• Kaz, and what you described as an analog switch sounds like exactly what I'm looking for. Would you suggest a specific one for this job? Also, the Raspberry Pi does not have a built in ADC for the GPIO pins – K. Barresi Apr 11 '13 at 2:03

## 2 Answers

To read 8 rows of 16 analog inputs, you should consider using 8 CD74HC4067E analog multiplexer chips. To gather their 8 output voltages, I can think of a few options:

1. Use (part of) a ninth 4067 chip in mux mode. Connect the 8 analog outputs to the first 8 analog inputs of the ninth 4067. This is probably the cleanest, least hacky solution.

2. Use (part of) a ninth 4067 as an 8 bit binary decoder to drive the output enable pins on the other chips, and just tie their outputs together. This would probably be easier to route, and may have lower distortion since the signal is only going through one mux chip instead of two.

Either way, you will have a 7 bit address that you need to generate. If you don't have enough output pins free, you could:

1. User an 8 bit serial in parallel out shift register to drive the output enable pins on the 8 4067's, maybe another one to drive the address pins on the 4067's if you're really desperate for pins.

2. Most "8 channel" ADC's are actually a single channel ADC with an 8 port analog mux built-in. They are usually lower spec, but if it works, one of these, plus the 8 4067's would probably be your lowest part count option.

Depending on your signal levels and how fast you need to operate, you may need to add an op-amp or buffer between the multiplexers and your ADC.

Note, I haven't tried any of these yet, but parts are in the mail (I'm working on something similar).

Update: If you want more noise robustness, you could run the sensing part of your circuit at a higher voltage and use DG506B mux chips instead.

The problem seems to elaborate to a need for acquiring the digital data generated by the ADC, via a single-line interface.

There are several ADCs available, which support such single pin serial line interfaces. For instance:

Basically, one needs to look for ADCs with such a single data pin interface, rather than the SPI device specified in the question.

• Thanks Anindo. So the DS2450, I use a One-Wire library on the RPi to write to the pin (setting the shift registers), and then to read from the pin (from the exact phototransistor set by the shift register)? – K. Barresi Apr 10 '13 at 22:39
• The 1-Wire interface will be used to take data in from the ADC, once the ADC has sampled the analog value from the phototransistor connected to its analog input. No shift register comes into play. What exactly were you hoping for the shift register to do for you? – Anindo Ghosh Apr 10 '13 at 22:54
• I have a matrix of phototransistors (8x16), so I was hoping the shift register could reduce the number of pins to 3 (SPI). – K. Barresi Apr 10 '13 at 22:59
• @user1000229 Nope. What you need then is an analog multiplexer, or actually several of them, and a bunch of additional GPIO lines for controlling them. That merits a separate question, else this will become what is known as a Chameleon Question. – Anindo Ghosh Apr 10 '13 at 23:04