1

I have a DHT11 that outputs temperature and humidity values to my serial monitor. I'm trying to serial the COLOUR of the light based on the temperature conditions. So far I only get 1's and 0's. How can I change these integer values to output string say RED, GREEN or BLUE?

Code

#include <dht.h>

dht DHT;
#define DHT11_PIN A5
int redPin = 10; // Red LED,  connected to digital pin 10
int grnPin = 9; // Green LED, connected to digital pin 9
int bluPin = 8;  // Blue LED, connected to digital pin 8

void setup() {
  Serial.begin(9600);
  // Sets the pins as output for RGB LED
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
}

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  int t = DHT.temperature;
  int h = DHT.humidity;
  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.print( "," );
  Serial.print("Humidity = ");
  Serial.print(h);
  Serial.print("\n");
  delay(60000);
  if((DHT.temperature < 26) && (DHT.temperature >= 23.2)) {
    // Writing the LED colour pins HIGH or LOW to set colours
    digitalWrite(redPin, HIGH); // yellow
    digitalWrite(grnPin, HIGH);
    delay(100);
    digitalWrite(bluPin, LOW);
  }
  if((DHT.temperature < 23) && (DHT.temperature > 20.2)) {
    digitalWrite(grnPin, HIGH); // green
    delay(100);
    digitalWrite(redPin, LOW);
    digitalWrite(bluPin, LOW);
  }
  if((DHT.temperature < 20) && (DHT.temperature > 17.2)) {
    digitalWrite(grnPin, HIGH); // aqua
    digitalWrite(bluPin, HIGH);
    delay(100);
    digitalWrite(redPin, LOW);
  }
  if(DHT.temperature <= 17) {
    digitalWrite(bluPin, HIGH); // blue
    delay(100);
    digitalWrite(grnPin, LOW);
    digitalWrite(redPin, LOW);
  }
  delay(1000);
  // Sensor shouldn't be read too frequently so delay of 1s
}

I will be grateful if I can get some help. Thanks!

1
  • Add Serial.print() in each block that sets the pins to new values. Commented Jan 10, 2017 at 9:31

2 Answers 2

2

So far I only get 1's and 0's

Before moving into the LED parts, you have to make sure that your system can correctly read the DHT sensor. There are debugging example from arduino-info.wikispaces.com

How can I change these integer values to output string say RED, GREEN or BLUE?

You can do it by simple if-else statement. This would be better than your original code, because there are multiple if's without any else which is (although not so significant) less optimal.

So, if put together, you can try:

void loop()
{
  int t,h,chk = DHT.read11(DHT11_PIN);

  switch (chk)
  {
    case DHTLIB_OK:  
                Serial.print("OK,\t"); 
                break;
    case DHTLIB_ERROR_CHECKSUM: 
                Serial.print("Checksum error,\t"); 
                break;
    case DHTLIB_ERROR_TIMEOUT: 
                Serial.print("Time out error,\t"); 
                break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default: 
                Serial.print("Unknown error,\t"); 
                break;
  }
  //moving on into next parts only if you get "OK" from serial reply, which means you successfully read the DHT11
  t = DHT.temperature;
  h = DHT.humidity;
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  //----end of sensor reading and debugging

  if (t<=17)
  { //blue
   digitalWrite(bluPin, HIGH);   
   digitalWrite(grnPin, LOW);  
   digitalWrite(redPin, LOW);     
   delay(100);
  } else
      if (t<20)
      {
       digitalWrite(grnPin, HIGH);  // aqua  
       digitalWrite(bluPin, HIGH);  
       digitalWrite(redPin, LOW);  
       delay(100); 
      } else
      if (t<23)
      {
        //....  
      }
      //....
      //and goes on

 delay(1000);     
}

And yes, as @Matt suggest, it would be better if you use PWM to control the RGB led which will give you (mathematically) 255*255*255 colour possibilities instead of 8 combinations from digitalWrite(). For example:

//....
//....
  if (t<20)
  { //aqua
   new_t = map(t,DHT11_min_temperature_reading,DHT11_max_temperature_reading,0,255);
   //you can do self research about DHT11 maximum and minimum temperature reading
   //parameters 0 and 255 is the minimum value of PWM(0) and the maximum value(255)
   AnalogWrite(bluPWMPin, new_t);   
   AnalogWrite(grnPWMPin, 255-new_t);  
   AnalogWrite(redPWMPin, 0);     
   delay(100);
  }
//....
//....

Hope it helps :D

0
0

You need to use analogWrite(). You use this with a digital pin and it does Pulse Width Modulation (PWM) which will give you are larger range of colours. Have a look at the Reference page for the function.

You might even be able to use the map function (something like that) to 'map' the temperature to the colour values without all the if statements.

3
  • 1
    The PWM pins have a ~ next to them on the PCB.
    – Gerben
    Commented Jan 10, 2017 at 11:07
  • Thanks for your response. Do you have an idea of how I can implement the map function?
    – Ekom
    Commented Jan 10, 2017 at 12:54
  • Erm... not a clue, I've never used it, but I have seen it on here. Commented Jan 10, 2017 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.