Components and supplies
1K resistor
Arduino Nano
push buttons
2N3904 NPN transistor
LCD 16x2
Jumper wires (generic)
Box for Arduino
Apps and platforms
KiCad
Arduino IDE
Project description
Code
Arduino Nano weather station with BME280 & LCD 16x2
cpp
1//WEATHER STATION WITH ARDUINO NANO AND BME280 V2.0 2//IN THIS VERSION DELAY IS ACOMPLISHED WITH MILLIS FUNCTION AND THERE IS PUSHBUTTON THAT CONTROLS THE DISPLAY MODE 3 4//THERE IS A SECOND PUSHBUTTON THAT CONTROLS LCD BACKLIGHT. EVERYTIME IS PRESSED BACKLIGHT IS ON FOR 10'' 5//BUTTONS ARE CONTROLLED WITH THE EZBUTTON LIBRARY 6 7#include <ezButton.h> 8#include <LiquidCrystal.h> 9#include <Wire.h> 10#include <Adafruit_Sensor.h> 11#include <Adafruit_BME280.h> 12 13 14float temp; //VARIABLE FOR MEASURING TEMPERATURE 15float humid; //VARIABLE FOR MEASURING HUMIDITY 16float pressure; //VARIABLE FOR MEASURING PRESSURE 17int mode = 0; //VARIABLE FOR DISPLAY MODE. VALUE DEPENDS FROM PUSHBUTTON PRESSINGS. MODE=0 SEQUENSE -- MODE=1 TEMPERATURE -- MODE=2 HUMIDITY -- MODE=3 PRESSURE 18int whatToPrint = 0; //VARIABLE FOR CHECKING WHAT TO PRINT IN SEQUENCE MODE 19 20 21ezButton button1(5); // create ezButton object that attach to pin 5; CONNECT PUSHBUTTON FOR CONTROL LCD BACKLIGHT 22ezButton button2(7); // create ezButton object that attach to pin 7; CONNECT PUSHBUTTON FOR DISPLAY MODE 23 24int BKL_out = 4; //OUTPUT FOR CONTROLLING LCD BACKLIGHT THROUGH NPN TRANSISTOR 25int state = 0; //VARIABLE FOR BACKLIGHT 26 27unsigned long prevTime_T1 = 0; 28unsigned long prevTime_T2 = 0; 29unsigned long prevTime_T3 = 0; 30 31 32long interval_T1 = 3000; //DISPLAY TIME 3'' 33long interval_T2 = 5000; //SEND DATA VIA SERIAL EVERY 5'' 34long interval_T3 = 10000; //LCD BACKLIGHT ON TIME 10'' 35 36const int RS=2, E=3, D4=8, D5=9, D6=10, D7=11; // CONNECTION OF LCD TO ARDUINO /ΣΥΝΔΕΩ ΤΑ PIN ΤΗΣ LCD ΟΘΟΝΗΣ ΣΤΑ PIN TOY ARDUINO 37 38LiquidCrystal lcd(RS,E,D4,D5,D6,D7); //LCD INITIALIZATION / ΑΡΧΙΚΟΠΟΙΗΣΗ ΟΘΟΝΗΣ 39 40Adafruit_BME280 bme; // I2C 41 //Adafruit_BMP280 bme(BMP_CS); // hardware SPI 42 //Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK); 43 44void setup() { 45 46 pinMode (BKL_out,OUTPUT); 47 lcd.begin(16,2); //LCD 16 COLUMΝS 2 ROWS / ΑΡΧΙΚΟΠΟΙΣΗ ΟΘΟΝΗΣ ΣΤΗΛΕΣ=16 ΓΡΑΜΜΕΣ=2 48 Serial.begin(9600); //BEGIN SERIAL COMMUNICATION / ΕΝΑΡΞΗ ΣΕΙΡΙΑΚΗΣ ΕΠΙΚΟΙΝΩΝΙΑΣ 49 Serial.println(F("BMP280 test")); 50 51 if (!bme.begin()) { 52 Serial.println("Could not find a valid BMP280 sensor, check wiring!"); 53 while (1); 54 } 55 button1.setDebounceTime(50); // set debounce time to 50 milliseconds 56 button2.setDebounceTime(50); // set debounce time to 50 milliseconds 57 58 59 lcd.clear(); 60 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 61 lcd.print("WEATHER STATION"); //PRINT WELCOME MESSAGE 62 lcd.setCursor(1,3); 63 lcd.print(" Version:2.0"); 64 delay(5000); 65 66 67} 68 69void loop() { 70 71 button1.loop(); // MUST call the loop() function first 72 button2.loop(); // MUST call the loop() function first 73 74 unsigned long currentTime = millis(); 75 76 77 temp = bme.readTemperature(); //READ AND STORE TEMPERATURE 78 delay(10); 79 humid = bme.readHumidity(); //READ AND STORE HUMIDITY 80 delay(10); 81 pressure = bme.readPressure()/100; //DIVIDE WITH 100 TO GET hPa 82 delay(10); 83 84 if(currentTime - prevTime_T2 > interval_T2){ 85 String message = (String) "Humidity: "+humid+"% Temperature: " +temp+"°C Pressure: " +pressure+"hPa"; 86 Serial.println(message); 87 prevTime_T2 = currentTime; 88 } 89 90 91 if(button1.isPressed()){ //CHECK IF PUSHBUTTON FOR BACKLIGHT IS PRESSED 92 digitalWrite (BKL_out, HIGH); //BACKLIGHT ON 93 state=1; //VARIABLE FOR BACKLIGHT SET TO 1 94 prevTime_T3=currentTime; //WRITE TO VARIABLE WHAT TIME THE BACKLIGHT PUSHBUTTON WAS PRESSED 95 Serial.println("state=1"); 96 Serial.println(prevTime_T3); 97 } 98 99 if(state==1){ //IF BACKLIGHT IS ON 100 if(currentTime - prevTime_T3 > interval_T3){ //AND IF 10'' HAS PASSED 101 digitalWrite (BKL_out, LOW); //TURN OFF BACKLIGHT 102 state=0; //VARIABLE FOR BACKLIGHT SET TO 0 103 Serial.println("turn off BACKLIGHT"); 104 prevTime_T3 = currentTime; 105 } 106 } 107 108 if(button2.isPressed()) //IF BUTTON FOR DISPLAY MODE IS PRESSED 109 mode++; //INCREASE VARIABLE FOR DISPLAY MODE 110 if (mode==4){ //CHECK IF VARIABLE IS 4 111 mode = 0; //SET IT TO 0 112 } 113 114 115 if(mode == 0){ //IF VARIABLE IS 0 116 if(currentTime - prevTime_T1 > interval_T1){ //AND IF 3'' HAS PASSED 117 whatToPrint++; //INCREASE THE VARIABLE WHATTOPRINT 118 if(whatToPrint == 3){ 119 whatToPrint = 0; 120 } 121 prevTime_T1 = currentTime; 122 } 123 124//PRINT TEMPERATURE 125 if(whatToPrint == 0){ 126 127 lcd.clear(); 128 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 129 lcd.print("Temperature"); //PRINT TEXT 130 //delay(20); 131 lcd.setCursor(0,1); 132 lcd.print("T="); 133 lcd.setCursor(2,1); 134 lcd.print(temp); 135 lcd.setCursor(8,1); 136 lcd.print("C"); 137 } 138 139//PRINT HUMIDITY 140 if(whatToPrint == 1){ 141 142 lcd.clear(); 143 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 144 lcd.print("Humidity"); //PRINT TEXT 145 //delay(20); 146 lcd.setCursor(0,1); 147 lcd.print("H="); 148 lcd.setCursor(2,1); 149 lcd.print(humid); 150 lcd.setCursor(8,1); 151 lcd.print("%"); 152 } 153 154//PRINT PRESSURE 155 if(whatToPrint ==2){ 156 157 lcd.clear(); 158 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 159 lcd.print("Barometric pres"); //PRINT TEXT 160 //delay(20); 161 lcd.setCursor(0,1); 162 lcd.print("P="); 163 lcd.setCursor(2,1); 164 lcd.print(pressure); 165 lcd.setCursor(10,1); 166 lcd.print("hPa"); 167 } 168 169} 170 171 if(mode == 1){ 172 lcd.clear(); 173 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 174 lcd.print("Temperature"); //PRINT TEXT 175 //delay(20); 176 lcd.setCursor(0,1); 177 lcd.print("T="); 178 lcd.setCursor(2,1); 179 lcd.print(temp); 180 lcd.setCursor(8,1); 181 lcd.print("C"); 182 } 183 184 if(mode == 2){ 185 lcd.clear(); 186 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 187 lcd.print("Humidity"); //PRINT TEXT 188 //delay(20); 189 lcd.setCursor(0,1); 190 lcd.print("H="); 191 lcd.setCursor(2,1); 192 lcd.print(humid); 193 lcd.setCursor(8,1); 194 lcd.print("%"); 195 } 196 197 if(mode == 3){ 198 lcd.clear(); 199 lcd.setCursor(0,0); //DEFINE LCD CURSOR POSITION 200 lcd.print("Barometric pres"); //PRINT TEXT 201 //delay(20); 202 lcd.setCursor(0,1); 203 lcd.print("P="); 204 lcd.setCursor(2,1); 205 lcd.print(pressure); 206 lcd.setCursor(10,1); 207 lcd.print("hPa"); 208 } 209}
Comments
Only logged in users can leave comments
di_papas
1 Followers
•1 Projects
0
1
bob-easyeda
24 days ago
Hi, your project is incredible! We’re offering up to $5000 in coupons for fabrication costs via OSHWLab Stars: https://oshwlab.com/activities/spark2023. Plus, we have cool programs to win PCB coupons, like Campus Ambassadors, Video Creators, and Collaboration Sponsorships. Interested in joining? Let’s chat!