Incorrect I2C sensor reading after low-power mode wakeup

Good day!

I am new to Arduino and inexperienced with I2C and was hoping I could get help with a software-related issue I am having. The project I am working on has the following setup:

Basically, a Seeeduino Xiao MCU communicates via I2C with three sensors (temperature, light and accelerometer) and stores the readings on the SD card before returning to sleep mode.

I have managed to implement this but I am having an issue with the MAX4009 ambient light sensor. Without the sleep mode implemented I can communicate with all three sensors perfectly but once sleep mode is used the other two sensors take a reading but not the light sensor.

The output :
|accel x y z | Temperature | Ambient light
| X:-356 | Y:-355 | Z:-355 | 22.50C | 376012.81lux |
| X:-138 | Y:902 | Z:-145 | 22.99
C | 376012.81lux |
| X:-150 | Y:898 | Z:-141 | 23.23C | 376012.81lux |
| X:-159 | Y:899 | Z:-142 | 23.95
C | 376012.81lux |

This is the code:

#include <RTC_SAMD21.h.>
#include <DateTime.h>
#include <EnergySaving.h>
#include <SPI.h>
#include <SD.h>
#include<Wire.h>

#include <DFRobot_LIS2DW12.h>
DFRobot_LIS2DW12_I2C acce(&Wire, 0x19);
#include <Adafruit_TMP006.h>


#define Address_GY49 0x4A // Ambient Sensor I2C Address is 0x4A(74)
#define PIN_LED 13

Adafruit_TMP006 tmp006; // TMP006 Temperature sensor
EnergySaving myPowSave;
RTC_SAMD21 myRtc;



const int chipSelect = 3;

void setup()
{

  SD.begin(chipSelect);
  myRtc.begin();

  \\RTC Setup for Stand - by mode
  myRtc.adjust(DateTime(2030, 4, 1, 8, 30, 0) );
  myRtc.attachInterrupt(dummyfunc);
  myPowSave.begin(WAKE_RTC_ALARM);

  myRtc.disableAlarm();
  DateTime timeNow = myRtc.now();
  const uint16_t uiAlmNextSec = 1000;
  DateTime timeAlarm = DateTime(timeNow.year(), timeNow.month(), timeNow.day(), timeNow.hour(), timeNow.minute(), timeNow.second() + uiAlmNextSec);
  myRtc.setAlarm(timeAlarm);
  myRtc.enableAlarm(myRtc.MATCH_SS);


}

void loop()
{
  delay(300); \\ No idea what im doing with these delays. Where should they be ?
  unsigned int data[2];
  Wire.begin();
  Wire.beginTransmission(Address_GY49);
  Wire.write(0x02); // Select configuration register
  Wire.write(0x42); // Continuous mode, Integration time = 200 ms
  Wire.endTransmission();
  delay(300);
  Wire.beginTransmission(Address_GY49);
  Wire.write(0x03); // Select data register
  Wire.endTransmission();
  delay(300);
  Wire.requestFrom(Address_GY49, 2); // Request 2 bytes of data
  // Read 2 bytes of data
  if (Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }
  // Convert the data to lux
  int exponent = (data[0] & 0xF0) >> 4;
  int mantissa = ((data[0] & 0x0F) << 4) | (data[1] & 0x0F);
  float luminance = pow(2, exponent) * mantissa * 0.045;
  delay(300);



  //Accelerometer Code (working)
  String output = "";
  acce.continRefresh(true);
  acce.setDataRate(DFRobot_LIS2DW12::eRate_50hz);
  acce.setRange(DFRobot_LIS2DW12::e2_g);
  acce.setFilterPath(DFRobot_LIS2DW12::eLPF);
  acce.setFilterBandwidth(DFRobot_LIS2DW12::eRateDiv_4);
  acce.setPowerMode(DFRobot_LIS2DW12::eContLowPwrLowNoise2_14bit);
  delay(200);

  output = "| X:" + (String)acce.readAccX() + " | Y:" + (String)acce.readAccY() + " | Z:" + (String)acce.readAccZ() + " | ";
  //gy-49 ambient light module (Problem)



  //Temperature sensor (working)

  float objt = tmp006.readObjTempC();
  float diet = tmp006.readDieTempC();

  String output1 = (String)objt + "*C | ";

  String output2 = (String)luminance + "lux |";

  output += output1 + output2;

  File dataFile = SD.open("datalog6.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(output);
    dataFile.close();
  }
  myPowSave.standby();

  //proceed after wakeup


}

Any suggestions will be appreciated, thank you :slight_smile:

Nice schematic but I did not see any of the required pull up resistors on the I2C lines. They should be in the 3.1K range.

Hey Gil, these sensors are all on breakout boards which include pull up resistors:

The light sensor schematic:

Implementation:

The schematics and links help a lot. Your pull ups are about 3.3K when all are connected, right where you normally want to be. The hardware appears to be ok. You close a file before going to sleep do you open it later? I have not used the part so the best for is a guest. Can you just run the light sensor and validate it works after wake up? Is there something that will drop the 3V3 Voltage? It appears to me it is getting put in the wrong mode or not completely set up when coming out of sleep.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.