T3.1 DS1307RTC and BME280 i2c communication

Status
Not open for further replies.

pltom55

Member
Hi everyone, I've setup a DS1307 breakout board from adafruit
and a BME280 Temp+humidity+pressure breakoutboard from adafruit in i2c on a teensy3.1. I can get them to work individually but when both are plugged in, only the BME280 is communicating.

However, when i initially power the teensy, the RTC time will show only for the first cycle of the loop. Can anyone tell me where do i setup the i2c addresses for the chips, or point me in the right direction. I know the BME280 address is 0x77 and the DS1307 is 0x68. I also have pull up resistors for both ( i know i should take two out).

Thanks for the help.



Code:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "ILI9341_t3.h"
#include <TimeLib.h>
#include <DS1307RTC.h>

                  

#define SEALEVELPRESSURE_HPA (1013.25)
#define TFT_DC  9
#define TFT_CS 10
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);

int rly1 = 14;
int rly2 = 15;
int rly3 = 16;
int rly4 = 17;

Adafruit_BME280 bme; // I2C


unsigned long delayTime;

void setup() {
  pinMode(rly1, OUTPUT);    
  pinMode(rly2, OUTPUT);     
  pinMode(rly3, OUTPUT);     
  pinMode(rly4, OUTPUT);    
    Serial.begin(9600);
    Serial.println(F("BME280 test"));
                    tft.begin();
                    tft.fillScreen(ILI9341_BLACK);
                    tft.setTextColor(ILI9341_YELLOW);
                    tft.setTextSize(2);
                    tft.println("Waiting for Arduino Serial Monitor...");
    bool status;
    
    // default settings
    status = bme.begin();
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
    }
    
    Serial.println("-- Default Test --");
    delayTime = 1000;

    Serial.println();
}


void loop() { 
    TimeRead();
    printValues();
    if (bme.readTemperature() *9/5 + 32 < 83) {
      digitalWrite(rly1, HIGH);
    }else{
     digitalWrite(rly1, LOW); 
    }
    
delay(5000);
}

void TimeRead() {

  tmElements_t tm;

 if (RTC.read(tm)) {
    Serial.print("Time = ");
    Serial.print(tm.Hour);
    Serial.write(':');
    Serial.print(tm.Minute);
    Serial.write(':');
    Serial.print(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
}




void printValues() {

    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature() *9/5 + 32);
    Serial.println(" *F");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
    
                tft.setRotation(1);
                tft.fillScreen(ILI9341_BLACK);
               // tft.fillRect(0,0,320,320,ILI9341_PURPLE);
               // tft.fillRect(1,1,200,100  ,ILI9341_BLACK);
              
               
                tft.setCursor(0, 0);
                tft.setTextColor(ILI9341_YELLOW);  tft.setTextSize(3);
                tft.print("Temperature:" );
                tft.println(bme.readTemperature() *9/5 + 32);

               
                tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(3);
                
                tft.print("Humidity:");
                tft.println(bme.readHumidity());
                tft.println();
                tft.print("Pressure:");
                tft.print(bme.readPressure() / 100.0F);
 
You might want to run the Wire -> i2c_scanner example with each of the devices connected, and see if either of the devices have alternate I2C addresses that they are listening on. Some RTC devices also provide a few bytes of EEPROM memory at a different location and/or mimic other RTC's at a different address. Note, I'm at work right now, and I may be mis-remembering the name of the i2c scanner example.

If you are not using the Teensy LC, you might want to use the RTC built into the Teensy. For the Teensy 3.0/3.1/3.2, you will need to solder a 32.768 kHZ crystal to the two holes on the Teensy. For the Teensy 3.5 and 3.6, the crystal is included with the Teensy. Then you connect a coin cell battery to ground and Vbat (pin on the back row). The example File > Examples > Time > TimeTeensy3 shows how to use it. There is a thread that describes how the Teensy IDE sets the time:

Note, the Teensy RTC is not controlled for temp. variations like a DS3231 RTC is. Likewise, your DS1307 is also not controlled for temp. variations.
 
I ran the Wire > Scanner and here are the results.

I2C Scanner
Scanning...
Device found at address 0x68 (DS1307,DS3231,MPU6050,MPU9050,MPU9250,ITG3200,ITG3701,LSM9DS0,L3G4200D)
Device found at address 0x77 (BMP085,BMA180,BMP280,MS5611)
done
 
Yes, but why am I only getting data from one chip, is my order wrong or something. Am i not initializing the DS1307 correctly?
 
I fear that without having the same breakouts at hand, nobody will be able to reproduce the problem. When in your situation, my first attempt would be looking at the i2c data line with a logic analyser to see what happens on the bus. I never worked with external RTC modules, but are you sure that the RTC data which is displayed comes really from the external RTC module and not from the Teensy's internal RTC? I can't find any RTC object constructor or DS1307.begin() in your code (But since I don't know the library, I'm not sure if it's needed)?!?
 
So i figured it out.

Code:
  if (bme.readTemperature() *9/5 + 32 < 82){
    digitalWrite(rly1, HIGH);
  }else{
      digitalWrite(rly1, LOW);
    }

That piece of the code was preventing the DS1307 from working. Does anyone have a clue why?
 
Status
Not open for further replies.
Back
Top