Devices & Components
Arduino Nano
Power Supply Module MB V2 5v/3.3v
0.96" 128x64 OLED I2C Display
9V Battery with clip
DHT11 Temperature & Humidity Sensor (3 pins)
Mini breadboard - White
Jumper wire
Software & Tools
Arduino IDE
Project description
Code
DHT11 on OLED
cpp
Can copy and paste into Arduino IDE
1//DHT sensor 2#include "DHT.h" 3#define DHT11Pin 2 4#define DHTType DHT11 5//OLED 6#include <Wire.h> 7#include <Adafruit_GFX.h> 8#include <Adafruit_SSD1306.h> 9 10DHT HT(DHT11Pin,DHTType); 11float humi; 12float tempC; 13float tempF; 14 15//OLED define 16#define SCREEN_WIDTH 128 //OLED display width, in pixels 17#define SCREEN_HEIGHT 64 //OLED display height, in pixels 18//SSD1306 display connected to I2C (SDA, SCL pins) 19Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 20 21void setup() { 22 Serial.begin(9600); 23 //For DHT11 24 HT.begin(); 25 //Necessary code for OLED I2C 26 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 27 Serial.println(F("SSD1306 allocation failed")); 28 for(;;); 29 } 30 display.display(); //Displays the starting logo 31 delay(1000); 32 display.clearDisplay(); 33} 34 35void loop() { 36 delay(1000); 37 humi = HT.readHumidity(); 38 tempC = HT.readTemperature(); 39 tempF = HT.readTemperature(true); 40 41 display.clearDisplay(); 42 oledDisplayHeader(); 43 //configures the position of the readings. (charactersize, x axis value, y axis value, reading, symbol) 44oledDisplay(2,90,28,humi,"%"); 45 oledDisplay(3,-10,44,tempC,"C"); 46 oledDisplay(3,-10,16,tempF,"F"); 47 48 display.display(); 49 50} 51 52void oledDisplayHeader(){ 53 //configures position and qualities of headers 54 display.setTextSize(1); 55 display.setTextColor(WHITE); 56 display.setCursor(0, 0); 57 display.print("Temperature"); 58 display.setCursor(80, 0); 59 display.print("Humidity"); 60} 61void oledDisplay(int size, int x,int y, float value, String unit){ 62 int charLen=18; 63 int xo=x+charLen*3.2; 64 int xunit=x+charLen*3.6; 65 int xval = x; 66 display.setTextSize(size); 67 display.setTextColor(WHITE); 68 69 if (unit=="%"){ 70 display.setCursor(x, y); 71 display.print(value,0); 72 display.print(unit); 73 } else { 74 if (value>99){ 75 xval=x; 76 } else { 77 xval=x+charLen; 78 } 79 display.setCursor(xval, y); 80 display.print(value,0); 81 display.drawCircle(xo, y+2, 2, WHITE); // print degree symbols 82 display.setCursor(xunit, y); 83 display.print(unit); 84 } 85 86}
Downloadable files
Schematic
Schematic slightly different from pictures
schematic.png

Comments
Only logged in users can leave comments