Weird DHT-11 and BN-220 Interaction?

I have a project containing a Nano, DHT-11 temperature & humidity module, a BN-220 GPS module and a SSD1306/OLED display. When code for both of those sensors is incorporated into the sketch, strange stuff happens and happens intermittently. First is that the GPS sentences are sometimes garbled. Second is a strange seemingly random group of pixels shows up on the OLED display. Stranger still, the GPS sentence problem occurs even if the DTH-11 read code is present in the sketch but not executed. The offending code is of the form: float h = dht.readHumidity(); If that code is commented out, everything works. If the code is left in, the GPS is messed up whether or not the code is executed. If the code is executed, both the GPS and the display are affected as described above. And, also as mentioned, the effect is intermittent i.e. sometimes everything works for a while, then fails.

Not sure what more to add. The GPS is connected to the hardware RX pin and the DHT-11 is assigned to one of the DIs. The above effects occur even if the DHT-11 is detached from the circuit board. The DHT include is #include "DHT.h". The OLED is #include <Adafruit_SSD1306.h>. There is no include for the GPS.

Can anyone please suggest what might be going on here. Thanks.

I suspect the problem lies with the parts you didn't post. Such as your code.

#include <Adafruit_SSD1306.h> // OLED display
#include <Fonts/FreeMono9pt7b.h>
#include <Fonts/FreeMono12pt7b.h>
#include <Fonts/FreeMono24pt7b.h>
#include "DHT.h" // Humid & Temp sensor

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

float knots;
String year(1);
String month(1);
String day(1);
byte hours;
byte minutes;
byte seconds;

String latdeg; //Latitude degrees
String latmins; //Minutes
String longdeg; //Longitude degrees
String longmins;

double Kmsecscumu = 0; //Cumulative Km/hr readings (accumulated once per GPS transmitt (once per second))

String inputString = ""; //Where characters from GPS are stored
int switchdisp = 0;

#define DHTPIN 9     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11   // DHT 11 type of temperature & humidity sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

void setup() {

  Serial.begin(9600);
  pinMode(4, INPUT_PULLUP); //Display data button select connected here
  dht.begin(); // Start the DHT sensor

  // Generate the high voltage from the 3.3v line internally
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with voltage setting & the I2C addr of the OLED display
  display.setTextColor(WHITE, BLACK);
}

