Hello,
I´ve done a quick project on a Teensy 4.0 with two BMP280 sensors, connected via I2C (SCL0, SDA0 and SCL1, SDA1).
Actually I did not get how to proper initialize the second bus to get valid data from the second sensor because both sensor have the same address (0x76).
ArduinoIDE (1.8.13)
Libraries: <Wire.h>, "cactus_io_BME280_I2C.h", <Servo.h>
Attached please find my code:
I´ve done a quick project on a Teensy 4.0 with two BMP280 sensors, connected via I2C (SCL0, SDA0 and SCL1, SDA1).
Actually I did not get how to proper initialize the second bus to get valid data from the second sensor because both sensor have the same address (0x76).
ArduinoIDE (1.8.13)
Libraries: <Wire.h>, "cactus_io_BME280_I2C.h", <Servo.h>
Attached please find my code:
Code:
#include <Wire.h>
#include "cactus_io_BME280_I2C.h"
#include <Servo.h>
#define RelayHPin 4 // Relay Pin D4
#define RelayTPin 5 // Relay Pin D5
#define ServoPin 9 // Servo PWM out D9
int relayH = 4; // Relay on Pin D4
int relayT = 5; // Relay on Pin D5
int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 0
// outside leads to ground and +5V
int val = 0; // variable to store the value read
Servo mainServo;
int position = 0;
int previousPosition;
// Create BME280 object
BME280_I2C bme1(0x76); // I2C using address 0x76 // connected to I2C SCL0 Pin 19, SDA0 Pin 18
void setup() {
mainServo.attach(ServoPin);
pinMode(relayH, OUTPUT); // Pin for relay module set as OUTPUT
pinMode(relayT, OUTPUT); // Pin for relay module set as OUTPUT
Serial.begin(9600);
Serial.println("Bosch BME280 Pressure - Humidity - Temp Sensor | cactus.io");
if (!bme1.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
bme1.setTempCal(-1);// Temp was reading high so subtract 1 degree
Serial.println("tHumdity\t\tTemp");
}
void loop() {
// ***************** Potentiometer reading *****************
// val = analogRead(analogPin); // read the input pin
int setpoint = map(analogRead(analogPin), 0, 1024, 0, 50); // mapping from 0-1024 to 0-50
// ****************** BMP280 sensor reading ********************
bme1.readSensor();
// ******************* Humidity and Temp values + Offset ****************
float h1Offset = bme1.getHumidity() + 0.0; // Humidity Sensor1
float t1Offset = bme1.getTemperature_C() + 0.0; // Temperature Sensor1
// ******************* Humidity scale to servo *************
// This maps humidity to degrees open for the flap + Operator setpoint
int position = map(h1Offset - setpoint, 30, 100, 60, 20); // mapping flap min - max incl. Poti offset /act 30, 100, 101, 40
if(previousPosition != position){
mainServo.write(position);
}
previousPosition = position;
// Relay ON/OFF by Servo position
if (position < 55){ // Relay OFF if Servoposition < setpoint
digitalWrite(relayH, LOW); // Relay OFF
}
else if (position > 65){ // Relay ON if Servoposition > setpoint
digitalWrite(relayH, HIGH); // Relay ON
}
// Relay ON/OFF by Temperature
if (t1Offset < 27){ // Relay ON if Temperature bigger
digitalWrite(relayT, HIGH); // Relay ON
}
else if (t1Offset > 28){ // Relay OFF if Temperature smaler
digitalWrite(relayT, LOW); // Relay OFF
}
Serial.print(F("HumSens1: ")); Serial.print(bme1.getHumidity()); Serial.print(" %\t");
Serial.print(F("TempSens1: ")); Serial.print(bme1.getTemperature_C()); Serial.print(" °C\t");
Serial.print(F("HumSens1Offset: ")); Serial.print(h1Offset); Serial.print(" %\t");
Serial.print(F("TempSens1Offset: "));Serial.print(t1Offset); Serial.print(" °C\t");
Serial.print(F("ServoPos: ")); Serial.print(position); Serial.print(" °\t");
Serial.print(F("PotiPos: ")); Serial.print(setpoint); Serial.println(" °\t");
// Add a 2 second delay.
delay(100); //just here to slow down the output.
}