Use shift register with OLED display.

Hey everyone,

I’m using an Atmega328-PU IC on a breadboard (barebones arduino) and I’ve exhausted the output pins with buttons and LED’s.

I’m currently using 3x 7 wire SPI OLED displays. They all share the DC, MOSI and SCK lines and have individual reset and CS pins.

I’m trying to add a 4th OLED and I wondered is it possible to use a shift register on the reset and CS Pins to move 8 pins down to 3?

I’ve researched and can’t seem to find an answer anywhere.

Thank you!

Yes, that should work in terms of the hardware. Also, you would wire the shift register to SCK and MOSI, so only one extra pin needed for LATCH. But you would have to change the library code to be able to select the displays via the shift register. It might be easier to use the shift register for your LEDs or something to free up pins for the displays and avoid changing the library. Don't forget the overall current limit of 70mA, assuming it's a 74hc595.

But there's an easier way. Connect all the OLED reset pins to the Arduino reset pin. That will free up 3 pins. When initialising up the displays in setup(), put U8X8_PIN_NONE as the reset pin for each display (assuming you are using u8g2 or u8x8 libraries; other libraries will probably have an equivalent of U8X8_PIN_NONE).

I’ve looked into it and it’s tough to multiplex the LED’s or buttons.

I have 4 buttons and 4 LED’s connected to pins 2-9, most multiplexers I’ve found so far turn 4 pins into 8 and I need 4 pins for each anyway.

10 - CS
11 - MOSI
13 - CLK

Then I’m using A0-A5 for the resets and DC connections for 3 OLEDS.

I like your solution of using the reset line on the arduino for the OLED resets. I shall have to research further!

Also, I am using the U8G2 library for the OLED displays.

Thank you so much :slight_smile:

LCOLLINS1989:
I’ve looked into it and it’s tough to multiplex the LED’s or buttons.

Why do you say that? If you can connect up 3 or 4 OLED displays using the SPI bus, then connecting up leds and buttons should be easy.

LCOLLINS1989:
I have 4 buttons and 4 LED’s connected to pins 2-9, most multiplexers I’ve found so far turn 4 pins into 8 and I need 4 pins for each anyway.

I don't follow your "4 into 8" explanation.

There are many ways to expand inputs and outputs. 74hc595 is an easy way to add outputs. You can chain several of them together, giving you 8, 16, 24... extra pins and they share the same LATCH pin, and of course share the MOSI and SCK pins with the displays. If you need more inputs, you can use 74hc165 shift registers. These can also be chained and use the same LATCH pin as the '595s. They can share the SCK pin with all the other chips but connect to MISO rather than MOSI.

Another option is mcp23s08 or mcp23s17 chips. These give you 8 or 16 extra inputs or outputs in any combination you need. They also connect to MISO, MOSI and SCK and will need their own Arduino pin for the CS pin. There is an Arduino library you can use with them that allows you to use pinMode(), digitalWrite() and digitalRead() for the pins on the expander chip, just like they were Arduino pins.

Ok, this shows how new and self taught I am at this.

It never occurred to me that a multiplexer can share the same MOSI and SCK pins as the OLED displays.

I thought I’d be using a multiplexer for the inputs that took 4 pins to use 4 buttons and a multiplexer for the outputs that took 4 pins to use 4 LED’s.

If I can share the MOSI and SCK that’s easier still!

Thank you again. I have a lot to learn and I now feel I’m heading the right way!

I think you may be confusing "multiplexer" and "shift register". 74hc595 & 165 are shift registers and can share the MISO/MOSI/SCK pins.

Multiplexers like cd4051 can also be used to connect many buttons to a single Arudino input, but they can't share MOSI/MISO/SCK. Also, you could use them to multiplex multiple LEDs, but you could only light one led at once.

Thank you so much,

Yes it would appear I’m confusing the two!

I’m going to try tying all the oled reset pins to the arduino reset and if that doesn’t work for some reason I’ll buy a shift register and use it for the buttons/LED’s.

Thank you again

LCOLLINS1989:
if that doesn’t work for some reason I’ll buy a shift register and use it for the buttons/LED’s.

One reason it might not work is if the timing of the Arduino's reset signal is not right to cause the OLEDs to reset. But its definitely worth a try.

More options to consider:

For 4 buttons, you can read them with one analog input and some resistors.

If only one of your 4 leds is ever lit at once, this can be done with 3 Arduino outputs, saving a pin, using a technique called Charlieplexing. Sounds slightly scary, but not too difficult for only a small number of leds, and an interesting thing to learn about.

Sadly I need full control over the led’s. Also need the possibility to press two buttons at the same time.

Thank you again for your input Paul. Definitely pointed me in the right direction on what to research :slight_smile:

Ok, in that case I would recommend the mcp23s08 option. One chip will do your 4 inputs and 4 outputs, and will "play nice" with the oled displays. If you need more inputs/outputs, you can add a few more mcp23s08 without needing more Arduino pins, I think.

Great thank you!

I have ordered some 74HC595 chips and some mcp23s08 chips to play around with.

Thanks again for your help!

PaulRB:
If only one of your 4 leds is ever lit at once, this can be done with 3 Arduino outputs, saving a pin, using a technique called Charlieplexing. Sounds slightly scary, but not too difficult for only a small number of leds, and an interesting thing to learn about.

Actually, remember that "only one ever lit at once" is relative. Multiplexing of displays as visual indicators is performed by rapidly switching on different LEDs at a rate faster than the eye will discern as flicker, so even if only one LED is on at any given time, each of four can be lit in sequence at a 100 Hz cycle frequency. The only disadvantages are having to code for it and that you will only get one quarter of the brightness however with modern LEDs at 20 mA for a quarter of the time, that is quite bright. Three output pins can Charlieplex six LEDs.

And using resistors to encode four buttons into one analog input, any or all of the four can be pressed simultaneously and be individually distinguished.

Of course the MCP23S08 or the cheaper PCF8574 connected to your I2C bus on A4 and A5 will very neatly deal with 4 LEDs and 4 buttons with resistors only for the LEDs, or the PCF8575 would give you 16 I/O.

Just to update. Tying the reset line of the OLED screens together and then to the Arduino reset worked perfectly!

Thank you both of you!