BME280 hardware SPI not working on Mega2560 (fixed)

Using the standard BME280 library i ran into a strange issue when trying to use the BME280 from a Mega2560.
Using the stadard library on a Nano the hardware SPI works OK.
Because the BME280 library is using more memory than average i switched to a Mega2560. Just replaced pins 10, 11, 12, 13 to the corresponding 53, 51, 50, 52.
Tried 2 Mega2560 boards. Both just no response at all.
Then switched from hardware SPI to software SPI:

BME280SpiSw::Settings settings(BME_PIN, BME_MOSI, BME_MISO, BME_SCK);
BME280SpiSw bme(settings);

This is working fine.

NO clue what the issue is. If you ran into the same issue then atleast you have a workaround .

PS. I did have a levelshifter between the 5V mega and the 3.3V BME280 spi sensor!

There should be no reason to specify the SPI pins, besides CS.
You are running Software SPI on the hardware pins.

As you mention i started with the Hardware SPI specifiying only CS.

  • On Nano this worked.
  • On Mega it did not so i tried to specify the other pins too. After strugeling for a few days i thought lets give the Software SPI a try. Suppricinly this worked :slight_smile:

And yes i do the Software SPI on the hardware pins. Just because these pins were allready connected.

Now you mention, i now tried the Software SPI on the Mega using 10, 11, 12, 13. Strangly this does NOT work :shushing_face:
At least i have a working config now :slight_smile:

How many level shifters do you have ? For every SPI signal ?

There is nothing wrong with a software SPI. If it works, use it.

For the hardware SPI, the SS pin (53) must be set as output.
It is written near the bottom of this page:

Using software SPI at pins 10,11,12,13 should work, assuming you have a "R3" version of the Mega board.
Perhaps something is not reliable yet. Can you show how the level shifters are wired ?
The level shifters for the I2C bus make the TTL signals weaker.

Can you start thinking about buying a 3.3V Arduino board ? :yum:

Now also the Hardware SPI works on the Mega. :slight_smile:

Many thanks for the tip: I missed the SS pin (53) must be set as output part.

For level shifters i am using a cheap 4-Channel logic voltage level converter. Not sure if you see this as a I2C one. Now it is working i will try to use a resitor only level shifting.

Again many thanks,
Hope other can benefit from this. I spend a lot of time.

Added these lines to the setup:

digitalWrite(53, HIGH); 
pinMode(53, OUTPUT);

To compleet the interface from a 5v arduino to the 3.3v BME280.
I needed 3 level shifters to get it working:
The CLK signal did need an active one: 1 fet and 2 resistors.
For the CSB and MOSI a pasive of 2 resitors worked: 1k8 / 3k3 (18k / 33k did not work)
For the MISO no level shifters is needed.

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