Anton & GhostNova
Anton Anton
I was just finishing a custom mechanical keyboard, 3D‑printed case and all, and I'm thinking about adding a little firmware tweak so it reacts to ambient light. Would you want to help me debug it?
GhostNova GhostNova
Sounds like a neat hack. Give me the sketch and the sensor wiring, and let me see where the logic is breaking. I'll keep an eye on the power draw too, those light sensors can sneak in some noise. Just hit me with the code and a quick rundown of the hardware setup.
Anton Anton
Sure thing. **Hardware** - Arduino Nano (or any 328P) - BH1750 digital light sensor (I²C) - 3.3 V regulator if you’re running the sensor on 3.3 V, otherwise just 5 V is fine. - 4.7 kΩ pull‑ups on SDA/SCL if you’re using the breakout board’s pull‑ups, otherwise add them yourself. - 5 V USB power to the Nano, sensor shares that 5 V. - Connect GND to GND. - Connect SDA to A4, SCL to A5 on the Nano. - If you’re using the sensor on a custom PCB, just follow the same pins. **Sketch** ```cpp #include <Wire.h> #include <BH1750.h> BH1750 lightMeter; void setup() { Serial.begin(115200); Wire.begin(); // Initialise the sensor if (!lightMeter.begin()) { Serial.println("Sensor not found"); while (1); } Serial.println("Sensor OK"); } void loop() { // Read the lux value uint32_t lux = lightMeter.readLightLevel(); Serial.print("Lux: "); Serial.println(lux); // Simple threshold logic – adjust as needed if (lux > 300) { Serial.println("Bright enough – LED on"); // put your LED control here } else { Serial.println("Too dark – LED off"); } delay(500); // half a second } ``` That’s the core. If you hit a hiccup, let me know which line trips you. I’ll keep an eye on the power draw – just a couple of mA for the sensor, so it should be fine. Let me know if you need help wiring the LED side or anything else.
GhostNova GhostNova
Looks solid, but check the I²C address just in case the sensor’s set to 0x23 or 0x5C. If the Serial prints “Sensor not found” it’s usually a pull‑up or wiring glitch. The 500 ms delay is fine for a demo – for a real keyboard you might want to debounce the threshold or use a moving average to smooth flicker. Let me know if the LED never toggles; maybe the pin’s set to INPUT by default, just set it to OUTPUT in setup. Good luck.
Anton Anton
Sounds good, thanks for the heads‑up. I’ll pin the LED on D13 and set it to OUTPUT in the setup. If it still doesn’t flip, I’ll double‑check the pull‑ups and the I²C address. Appreciate the help.