DHT 22 Temperature

Interface Photon with DHT 22 and OLED display.

particle logo

Interface DHT 22 Sensor and OLED display with Phtoton

DHT 22 is digital output relative temperature and humidity sensor. It's is a basic, low-cost digital temperature and humidity sensor. DHT 22's more expensive version obviously has better specifications.It's temperature measurement range is from -40oC to 80oC. In this project we will use OLED display 128x64 resolution for displaying the temperature details. To know more about OLED interface with photon click here .... To interface DHT 22 with Particle photon click here ...

Materials Required

  1. A Breadboard (http://amzn.to/2r7ysO5)
  2. Power Supply unit (http://amzn.to/2s8aWRE)
  3. Jumper Wires (http://amzn.to/2txKJuT)
  4. OLED Display(http://amzn.to/2sxy75n)
  5. DHT 22 Sensor(http://amzn.to/2FSosx2)
  6. A 10 kohm Resistor

Connection diagram


Source Code


    // This #include statement was automatically added by the Particle IDE.
    #include <Adafruit_DHT.h>

    // This #include statement was automatically added by the Particle IDE.
    #include <Adafruit_SSD1306.h>

    #define DHTPIN 5     // what pin we're connected to
    #define DHTTYPE DHT22		// DHT 22 (AM2302)

    // Uncomment this block to use hardware SPI
    // If using software SPI (the default case):
    #define OLED_MOSI   D0
    #define OLED_CLK    D1
    #define OLED_DC     D2
    #define OLED_CS     D3
    #define OLED_RESET  D4
    Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

    DHT dht(DHTPIN, DHTTYPE);
    double temperature = 0.0;


    #if (SSD1306_LCDHEIGHT != 64)
    #error("Height incorrect, please fix Adafruit_SSD1306.h!");
    #endif

    static const unsigned char PROGMEM cameo19_glcd_bmp[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xe0, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xfc, 0x00, 0x00, 
    0x00, 0x00, 0x7e, 0x3f, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xff, 0xff, 0x00, 0x00, 
    0x00, 0x79, 0xff, 0x8f, 0xff, 0xff, 0x00, 0x00, 0x01, 0xff, 0xff, 0xcf, 0xff, 0xff, 0x80, 0x00, 
    0x01, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xc0, 0x00, 0x03, 0xff, 0xff, 0xc1, 0xff, 0xff, 0xc0, 0x00, 
    0x03, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xe0, 0x00, 0x03, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xe0, 0x00, 
    0x0f, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xe0, 0x00, 0x1f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xe0, 0x00, 
    0x3f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xf0, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xf0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xf0, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xf0, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 
    0x00, 0x0f, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 
    0x00, 0x07, 0xff, 0xff, 0xf8, 0x00, 0xfc, 0x00, 0x00, 0x07, 0xff, 0xff, 0xe0, 0x01, 0xfe, 0x00, 
    0x00, 0x07, 0xff, 0xff, 0xc3, 0xf3, 0xff, 0x80, 0x00, 0x07, 0xff, 0xff, 0x8f, 0xff, 0xff, 0x80, 
    0x00, 0x03, 0xff, 0xff, 0x9f, 0xff, 0xff, 0xc0, 0x00, 0x03, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xc0, 
    0x00, 0x01, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xc0, 
    0x00, 0x00, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x7f, 0xf8, 0x3f, 0xff, 0xff, 0xe0, 
    0x00, 0x00, 0x3f, 0xf1, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x1f, 0xe3, 0xff, 0xff, 0xff, 0xf8, 
    0x00, 0x00, 0x07, 0xc7, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x03, 0xcf, 0xff, 0xff, 0xff, 0xfc, 
    0x00, 0x00, 0x00, 0xcf, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xfc, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };



    void setup()   {                
    Serial.begin(9600);
    dht.begin();
    Particle.variable("temperature", temperature);
    
    // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    display.begin(SSD1306_SWITCHCAPVCC);
    // init done
    
    display.display(); // show splashscreen
    delay(2000);
    display.clearDisplay();   // clears the screen and buffer
    }


    void loop() {
        delay(2000);
        display.clearDisplay();
        temperature = dht.getTempCelcius();
        // text display tests
        display.setTextSize(1);
        display.setTextColor(WHITE);
        display.clearDisplay();
        display.setCursor(0,3);
        display.setTextSize(1);
        display.print("Temperature");
        display.setCursor(1,30);
        display.println("");
        display.setTextSize(2);
        display.println(temperature);
        display.drawBitmap(55, 0,  cameo19_glcd_bmp, 64, 64, WHITE);
        display.setCursor(10,56);
        display.setTextSize(1);
        display.print("www.sarathiblog.com");
        delay(2000);
        display.display();
    }
                        

Video

The below video will show how the app actually works