BMP280 via SPI

Hello, I am using gy-bmp280 sensor for pressure readings which works fine when i use I2C and the bidirectional level shifter ( which i made myself by following the circuit online ) but when i switch to SPI mode it doesn't work. I am using adafruit bmp280 library.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)

//Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

void setup() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));

if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}

/* Default settings from datasheet. /
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /
Operating Mode. /
Adafruit_BMP280::SAMPLING_X2, /
Temp. oversampling /
Adafruit_BMP280::SAMPLING_X16, /
Pressure oversampling /
Adafruit_BMP280::FILTER_X16, /
Filtering. /
Adafruit_BMP280::STANDBY_MS_500); /
Standby time. */
}

void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");

Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");

Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");

Serial.println();
delay(2000);

I made the level shifter using this circuit.
Source - Bi-Directional MOSFET Voltage Level Converter 3.3V to 5V'
I checked the voltage levels so they are perfectly fine. So , my concern is if I2C works then why SPI is not working ??

mosfet_level_converter.jpg

tech7:
but when i switch to SPI mode it doesn't work.

What does "it doesn't work mean"?

Where's the complete schematic of your setup?

doesn't work means it prints error message from the code i.e "Could not find a valid BMP280 sensor, check wiring!"

Then for starters you'll have to do just that.

Today i checked bmp280 with Arduino uno and without the level shifter and it worked perfectly via both I2C and SPI but this module is 3.3V tolerant. How is this possible ? I have attached the image of my bmp280.

bmp280.jpg

You're running it out of spec - that module of yours does not have level shifters or 3.3V regulator on board. Some modules do, and those can be connected to a 5V circuit directly.

This way it may work, it may suddenly stop working, it may produce weird results... all in all it's simply not a good idea.

Yes I agree with you, with 5volt it may work for sometime but it may show weird results afterwards.
But if it works for I2C then why not for SPI ? I Checked the voltage levels , they are OK.
I don't think i am missing anything when i switched to SPI mode. I crossed checked the connections also.

Maybe that 5V level is part of the problem.

Most such bidirectional level shifters are fine for I2C, but too slow for SPI. There are buffer ICs (74HC series) that are much faster, and which will happily do SPI. Note that all signals in SPI are in one direction only.

yes this may be the reason. So , basically it all came down to the mosfet i am using ? can you explain me about this in much detail ? thanks for your responses btw

Typical I2C level shifters rely on a pull-up resistor to bring the line high. That gives a much longer rise time than a push/pull output as normally used for SPI, so at high speeds you don't get anything resembling a nice block wave out. Probably more like as sawtooth.
10k pull-up with a mere 50pF stray capacitance already gives 0.5 µs RC time constant, i.e. rise to 70%, which is also typically needed to read a high. Signal has to be high in less than 1/2 of the period to be sure it's read properly (the read is usually about halfway the period: a bit is set on one clock edge, read on the other). That'd limit this to ~1 MHz.
Fair chance your stray capacitance is higher, especially if you use a solderless breadboard you easily add 50-100 pF there and then, so rise times are longer and maximum frequency goes down accordingly.
Default speed for I2C is 100 kHz, so no problem there. That's comfortably slow enough.

Ok i got your point.
one more thing i want to ask is when i apply force physically the value increases and then it again comes down to the normal pressure range even when the physical external force is not removed. whereas when i checked different bmp280 the pressure doesn't come back to the normal pressure range until physical external force is removed. So is there anything i can adjust with the first bmp280 so that it doesn't comes back to normal pressure range until force is removed. The code is same i attached above.

I think that mostly shows you why it's sold as an air pressure sensor. Not as "external force" sensor.

wvmarle:
I think that mostly shows you why it's sold as an air pressure sensor. Not as "external force" sensor.

yes :smiley: