I have 4 pairs of "LED + LDR" sensor. I need to be able to switch each of them on/off independently and take reading from the LDRs. To connect all of them to the microcontroller board, I would need 4 output pins for the LEDs, 4 output pins for the LDRs and 4 input pins for the LDRs. The output pins (also the power supply for the components) turn the LEDs and LDRs on/off while the input pins measure the current/voltage from the LDRs. My microcontroller board has only 12 input/output pins, how do I minimize the pins usage? I know the LDRs can be connected to a single input pin via a multiplexer but how do I connect the output pins to the components? A schematic diagram would be very convenient. Thanks.

  • \$\begingroup\$ As far as i can tell, you need 8 pins. 4 to turn on/off LED+LDR pairs. 4 LDR inputs. So where's the constraint? \$\endgroup\$ – ammar.cma Nov 24 '15 at 22:41
  • \$\begingroup\$ Yes that works. But I need to save as much pins as possible, I need to connect many other components to the microcontroller board as well. \$\endgroup\$ – Umar Zain Nov 24 '15 at 22:46
  • \$\begingroup\$ Okay so you can save 3 pins by multiplexing the input. Now you can certainly employ a shift register to turn on/off the LED+LDR pairs. (THAT'S A GUESS!). If the above is correct, You might end up using only 4 pins for the pairs. \$\endgroup\$ – ammar.cma Nov 24 '15 at 22:50
  • \$\begingroup\$ Do you need an analog measurement? If not, then it's relatively easy to use 2 different kinds of shift registers, one for digital inputs and one for digital outputs, daisy-chained as far as you want to go, all controlled by 4 pins: Bit Clock, Data In, Data Out, and Set Data. An almost perfect match for a SPI peripheral. If you need analog, you might be able to get a SPI-based ADC on the same bus as well (no additional pins), but you also have to consider what the shift registers do while you're accessing the ADC and vice-versa. You could get around that by using a second chip-select. \$\endgroup\$ – AaronD Nov 24 '15 at 23:02
  • \$\begingroup\$ Or perhaps you could charlieplex the LED's. (google that) \$\endgroup\$ – AaronD Nov 24 '15 at 23:07

IMG http://i65.tinypic.com/2liblvm.jpg I came up with this circuit. 4 pins are used. Note: I didn't include the clock connection for simplicity. The microcontroller board has a dedicated output pin of its clock, hence this would not affect the amount of input/output pins used up.

| improve this answer | |
  • \$\begingroup\$ Looks good to me. Actually, I'm pretty sure you can get an 8x2 analog mux/demux that does all of that on one chip. It has two 4x1 circuits in it that can pass current in both directions, and that share the 2 select pins. No clock required, but try to set the current to zero before switching. I've stressed these chips to the point of latent failure by switching them with current. \$\endgroup\$ – AaronD Nov 25 '15 at 1:13
  • \$\begingroup\$ @UmarZain This does not fulfill your requirement to have all combinations (2^4 as you mentioned) available. With this configuration, you can select one at a time. \$\endgroup\$ – Daniel Nov 25 '15 at 18:36
  • \$\begingroup\$ @Daniel My idea is to turn on and read current from the sensor(s) one at a time but switching between them quickly and repetitively. For example if I want to use only Sensor1 and Sensor4, I would provide (via pins AO1 & AO2) an input of 00 to the mux and demux for 1ms, then 11 for 1ms, then 00 for 1ms, then 11 for 1ms...on and on until I need a different sensor combination. It might be less hassling with shift registers but I won't be able to get analog readings directly and faster. I can't think of other methods. \$\endgroup\$ – Umar Zain Nov 25 '15 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.