0

enter image description here

I have connected two temp sensors and an OLED display to a NodeMCU using I2C. When the program reaches the instruction to print the measurements on the display the program crashes and NodeMCU resets itself. In the serial monitor I see "rst cause 2". I did search about this online but couldn't find a similar issue involving an OLED. I wonder what is causing this reset and how to fix it?

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Sodaq_SHT2x.h>
#include <Adafruit_SSD1306.h>

//defining OLED dimensions
#define WIDTH 128
#define HEIGHT 32
#define OLED_RESET -1

Adafruit_SSD1306 display(OLED_RESET);


Adafruit_BME280 bme1; // I2C

void setup() {
  Serial.begin(115200);

  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initiate OLED

  Serial.println(F("BME280 test"));

  //BME280 sensor 1
  if (! bme1.begin(0x77, &Wire)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }

  //  weather monitoring
//  Serial.println("-- Weather Station Scenario --");
//  Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
//  Serial.println("filter off");
  bme1.setSampling(Adafruit_BME280::MODE_FORCED,
                   Adafruit_BME280::SAMPLING_X1, // temperature
                   Adafruit_BME280::SAMPLING_X1, // pressure
                   Adafruit_BME280::SAMPLING_X1, // humidity
                   Adafruit_BME280::FILTER_OFF   );
}

void loop() {
  unsigned long newTime = millis();
  static unsigned long oldTime = 0;

  if (newTime - oldTime >= 10000) {
    oldTime = newTime;

    float temp1 = 0.0;
    float humid1 = 0.0;
    float temp2 = 0.0;
    float humid2 = 0.0;
    
    takeReadingsBME(temp1, humid1);
    printValuesBME(temp1, humid1);

    takeReadingsSHT(temp2, humid2);
    printValuesSHT(temp2, humid2);
    displayResults(temp1, temp2, humid1, humid2);
  }
}


void printValuesBME(float temp, float humid) {

  Serial.println();
  Serial.print("BME: ");
  Serial.write(9); 
  Serial.print("T = ");
  Serial.print(temp);
  Serial.write(9);
  Serial.print("H = ");
  Serial.println(humid);
}

void printValuesSHT(float temp2, float humid2) {
  Serial.print("SHT21: ");
  Serial.write(9); 
  Serial.print("T = ");
  Serial.print(temp2);
  Serial.write(9); 
  Serial.print("H = ");
  Serial.println(humid2);
}

void displayResults(float temp1, float temp2, float humid1, float humid2) {
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Sensor Data");
  display.println();
  display.print("BME ");
  display.print("T = ");
  display.print(temp1,1);
  display.print(" H = ");
  display.println(humid1,1);
  //display.setCursor(0, 0);
  //  display.print("ST: ");
  //  display.print("T = ");
  //  display.print(SHT2x.GetTemperature(), 1);
  //  display.print(" H1 = ");
  //  display.println(SHT2x.GetHumidity(), 1);
  display.display();
}

void takeReadingsBME(float &temp1, float &humid1) {

  bme1.takeForcedMeasurement();
  temp1 = bme1.readTemperature();
  humid1 = bme1.readHumidity();
}

void takeReadingsSHT(float &temp2, float &humid2) {

  temp2 = SHT2x.GetTemperature();
  humid2 = SHT2x.GetHumidity();
}
  • Sounds like power problems. – Majenko Aug 2 at 16:08
  • I am powering the components and the NodeMCU via external power supply. It should not be an issue normally – Zaffresky Aug 2 at 16:14
  • Can you provide a diagram detailing how it is all wired up? Including the power supply? – Majenko Aug 2 at 16:15
  • Reset cause 2 is a reset triggered by the reset button, JFYI (docs.espressif.com/projects/esp-idf/en/latest/esp32/…) – Sim Son Aug 2 at 17:13
  • @Majenko I have added the circuit diagram. I could not find exact power adapter component so I have used a generic 9V power supply. In reality I have a breadboard power supply that converts 9V DC to 3.3V. – Zaffresky Aug 2 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.