Components and supplies
SSD1306 OLED 128X64 0.96 inch - I2C
DHT11 Temperature & Humidity Sensor (3 pins)
Jumper wires (generic)
Solderless Breadboard Half Size
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
weather station code
c_cpp
1#include "DHT.h" 2#define DHT11Pin 2 3#define DHTType DHT11 4//OLED 5#include <Wire.h> 6#include <Adafruit_GFX.h> 7#include <Adafruit_SSD1306.h> 8 9DHT HT(DHT11Pin,DHTType); 10float humi; 11float tempC; 12float tempF; 13 14//OLED define 15#define SCREEN_WIDTH 128 // OLED display width, in pixels 16#define SCREEN_HEIGHT 64 // OLED display height, in pixels 17// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 18Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 19 20void setup() { 21 Serial.begin(9600); 22 //For DHT11 23 HT.begin(); 24 //For OLED I2C 25 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 26 Serial.println(F("SSD1306 allocation failed")); 27 for(;;); 28 } 29 display.display(); //Display logo 30 delay(1000); 31 display.clearDisplay(); 32} 33 34void loop() { 35 delay(1000); 36 humi = HT.readHumidity(); 37 tempC = HT.readTemperature(); 38 tempF = HT.readTemperature(true); 39 40 Serial.print("Humidity:"); 41 Serial.print(humi,0); 42 Serial.print("%"); 43 Serial.print(" Temperature:"); 44 Serial.print(tempC,1); 45 Serial.print("C ~ "); 46 Serial.print(tempF,1); 47 Serial.println("F"); 48 49 display.clearDisplay(); 50 oledDisplayHeader(); 51 52 53 oledDisplay(3,5,28,humi,"%"); 54 oledDisplay(2,70,16,tempC,"C"); 55 oledDisplay(2,70,44,tempF,"F"); 56 57 display.display(); 58 59} 60void oledDisplayHeader(){ 61 display.setTextSize(1); 62 display.setTextColor(WHITE); 63 display.setCursor(0, 0); 64 display.print("Humidity"); 65 display.setCursor(60, 0); 66 display.print("Temperature"); 67} 68void oledDisplay(int size, int x,int y, float value, String unit){ 69 int charLen=12; 70 int xo=x+charLen*3.2; 71 int xunit=x+charLen*3.6; 72 int xval = x; 73 display.setTextSize(size); 74 display.setTextColor(WHITE); 75 76 if (unit=="%"){ 77 display.setCursor(x, y); 78 display.print(value,0); 79 display.print(unit); 80 } else { 81 if (value>99){ 82 xval=x; 83 } else { 84 xval=x+charLen; 85 } 86 display.setCursor(xval, y); 87 display.print(value,0); 88 display.drawCircle(xo, y+2, 2, WHITE); // print degree symbols ( ) 89 display.setCursor(xunit, y); 90 display.print(unit); 91 } 92 93} 94
weather station code
c_cpp
1#include "DHT.h" 2#define DHT11Pin 2 3#define DHTType DHT11 4//OLED 5#include <Wire.h> 6#include <Adafruit_GFX.h> 7#include <Adafruit_SSD1306.h> 8 9DHT HT(DHT11Pin,DHTType); 10float humi; 11float tempC; 12float tempF; 13 14//OLED define 15#define SCREEN_WIDTH 128 // OLED display width, in pixels 16#define SCREEN_HEIGHT 64 // OLED display height, in pixels 17// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 18Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 19 20void setup() { 21 Serial.begin(9600); 22 //For DHT11 23 HT.begin(); 24 //For OLED I2C 25 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 26 Serial.println(F("SSD1306 allocation failed")); 27 for(;;); 28 } 29 display.display(); //Display logo 30 delay(1000); 31 display.clearDisplay(); 32} 33 34void loop() { 35 delay(1000); 36 humi = HT.readHumidity(); 37 tempC = HT.readTemperature(); 38 tempF = HT.readTemperature(true); 39 40 Serial.print("Humidity:"); 41 Serial.print(humi,0); 42 Serial.print("%"); 43 Serial.print(" Temperature:"); 44 Serial.print(tempC,1); 45 Serial.print("C ~ "); 46 Serial.print(tempF,1); 47 Serial.println("F"); 48 49 display.clearDisplay(); 50 oledDisplayHeader(); 51 52 53 oledDisplay(3,5,28,humi,"%"); 54 oledDisplay(2,70,16,tempC,"C"); 55 oledDisplay(2,70,44,tempF,"F"); 56 57 display.display(); 58 59} 60void oledDisplayHeader(){ 61 display.setTextSize(1); 62 display.setTextColor(WHITE); 63 display.setCursor(0, 0); 64 display.print("Humidity"); 65 display.setCursor(60, 0); 66 display.print("Temperature"); 67} 68void oledDisplay(int size, int x,int y, float value, String unit){ 69 int charLen=12; 70 int xo=x+charLen*3.2; 71 int xunit=x+charLen*3.6; 72 int xval = x; 73 display.setTextSize(size); 74 display.setTextColor(WHITE); 75 76 if (unit=="%"){ 77 display.setCursor(x, y); 78 display.print(value,0); 79 display.print(unit); 80 } else { 81 if (value>99){ 82 xval=x; 83 } else { 84 xval=x+charLen; 85 } 86 display.setCursor(xval, y); 87 display.print(value,0); 88 display.drawCircle(xo, y+2, 2, WHITE); // print degree symbols ( ) 89 display.setCursor(xunit, y); 90 display.print(unit); 91 } 92 93} 94
Downloadable files
Weather station schematic
Weather station schematic
Comments
Only logged in users can leave comments