Combined (2x+) ATmega328p

Hi,

I have to do a project where i need more pins, yes i know in this care you will suggest me
a smd atmega chip or other kind of multy pin chip but in my care i have only ATmega328p-pu.

My question is:

Is it possible to combine 2 atmega chips together in the way that I will have same data as out
and input in both atmegas?

What I mean more simple how can i combine both atmegas to work as one?

No.

If you tell us about your project, we probably know many ways to get more pins, or reduce the number of wanted pins.
The most common is a shift register for more output pins.
Or two Arduino (or ATmega328P) communicating via I2C or SPI or Serial. For example the I2C LCD uses an ATmega328P to listen to I2C and controlling the LCD signals.
Or using a matrix for buttons, there is a Keypad library for that.
Or Charlieplexing.
Or ... and so on.

The ATmega328P can be used with the internal oscillator at 8MHz, then the XTal pins can be used as normal digital pins. Also the analog pins can be used as normal digital pins. The absolute maximum is 24 pins for an ATmega328P (but using AREF as input pin is very dangerous, and using the RESET pin is terrible).

I'm gonna use the Atmega328p with a 16Mhz crystal, I need to connect approx:

5x Buttons
3x Pots
2x Power Switch

(Probably I'm gonna use more Switch's and buttons but for the moment that's the max)

1x OLED (SSD1306) (probably 2x)
1x nRF24

That's all what i need to connect and have on the ATmega328p.

That's a really nice example how to reduce buttons pins:

But anyway in case that i need more and more pins how can I connect both atmega's to work
together?

Domino60:
But anyway in case that i need more and more pins how can I connect both atmega's to work
together?

That's entirely up to you.

You can connect them over I2C, SPI, the Serial Pins, Software Serial or bit-banging with GPIO pins.

YOU have to write the code that decides how they communicate and operate with each other.

There is no magic "connect these two pins and use this library" option. YOU have to write it.

Of course, that's a lot of overhead when simple shift register could be used to add more pins.

Domino60:
That's a really nice example how to reduce buttons pins:

But anyway in case that i need more and more pins how can I connect both atmega's to work
together?

This seems quite interesting, can you explain more about this circuit. I've seen this kind of setup for numeric key matrices and I am curious on how to read the values.

Fexduino:
This seems quite interesting, can you explain more about this circuit. I've seen this kind of setup for numeric key matrices and I am curious on how to read the values.

Turn on the internal pull-up resistor and you have a voltage divider that depends on which button is pressed. Use analogRead() to read the values.

This seems quite interesting, can you explain more about this circuit. I've seen this kind of setup for numeric key matrices and I am curious on how to read the values.

Turn on the internal pull-up resistor and you have a voltage divider that depends on which button is pressed. Use analogRead() to read the values.

Yea you read the values with a Analog pin, a simple analog read example will do the work,
and you will get in the serial monitor the values from 0 to 1024. More buttors you will have
the closer the values will be for example.

(You press each button apart you will get a value)

Button 1: 248
Button 2: 475
Button 3: 683
Button 4: 834
Button 5: 987

With that values you can code a if and get the output you want.
The values depend on the resistors and how many buttons you press a time.

(Note: You will not get the same value for button 2 if you press both buttons 1 and 2 in the same time
they will be combined)

This example is good and accurate only if you press a single button a time.

You can connect them over I2C, SPI, the Serial Pins, Software Serial or bit-banging with GPIO pins.

YOU have to write the code that decides how they communicate and operate with each other.

There is no magic "connect these two pins and use this library" option. YOU have to write it.

Of course, that's a lot of overhead when simple shift register could be used to add more pins.

I never tried I2C connection between 2 ATmegas, I know the code is up to me how exactly they gonna work
but in this case i can imagine only to get the serial monitor data from the one Chip and process them
with the other one.

Like this:

(Sensors, buttons..etc) -> ATmega1 -> Data -> Atmega2 -> OUTPUT -> (Leds, ..etc)

This is an introduction to the keypad library : Keypad Library, For Connecting Keypads With Row-Column Matrix Wiring
In a matrix, 6 pins are needed for 9 buttons, or 8 pins for 16 buttons.

I don't know about the OLED. Is that in SPI or I2C or an other mode ? Sometimes a OLED display can be used with other devices on the same bus, sometimes the OLED controller is so quirky that it must be the only device on the bus. The U8glib is able to select a software bus for some devices, that means that any pin can be used.

3 pots to analog inputs, and A4 and A5 for I2C.
One analog input for analog ladder of input buttons.
2 digital input for power switches.
SPI bus and extra for NRF24L01 (4 or 5 pins total).
Use the OLED with I2C if possible (slow).
Pin 0 and 1 for serial monitor and upload sketch (If you decide to use that).
Result: 5 digital pins remaining

Peter_n

Good one but i wanna receive data too so using a OLED and all that just for one ATmega will eat all my
RAM and will crush. I used once OLED and serial monitor and crashed my atmega. Would be better with 2
ATmegas as i said above :

I never tried I2C connection between 2 ATmegas, I know the code is up to me how exactly they gonna work
but in this case i can imagine only to get the serial monitor data from the one Chip and process them
with the other one.

Like this:

(Sensors, buttons..etc) -> ATmega1 -> Data -> Atmega2 -> OUTPUT -> (Leds, ..etc)