DHT12.ino 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Description: Use DHT12 Sensor to read temperature, humidity and display the data on the screen.
  3. */
  4. #include <M5Stack.h>
  5. #include "DHT12.h"
  6. #include <Wire.h> //The DHT12 uses I2C comunication.
  7. DHT12 dht12; //Preset scale CELSIUS and ID 0x5c.
  8. /*
  9. For configuration library:
  10. DHT12 dht12("Scale temperature","ID device for I2C");
  11. On "Scale temperature" you can select the preset scale:
  12. CELSIUS, FAHRENHEIT or KELVIN.
  13. And on "ID device", you can put ID sensor, on DHT12
  14. normally is 0x5c.
  15. Examples:
  16. DHT12 dht12;
  17. The preset scale is CELSIUS and ID is 0x5c.
  18. DHT12 dht12(KELVIN);
  19. the preset scale is KELVIN and ID is 0x5c.
  20. DHT12 dht12(FAHRENHEIT,0x53);
  21. The preset scale is FAHRENHEIT and ID is 0x53.
  22. */
  23. void setup() {
  24. M5.begin();
  25. M5.Power.begin();
  26. Wire.begin();
  27. Serial.println("Prueba de libreria DHT12:");
  28. M5.Lcd.println("Prueba de libreria DHT12:");
  29. }
  30. void loop() {
  31. //Read temperature with preset scale.
  32. Serial.print("Temperatura: ");
  33. M5.Lcd.print("Temperatura: ");
  34. Serial.print(dht12.readTemperature());
  35. M5.Lcd.print(dht12.readTemperature());
  36. //Read humidity.
  37. Serial.print("*C Humedad: ");
  38. M5.Lcd.print("*C Humedad: ");
  39. Serial.print(dht12.readHumidity());
  40. M5.Lcd.println(dht12.readHumidity());
  41. //Read temperature as forced fahrenheit.
  42. Serial.println("%RH");
  43. Serial.println("%RH");
  44. Serial.print("Temperatura: ");
  45. Serial.print(dht12.readTemperature(FAHRENHEIT));
  46. //Read termperature as forced kelvin.
  47. Serial.println("*F");
  48. Serial.print("Temperatura: ");
  49. Serial.print(dht12.readTemperature(KELVIN));
  50. Serial.println("*K");
  51. delay(5000);
  52. }