Help with Arduino UNO dht11 thermometer

Greetings, I’m new to working with an Arduino and I’ve run into a bit of trouble that my google-fu hasn’t been able to help.

I’m working on a project that takes two seven segment displays driven by the 74LS47 chips. I also have a dht11 temperature and humidity sensor that inputs a value.

The issue comes into play when trying to run the code, it seems to only like to display the temperatures 77, 83-84, and occasionally ones above 90. It especially hates temperatures displaying 9’s in the one’s place.

I am not using the humidity sensor, and couldn’t if I wanted to because the pin that goes to it isn’t soldered to this weird mini board it came on (IDK, my uni supplied the parts). I also am certain that it isn’t an issue with my wiring because I can get the seven segment displays to count to 99 using a program that I had written before.

I think it’s software related but can’t pin down the issue. I have the values it’s supposed to be throwing onto the seven segs, print out via serial (so I can monitor them from the computer).

Anyway here is a copy of the code I’ve written for the thermometer. Oh, and just for kicks I’ve also attached the timer (I have it loop at 59 so it’s a 60 sec timer lol)

arduinoThermometer.ino (2.51 KB)

arduinoProjDualDisp.ino (1.33 KB)

#include <dht11.h>
//region

//declaring seperate port arrays
int dispR[] = {8, 9, 10, 11};
int dispL[] = {0, 1, 2, 3};
int thermoPort = 4;

//port #'s for leds
int ledRed = 7;
int ledYllw = 6;
int ledGrn = 5;

//declaring an object of dht11
dht11 thermo;

//variable to store temperature until next read
int temp;
int tempR;
int tempL;

//endregion

