Multiplexing ports for other uses than leds

Hi,
I building a control unit for my Tube amplifier project. The tube amp is ready but to control the inputs I am using the following parts

-5 relays
-Rotory encoder with a switch
-IR remote module
-2 LED rings (WS2812)
-3 MCP4151 digital resistors to adjust the pre amp per source

I am using a ATMEGA328 controller.

The issue I am having is that the number of digital ports is too low to operate all these modules. Is have looked at the Shift registers like 74HC595, but I only see examples for led arrays. How can I extend the number of digital ports of the ATMEGA. Ideally I would like to have an extra 8 to 12 digital ports which I can address for the devices above.

I hope someone is able to help me out here. Thank you!!

Please help me count

  • 5 relays = 5 pins
  • 1 rotary encoder + switch = 2 + 1 pin
  • IR remote = 1 pin
  • 2 WS2812 = 1 pin
  • 3 MCP4151 = SPI (3 pins) + 3 CS signals

That's a total of 13 pins plus the SPI signals. The ATmega328 has 20 IO pins; remember that the analogue pins are also digital IO pins.

So what am I missing?

Hi Sterretje,

The three MSP4151 are using a total of 9 pins, they do not allow using the analogue pins. On top of the list I am also using a MSGEQ7 spectrum analyser. So yes, the total of 20 ports in not enough. Any thoughts?

They should be using only 6, as explained by @sterretje. What are the other 3 pins?

That is not what @sterretje was suggesting. He suggested using the pins that can be analog pins (A0 to A5) as digital pins because they can also be digital pins. Only A6 & A7, if your ATMEGA328 even has those, cannot be used as digital pins.

Are there any other components you have forgotten to mention?

I would suggest using a 74hc595 to control the relays. This will require only 1 additional pin, so will be an overall saving of 4 pins.

Oke, thanks for your thoughts. I was puzzeling with a 74HC595 and a 74HC165 to control all inputs and outputs. Another advantage of this approach is that my PCB becomes much more efficient. I can install a flat cable above the shift registers. Wiring all the outputs from the ATMEGA is not very efficient on a relative small pcb. However, I was hoping just address the additional ports in my code, but found that this is less simple than I anticipated on.
If the shift registers are not an option I have to reconsider my wiring I suppose.

5(D2-D6) + 3/4(D7-D10) + 1(D11)+ 2(D12, D13) + 6(D14-D19) = 18
ATMEGA DIP has 20 digital. Leave 0/1 for Serial debugging.
HTH

Do not worry I can count... Perhaps I needed to phrase my question differtently.

Is it possible to receive the input the MCP4151, the IR remote module or a Rotary encoder with a 74HC165? This is what I was wondering...

search for Portexpanders.

MCP23017
MCP23S17
MCP23008
MCP23S08
PCF8574
PCA9685

and many more.

Possibly. Do you need to hack the libaries? Yes; or write your own ones. I think that the IR would be the trickiest one.

An Arduino Port has 8 Pins. On an Uno if you use Serial, your free PINS are 6 PINS each on 3 PORTS.

2 Pins of Port B, PB6 and PB7 are used by the clock source.
2 Pins of Port C are not wired.
2 Pins of Port D and Serial RX and TX.

Every free Port Pin may be used as a Digital Pin and many Pins can also be used in another way like ADC Input or PWM Output.

It is possible to address Arduino Pins by Port Letter and Pin Number so please notice the difference. Moreover, AVR Porst have 3 registers each that you can operate directly much faster than through Arduino-safe functions.

But you need to be able to tell Ports from Pins.

ATmega168 pin map is the same as the Atmega328/88/48 chips.

You can connect all your SPI chips (74HC595, MCP4151, ...) to the hardware SPI pins (SCK = D13, MISO = CIPO = D12, MOSI = COPI = D11) and use a 74HC138 to decode (demultiplex) the /CS: only 3 digital pins for up to 8 peripherals. Use 2 of them or one 74HC154 and 4 digital pins to access up to 16 peripherals. Using pins D14-D17 will even allow you to write PORTC[0-3] directly.

If the IR is analog, you can use an ADC (analog to digital converter), in series with the shift register. I have done it once, it is possible but not really practical. If I am correct, you can use analog port expanders like this one: https://numato.com/product/digital-and-analog-io-expander-shield/. Or you can make it yourself by using multiple IC's.

Thank you all for sharing your thoughts. At the end I have used a 74HC595 to operate all the output signals and left enough ports open for the other devices including the RX and TX to upload new code to the ATMEGA without replacing it to a separate Uno board.
The PCB is in production, looking forward building it up and start testing. The Tube Amplifier in a real glass cabinet will be looking great!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.