void loop() {

  while (Serial.available()) { //Read and store characters as they arrive from the GPS
    char inChar = (char)Serial.read();
    inputString += inChar;

    if (inChar == '\n') { //End of sentenance charater
      //Serial.println(inputString);

      if ((inputString.substring(0, 6) == "$GNRMC")) { //The sentence with the required data
        //Serial.print(inputString);
        knots = inputString.substring(46, 51).toInt(); //Speed begins 46 characters from start of string and extends to 51-1 (5 chars total)

        hours = inputString.substring(7, 9).toInt(); //Time stamp begins 7 characters from start of string and extends to 13-1 (6 chars total)
        minutes = inputString.substring(9, 11).toInt(); //Minutes
        seconds = inputString.substring(11, 13).toInt(); //Seconds

        day = inputString.substring(53, 55); //Day of month
        month = inputString.substring(55, 57); //Month
        year = inputString.substring(57, 59); //Year

        latdeg = inputString.substring(19, 21);
        latmins = inputString.substring(21, 27);

        longdeg = inputString.substring(32, 35);
        longmins = inputString.substring(35, 41);

        Kmsecscumu = Kmsecscumu + (knots * 1.852); //Cumulative Kms-seconds

        // Button read code placed here so the pin is checked, & switchdisp incremented,only once per second giving time to release the button
        // before switchdisp is incremented additional time(s).  Makes use of one second delay in GPS updates w/o desynchonizing message timing.

        if (digitalRead(4) == LOW) { //Switch to new display value? = 0 for speed, = 1 for time, = 2 for lat & long, = 3 for temperature & humidity, = 4 for odometer
          switchdisp++;
          if (switchdisp > 4) { //Roll back to 0
            switchdisp = 0;
          }
        }
        display.clearDisplay();
        if (switchdisp == 0) { //Display speed?
          if (knots * 1.852 <= 9) {
            display.setCursor(40, 60);
          }
          else {
            display.setCursor(20, 60);
          }
          display.setFont(&FreeMono24pt7b);
          display.setTextSize(2);
          display.print(int(knots * 1.852)); //Display in Kmph
        }
        if (switchdisp == 1) { //Display time?
          display.setFont(&FreeMono12pt7b);
          display.setTextSize(1);
          display.setCursor(10, 18);
          /*if (hours <= 7) { //MST day will be 1 less than UT day if UT hours <= 7
            day =
            }
            if (day == 0) { //Takes care of 1st of month
            day++;
            }*/
          display.print(year + ("/") + month + ("/") + day);

          display.setTextSize(2);
          if (hours >= 8) { //Convert time from UT 24 hr clock to MST 24 hr clock
            hours = hours - 7;
          }
          if (hours <= 7) {
            hours = hours + 17;
          }
          if (hours > 12) { //Convert time from MST 24 hr clock to MST 12 hr clock
            hours = hours - 12;
          }
          if (hours == 0) {
            hours = 1;
          }
          if (hours <= 9) {
            display.setCursor(0, 60);
            display.print("0");
            display.setCursor(28, 60);
          }
          else {
            display.setCursor(0, 60);
          }
          display.print(hours);

          display.setCursor(50, 55); //Display blinking ":"
          if (seconds % 2 > 0) {
            display.print(" ");
          }
          else {
            display.print(":");
          }
          if (minutes <= 9) { //Display a leading "0"
            display.setCursor(70, 60);
            display.print("0");
            display.setCursor(95, 60);
          }
          else {
            display.setCursor(75, 60);
          }
          display.print(minutes);
        }

        if (switchdisp == 2) { //Lat & Long
          display.setFont(&FreeMono9pt7b);
          display.setTextSize(1);
          display.setCursor(46, 10);
          display.print("LAT");
          display.setCursor(15, 25);
          display.print(latdeg);
          display.setCursor(60, 25);
          display.print(latmins);
          display.setCursor(40, 43);
          display.print("LONG");
          display.setCursor(4, 60);
          display.print(longdeg);
          display.setCursor(60, 60);
          display.print(longmins);

        }

        if (switchdisp == 3) {  //DHT readings
          // Reading temperature or humidity takes about 250 milliseconds!
          // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
          float h = 0; //dht.readHumidity();

          // Read temperature as Celsius (the default)
          float t = 0; //dht.readTemperature();

          h = h * 0.95; // humidity calibrate
          t = t * 0.96; // & deg C

          display.setFont(&FreeMono9pt7b);
          display.setTextSize(1);
          display.setCursor(4, 20);
          display.print("Temp");
          display.setCursor(74, 20);
          display.print("Humid");
          display.setFont(&FreeMono24pt7b);
          display.setTextSize(1);
          display.setCursor (0, 60);
          display.print(int(t)); //Back to integer for display
          display.setCursor(72, 60);
          display.print(int(h));
        }

        if (switchdisp == 4) { //Odometer
          display.setFont(&FreeMono9pt7b);
          display.setTextSize(1);
          display.setCursor(20, 20);
          display.print("Odometer");
          display.setTextSize(2);
          display.setCursor(20, 60);
          display.print((float(Kmsecscumu) / 3600), 1); //Cumulative Km/hr/seconds converted to Kms)
        }

        display.display(); //Show it all on display
      }
      inputString = "";
    }
  }
}

Nothing obviously wrong, other than that you have a very odd program layout (whether and when the DHT sensor si read depends on what characters the GPS arrived), and the use of the String class which is generally a bad idea when using memory starved MCUs like Arduinos.

Yeah, the DHT was added as an after thought and it was just convenient to add it to an existing part of the GPS code. Since the GPS characters arrival is pretty much consistent and reliable, the DHT works OK (apart from the subject weird behaviour).

As for memory starved, I would think that would only be an issue if memory size is insufficient for the task at hand. As it turns out however, the real memory hogs in the sketch are the OLED fonts.

Anyhow, not sure what else to do at this point except maybe just delete the DHT stuff or, maybe try a Uno or some other MCU.

nosmoke:
As for memory starved, I would think that would only be an issue if memory size is insufficient for the task at hand.

Which over time can become an issue when using the String class, unless you are VERY careful with it, and understand deeply how it works (especially how it creates copies of itself time and again as it needs to allocate more memory every time you add a character to a String). Then you may be able to get away with it.

Other than that, use C-strings. Safe, reliable, and far less of a drain on your device's memory.

Interesting. I have read quite a bit about Strings but not seen that mentioned (seems like something one should know about alright). Are there any good good sources for a "deep" understanding of it?

Related to that, does the OS ever spit out run time errors (on the serial monitor for instance) such as out of RAM, stack overflow, array out of bounds etc??

nosmoke:
Related to that, does the OS ever spit out run time errors

What OS?
There are articles about the evils of the String class. I don't have links at hand - but to understand this you also have to understand how C++ manages memory. Pointers, references, that kind of fun.

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