LightMeter_LCD.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <Wire.h>
  2. #include <AS_BH1750.h>
  3. #include <LiquidCrystal.h>
  4. /*
  5. * LightMeter_LCD
  6. *
  7. * Version 1.2
  8. * Datum: 05.08.2013
  9. *
  10. * Das Programm benutzt den BH1750 (Umgebiungslichtsensor)
  11. * und zeigt die Werte in Lux auf einem 16x2-Symbol-LCD.
  12. *
  13. * Verdrahtung (UNO, Nano...)
  14. *
  15. * BH1750:
  16. * Sensor SCL pin an A5
  17. * Sensor SDA pin an A4
  18. * Sensor VDD pin an 5V
  19. * Sensor GND pin an GND
  20. * Sensor ADDR pin frei
  21. *
  22. * LCD in 4-Bit-Modus:
  23. * LCD RS pin an digital pin 8
  24. * LCD RW pin an digital pin 13
  25. * LCD Enable pin an digital pin 9
  26. * LCD D4 pin an digital pin 4
  27. * LCD D5 pin an digital pin 5
  28. * LCD D6 pin an digital pin 6
  29. * LCD D7 pin an digital pin 7
  30. *
  31. *
  32. * Copyright (c) 2013 Alexander Schulz. All right reserved.
  33. *
  34. * This program is free software: you can redistribute it and/or modify
  35. * it under the terms of the GNU General Public License as published by
  36. * the Free Software Foundation, either version 3 of the License, or
  37. * (at your option) any later version.
  38. *
  39. * This program is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. * GNU General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU General Public License
  45. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  46. */
  47. AS_BH1750 lightMeter;
  48. // Setup LCD-Shield
  49. LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
  50. void setup() {
  51. // Display initialisieren
  52. lcd.clear();
  53. lcd.begin(16, 2); // 16x2 Zeichen
  54. lcd.setCursor(0,0);
  55. lcd.print("LightMeter v1.0");
  56. lcd.setCursor(0,1);
  57. lcd.print("Initializing...");
  58. delay(1000);
  59. lcd.clear();
  60. if(!lightMeter.begin()){
  61. // Prüfen, ob Sensor vorhanden ist
  62. lcd.clear();
  63. lcd.setCursor(0,0);
  64. lcd.print("BH1750 not found");
  65. lcd.setCursor(0,1);
  66. lcd.print("check wiring!");
  67. while (1) {
  68. delay(1000);
  69. }
  70. }
  71. }
  72. void loop() {
  73. char clux[9];
  74. // Werte auslesen und aufbereiten
  75. float lux = lightMeter.readLightLevel();
  76. dtostrf(lux, 8, 1, clux);
  77. lcd.setCursor(0,0);
  78. lcd.print("Light level: ");
  79. lcd.setCursor(5,1);
  80. lcd.print(clux);
  81. lcd.print(" lx");
  82. delay(500);
  83. }