Skip to content

Instantly share code, notes, and snippets.

@xxlukas42
Created September 15, 2018 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxlukas42/feb445f565be29d705fda2ef6f794c73 to your computer and use it in GitHub Desktop.
Save xxlukas42/feb445f565be29d705fda2ef6f794c73 to your computer and use it in GitHub Desktop.
Ambient light shield and LED shield for D1 Pro Mini
/*************************************************************
Wemos Lolin D1 PRO (ESP8266)
Basic demo
by Petr Lukas
*************************************************************/
// Install Adafruit_NeoPixel library https://github.com/adafruit/Adafruit_NeoPixel first
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
// Install [claws/BH1750 Library](https://github.com/claws/BH1750) first
#include <BH1750.h>
#define PIN D4
#define LED_NUM 7
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800);
BH1750 lightMeter(0x23);
void setup() {
Serial.begin(9600);
leds.begin(); // This initializes the NeoPixel library.
Wire.begin();
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
}
else {
Serial.println(F("Error initialising BH1750"));
}
}
void led_set(uint8 R, uint8 G, uint8 B) {
for (int i = 0; i < LED_NUM; i++) {
leds.setPixelColor(i, leds.Color(R, G, B));
leds.show();
delay(10);
}
}
void loop() {
uint16_t lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
// Tune sensitivity/response of LED shield by your needs. By default it reacts for changes between 0 and 255 lx
int intens = 255 - lux;
if(intens > 255) intens = 255;
if(intens < 0) intens = 0;
led_set(intens, 0, 0); // Red color by default
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment