I have bought a BMP280 (temperature, air pressure and altitude) sensor. My question is if I can use also other digital pins than D10, D11, D12 and D13.
This is the code I use:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.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);
}
}
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)); // this should be adjusted to your local forcase
Serial.println(" m");
Serial.println();
delay(2000);
}
I have already tried to connect it to other pins, but it didn't work.
You don't say what kind of Arduino you are using so I will assume Uno or other '328.
Yes, you can use those other pins. Some bme280 sensor modules use those pins for the SPI interface. But I'm guessing your module uses the i2c interface.
No, you can't use any other pins for the sensor. Ok, yes you can, it is possible, but unless you have a very, very good reason, don't.
yes I am using the arduino Uno.
I have already a SD card module connected to the arduino (soldered) and the BMP280 is the last sensor to finish the project. So I must use other pins.
Could you please help?
Thanks
I’m jumping in here just to see the outcome. But I would think you can still use the I2C pins on the uno, as long as the sensor does not have the same address as whatever else is on that bus. Paul?
I am already using a HMC5883L magnetometer at the I2C ports. And I just began with programming, so don't really know how I could use the I2C port for these two devices at the same time.
My project must be finished next week, so the easiest solution would really help.
You sound sceptical. SPI and I2C are examples of what are called "busses". The whole reason for a bus is to use (some or all of) the same pins to connect to several different devices.
With i2c, the same 2 pins (A4&A5 on Uno) are used to connect to many devices. As already mentioned, each device has a unique address on the bus, so the Arduino can talk to it without interference from the other devices. With SPI bus, three of the Arduino pins are shared but the 4th pin must be unique to each device. The Arduino uses that fourth pin to select the device ita wants to talk to.
So the SPI bus uses more pins, but it is much faster than i2c. But often, the extra speed is not needed, so it is not worth taking up extra pins.