Hello together,
I am currently building a battery powered microcontroller to record room data (temperature, air pressure, humidity, CO2, VOC, light intensity). For this I use as a base an Arduino Nicla Sens Me which I have additionally equipped with a RTC (RTC DS1307), a Microcard SD Reader (DEBO MicroSD2), a light sensor (DEBO Light Sens4) and a battery controller. The whole setup is powered by a Lithium Polymer battery with 3,7 V. Individually I can read out the sensors without any problems and read out the RTC, so I don't assume a hardware failure. However, if I try to read out all sensor values via a program and write them to the SD card, the light sensor and the BME of the Nicla in particular give me a headache. If I try to start the Wire library, I get a hard fault of the Arduino with the error code 0x80FF013D. If I start only the wire library for the I²C connection for the light sensor or the BSEC library for the BME sensor, the Arduino Nicla works fine. The program code is attached
Does anyone vllt have an idea what the problem could be? Thanks a lot!
#include "Nicla_System.h"
#include "Arduino_BHY2.h"
#include "ArduinoBLE.h"
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
#include <Wire.h>
#include <BH1750_WE.h>
#define BH1750_ADDRESS 0x23
RTC_DS1307 rtc;
BH1750_WE myBH1750 = BH1750_WE(BH1750_ADDRESS);
#define Seconds() (millis()/1000)
File myFile;
Sensor pressure(SENSOR_ID_BARO);
Sensor temperature(SENSOR_ID_TEMP);
Sensor humidity(SENSOR_ID_HUM);
Sensor gas(SENSOR_ID_GAS);
SensorBSEC bsec(uint8_t(171));
void setup() {
nicla::begin();
nicla::leds.begin();
BHY2.begin();
bsec.begin();
pressure.begin();
temperature.begin();
humidity.begin();
gas.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
//Wire.begin();
//myBH1750.init();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(6)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(String("time") + ";" + String("pressure") + ";" + String("temperature") + ";" + String("humidity") + ";" + String("gas") + ";" + String("CO2") + ";" + String("VOC"));
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
static auto lastCheck = millis();
BHY2.update();
DateTime now = rtc.now();
// Check sensor values every second
if (millis() - lastCheck >= 1000) {
lastCheck = millis();
unsigned co2 = bsec.co2_eq();
float b_voc_eq = bsec.b_voc_eq();
SD.begin(6);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(String(now.year()) + "/" + String(now.month()) + "/" + String(now.day()) + " " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + ";" + String(Seconds()) + ";" + pressure.value() + ";" + temperature.value() + ";" + humidity.value() + ";" + gas.value() + ";" + String(co2) + ";" + String(b_voc_eq));
delay(100);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
nicla::leds.setColor(green);
delay(1000);
nicla::leds.setColor(off);
delay(1000);
}
}