I have a UNO, a DHT22, a BMP280 and a SD module. So I want to get temperature, humidity and pressure and then write them into SD. I connect SD and BMP280 by SPI. But it does not work. SD works well - all numbers are written. And BMP gives wrong values (pressure is negative, for example). Everywhere it is written that it is necessary to switch CS pins: the first device, the second one, the first one, ... . But there is nowhere explanation how do I should do it.
Help me with it, please.
My code:
/* SD module:
CS to pin 10
MOSI to pin 11
SCK to pin 13
MISO to pin 12
DHT22:
DATA to pin 2
BMP280:
SCL to pin 13
SDA to pin 11
SDO to pin 12
CS to pin 9
*/
#include <SimpleDHT.h>
#include <SD.h>
#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 9
//Connection BMP280 by SPI interface
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
File myFile; //Create new file
int pinDHT22 = 2; //DHT pin
SimpleDHT22 dht22; //22 version
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
Serial.println("BMP280 test");
if (!bmp.begin(0x76)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
float temperature = 0;
float humidity = 0;
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
myFile = SD.open("example.txt", FILE_WRITE);
for (int i = 0; i < 50; i++) {
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
return;
}
float p = bmp.readPressure(); //read pressure from BMP280
myFile.print(temperature);
myFile.print('-');
myFile.print(humidity);
myFile.print('-');
myFile.println(p);
Serial.print(temperature);
Serial.print('-');
Serial.print(humidity);
Serial.print('-');
Serial.println(p);
Serial.println(i);
delay(2000);
}
myFile.close();
Serial.print("done");
}
void loop() {
}