ATmega328P not working with SSD1306 SPI

I had recently bought 6 (resistance based) soil moisture sensors and an SSD1306 128x64 .96 I2C display, however since the I2C pins on the ATmega are analog pins I didn't have enough analog pins for the sensors (I could theoretically connect all of them on the same pin and power them individually like I already do to save power but I would need diodes for that, correct me if I'm wrong) and so far I hadn't found an actual working solution for softwire and ssd1306 (i tried u8g2, adafruit_ssd1306's softwire fork, ssd1306 and ssd1306xled) so I decided to use an SPI display so I bought one and I have run into a few issues:

  • ofc it's using more pins so I might not have enough pins to have a pin for each sensor's vcc (i could just connect multiple vcc to the same pin so its not that big of an issue)
  • short answer: it's not working
    • pins are labeled (from left to right) as: GND, VCC, D0, D1, RES, DC, CS
    • tried it with my arduino nano 33 iot and it worked fine but when I connected it to the atmega I just got random dots

If anyone has any idea for
a) how I could set up software i2c with ssd1306
or b) how I could get the screen to work with SPI on the atmega
I'd really appreciate it.

If you had included a link to your particular display it might have been possible to point you towards the mods needed to switch it from I2C to SPI. Since you didn't appear to (that monolithic block of punctuation free text is next to impossible to decipher), it isn't.

Alternatively, you could use an external ADC like the one below, leave the OLED on I2C and still have 2 spare ADC channels.

I don't seem to have any problem with U8g2 running software I2C with an SSD1306, can you post the sketch you were testing with? Its possible you are running low on memory,

You don't need to use a separate software I2C library such as SoftWire, U8g2 has built-in software I2C and SPI support just by using the correct constructor.

You can't.

If it's I2C, that's what it is.

That's the solution. :+1:

You should have them active only when doing a read, since they corrode. Turn them off in between reads, which can be done with a common P- or N-chan MOSFET controlled by the ATmega. If you find yourself in need for new sensors, get the capacitive ones. Just beware of the clones and get branded ones, I found DFRobot to work fine.

As I've said I bought an SPI display after the software I2C was just not working.
I've tried u8g2 software i2c and it didn't work for me.
I'm already turning the sensors on only when I'm using them.

My mistake, I didn't see that. But if you have a SPI display, the SPI are not on any ADC pins. What display is it?

The nano 33 is 3.3V I/O and the atmega is 5V I/O
Is the display designed to work with 5V?

Or you could use an analog MUX and connect them all to one pin.

Then you can use any display you want.

You are referring to an atmega328P, are you using an UNO, a bare atmega328p chip in a DIP package, or some other form (Nano, Mini, Pro Mini, etc)?

Can you post the code you are using with the SPI display? I'm still questioning if your problem is lack of RAM.

Lack of RAM is impossible as the code worked fine with the I2C display I was using before switching to SPI.

The chip is just the atmega328

I'm powering the atmega with the arduino's 3.3v pin so that's not the issue (and the display supposedly works with 5v too).

SSD1306 128 by 64 .96 OLED display (blue but tbh that doesnt really matter). Also I'd like to update the comment, the libraries I tried even if they did work with software i2c they only worked with analog pins as the i2c pins (so it didnt solve anything).

If I remember correctly u8g2 worked with software i2c however only when I was using analog pins for communication.

That is only intended for low current devices.
The atmega will only run on 3.3V if the clock is 8MHz or less.

It is set to 8mhz (its the highest I could go without using external crystals and I'm not planning on doing it since I'm trying to achieve the lowest current consumption possible since the project will run on AA batteries).

Which bootloader did you use?

I don't think I'm using one, I'm using my 33 iot as an isp programmer to program the chip.

It's a lot of information we apparently have been unaware of, like the other display with SPI, what model is it? That's a SSD1306 too? Would you be kind and provide link to datasheet (unless it's the manufacturers which says nothing how the display is configured as by next instance) and / or the sellers page.

That will be a problem because all of the Arduino functions that use a clock will be running at half speed.

I suggest you use Minicore for a 8Mhz 328

I have talked about the SPI display, it is literally the title of the post. The datasheet for it is probably this, however quite frankly I have no idea as the seller didn't provide any information about the display.