void setup()
{
  for (int p = 0; p < 4; p++)
  {
    //this tells the board that each of these pins will be output pins
    pinMode(dispR[p], OUTPUT);
    pinMode(dispL[p], OUTPUT);
  }

  pinMode(ledRed, OUTPUT);
  pinMode(ledYllw, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  thermo.attach(thermoPort);
  Serial.begin(9600);

}

//coverting out int, into a byte that we can use to switch the 7-segs on and off
void dispBinR(byte numToShow)
{
  for (int i = 0; i < 4; i++)
  {
    //one is on zero is off
    if (bitRead(numToShow, i) == 1)
    {
      //writes a high/low voltage to the pin at index i
      digitalWrite(dispR[i], HIGH);
    }
    else
    {
      digitalWrite(dispR[i], LOW);
    }
  }
}

void dispBinL(byte numToShow)
{
  for (int i = 0; i < 4; i++)
  {
    if (bitRead(numToShow, i) == 1)
    {
      digitalWrite(dispL[i], HIGH);
    }
    else
    {
      digitalWrite(dispL[i], LOW);
    }
  }
}

//switching the LEDs on and off based on the parameters set
void ledLight (int temp)
{
  if (temp >= 70 && temp <= 80)
  {

    digitalWrite(ledRed, LOW);
    digitalWrite(ledYllw, LOW);
    digitalWrite(ledGrn, HIGH);

  }
  else if ( (temp >= 65 && temp < 70) || (temp > 80 && temp <= 85))
  {

    digitalWrite(ledRed, LOW);
    digitalWrite(ledYllw, HIGH);
    digitalWrite(ledGrn, LOW);

  }
  else
  {

    digitalWrite(ledRed, HIGH);
    digitalWrite(ledYllw, LOW);
    digitalWrite(ledGrn, LOW);
  }
}

void loop()
{
  //getting the temp and parsing it into two individual digits
  //then throwing it into our converter for high/low set
  //int chk = thermo.read();

  //switch (chk)
  //  {
  //    case 0: Serial.println("OK"); break;
  //    case -1: Serial.println("Checksum error"); break;
  //    case -2: Serial.println("Time out error"); break;
  //    default: Serial.println("Unknown error"); break;
  //  }


  temp = thermo.fahrenheit();
  tempR = temp / 10;
  tempL = temp - (tempR * 10);


  dispBinR(tempR);
  dispBinL(tempL);

  //turn the LED's on and off
  ledLight(temp);

  Serial.println(temp);
  Serial.println(tempL);

  delay(1000);

}

Thanks for the reply, I guess I should have included that instead of attaching the file, sorry.

Did you change anything in the code by chance? I was trying to spot any differences to see if you fixed it, or just posted the code in an easier format for people to see.

I think Dave just posted it in code tags to make it easier to see. Using code tags is one of the forum rules.

I'm looking at this bit of your code:

  temp = thermo.fahrenheit();
  tempR = temp / 10;
  tempL = temp - (tempR * 10);

It looks like you're splitting the temperature into 2 digits, one for 10s and one for the 1s.
Is tempR supposed to be the 1s or the 10s?

int tempR is supposed to be the ten’s digit because the dispR is the rightmost display out of the two.

I have since modified my code to figure out what’s wrong and it seems to be coming from the loop that I use to convert tempL to BCD

Here is what I’m getting out of the serial monitor. (sorry about the mess, I didn’t spend much time formatting the serial output)

BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5
BCD for one's digit

1010temp: 
75
one's digit: 
5

here is the present version of my code

#include <dht11.h>
//region

//declaring seperate port arrays
int dispR[] = {8, 9, 10, 11};
int dispL[] = {0, 1, 2, 3};
int thermoPort = 4;

//port #'s for leds
int ledRed = 7;
int ledYllw = 6;
int ledGrn = 5;

//declaring an object of dht11
dht11 thermo;

//variable to store temperature until next read
int temp;
int tempR;
int tempL;

//endregion

void setup()
{
  for (int p = 0; p < 4; p++)
  {
    //this tells the board that each of these pins will be output pins
    pinMode(dispR[p], OUTPUT);
    pinMode(dispL[p], OUTPUT);
  }

  pinMode(ledRed, OUTPUT);
  pinMode(ledYllw, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  thermo.attach(thermoPort);
  Serial.begin(9600);

}

//coverting out int, into a byte that we can use to switch the 7-segs on and off
void dispBinR(byte numToShow)
{
  for (int i = 0; i < 4; i++)
  {
    //one is on zero is off
    if (bitRead(numToShow, i) == 1)
    {
      //writes a high/low voltage to the pin at index i
      digitalWrite(dispR[i], HIGH);
    }
    else
    {
      digitalWrite(dispR[i], LOW);
    }
  }
}

//to be removed printing out the BCD to spot errors
void dispBinL(byte numToShow)
{
  Serial.println("BCD for one's digit");
  Serial.println("");
  for (int i = 0; i < 4; i++)
  {
    if (bitRead(numToShow, i) == 1)
    {
      digitalWrite(dispL[i], HIGH);
      Serial.print(1);
    }
    else
    {
      digitalWrite(dispL[i], LOW);
      Serial.print(0);
    }
  }
}

//switching the LEDs on and off based on the parameters set
void ledLight (int temp)
{
  if (temp >= 70 && temp <= 80)
  {

    digitalWrite(ledRed, LOW);
    digitalWrite(ledYllw, LOW);
    digitalWrite(ledGrn, HIGH);

  }
  else if ( (temp >= 65 && temp < 70) || (temp > 80 && temp <= 85))
  {

    digitalWrite(ledRed, LOW);
    digitalWrite(ledYllw, HIGH);
    digitalWrite(ledGrn, LOW);

  }
  else
  {

    digitalWrite(ledRed, HIGH);
    digitalWrite(ledYllw, LOW);
    digitalWrite(ledGrn, LOW);
  }
}

void loop()
{
  //getting the temp and parsing it into two individual digits
  //then throwing it into our converter for high/low set
  //int chk = thermo.read();

  //switch (chk)
  //  {
  //    case 0: Serial.println("OK"); break;
  //    case -1: Serial.println("Checksum error"); break;
  //    case -2: Serial.println("Time out error"); break;
  //    default: Serial.println("Unknown error"); break;
  //  }


  temp = thermo.fahrenheit();
  tempR = temp / 10;

  int leftStr = temp - (tempR * 10);
  tempL = leftStr;



  dispBinR(tempR);
  dispBinL(tempL);

  //turn the LED's on and off
  ledLight(temp);

  
  Serial.println("temp: ");
  Serial.print(temp);
  Serial.println("");
  
  Serial.println("one's digit: ");
  Serial.print(tempL);
  Serial.println("");

  delay(1000);

}

Real quick update: I realized that I was printing the BCD backward, so I switched the for loop around to display it right, and still correctly index.

#include <dht11.h>
//region

//declaring seperate port arrays
int dispR[] = {8, 9, 10, 11};
int dispL[] = {0, 1, 2, 3};
int thermoPort = 4;

//port #'s for leds
int ledRed = 7;
int ledYllw = 6;
int ledGrn = 5;

//declaring an object of dht11
dht11 thermo;

//variable to store temperature until next read
int temp;
int tempR;
int tempL;

//endregion

void setup()
{
  for (int p = 0; p < 4; p++)
  {
    //this tells the board that each of these pins will be output pins
    pinMode(dispR[p], OUTPUT);
    pinMode(dispL[p], OUTPUT);
  }

  pinMode(ledRed, OUTPUT);
  pinMode(ledYllw, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  thermo.attach(thermoPort);
  Serial.begin(9600);

}

//coverting out int, into a byte that we can use to switch the 7-segs on and off
void dispBinR(byte numToShow)
{
  for (int i = 0; i < 4; i++)
  {
    //one is on zero is off
    if (bitRead(numToShow, i) == 1)
    {
      //writes a high/low voltage to the pin at index i
      digitalWrite(dispR[i], HIGH);
    }
    else
    {
      digitalWrite(dispR[i], LOW);
    }
  }
}

//to be removed printing out the BCD to spot errors
void dispBinL(byte numToShow)
{
  Serial.println("BCD for one's digit");
  Serial.println("");
  for (int i = 3; i >= 0; i--)
  {
    if (bitRead(numToShow, i) == 1)
    {
      digitalWrite(dispL[i], HIGH);
      Serial.print(1);
    }
    else
    {
      digitalWrite(dispL[i], LOW);
      Serial.print(0);
    }
  }
}

//switching the LEDs on and off based on the parameters set
void ledLight (int temp)
{
  if (temp >= 70 && temp <= 80)
  {

    digitalWrite(ledRed, LOW);
    digitalWrite(ledYllw, LOW);
    digitalWrite(ledGrn, HIGH);

  }
  else if ( (temp >= 65 && temp < 70) || (temp > 80 && temp <= 85))
  {

    digitalWrite(ledRed, LOW);
    digitalWrite(ledYllw, HIGH);
    digitalWrite(ledGrn, LOW);

  }
  else
  {

    digitalWrite(ledRed, HIGH);
    digitalWrite(ledYllw, LOW);
    digitalWrite(ledGrn, LOW);
  }
}

void loop()
{
  //getting the temp and parsing it into two individual digits
  //then throwing it into our converter for high/low set
  //int chk = thermo.read();

  temp = (int)thermo.fahrenheit();
  tempR = temp / 10;

  int leftStr = temp - (tempR * 10);
  tempL = leftStr;



  dispBinR(tempR);
  dispBinL((byte)tempL);

  //turn the LED's on and off
  ledLight(temp);

  Serial.println("");
  Serial.println("temp: ");
  Serial.print(temp);
  Serial.println("");
  
  Serial.println("one's digit: ");
  Serial.print(tempL);
  Serial.println("");

  delay(5000);

}

Here is what it’s displaying on the serial monitor now.

BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0111
temp: 
77
one's digit: 
7
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

0101
temp: 
75
one's digit: 
5
BCD for one's digit

Zakgeki:
I am not using the humidity sensor, and couldn't if I wanted to because the pin that goes to it isn't soldered to this weird mini board it came on (IDK, my uni supplied the parts).

The DHT11 is both temperature and humidity, the output signal from the DHT supplies data for both humidity and temperature, the cut lead is not used.
There is a command that will let you read the humidity, it will be in the same format as the code you use to get temperature.
The DHT11 examples should have how to get this.
Tom... :slight_smile:

Zakgeki:
tempR is supposed to be the ten's digit because the dispR is the rightmost display out of the two.

Don't the 10s normally go on the left?

:o You are correct. I forgot my left from my right.

I'll go back and change it later, but I checked the pins and the ones that correspond to the 10's and 1's digits are correct. the 10's are 8-11 and the 1's are 0-3 on the digital side of the board.

Zakgeki:
the 10's are 8-11 and the 1's are 0-3 on the digital side of the board.

Ah! I should have noticed this before: don't use pins 0 and 1 - they are internally connected to the USB/Serial interface.

You're using Serial for debugging messages and that will be interfering with digitalWrite()

Thank you so much! :smiley:

I would have never realized or known that was a problem!

(please ignore, I have some classmates who are also having similar issues but I don't know how to contact them, so I'm trying to optimize the search.)

DHT11 not working
Two Seven segment displays
Dual Seven Segment displays
DHT11 temperature and humidity sensor
Thermostat
thermometer