I am trying to add an Arduino Micro to a miniature kit that included 7 LEDs. The kit included a battery pack with 3x AA batteries to power the LEDs. Unfortunately, I have no information on the specs of the LEDs.
Id like to essentially turn the kit into a nightlight. Id be using a photo-resistor (LDR) to detect light amounts and if dark, the LEDs turn on.
I have attempted to connect the 7 LEDs in parallel to the Arduino while powered via USB, however the LEDs did not come on when a loss of light was detected.
This led to much troubleshooting.
I confirmed everything (Code, Physical wiring, correct pins etc..) worked with another kit I have that includes only 3 LEDs.
To confirm the LEDs worked and in the chance the Arduino was not able to power the 7 LEDs, I powered them via 3x AA batteries directly. The LEDs come on at full brightness.
I then attempted to have the 7 LEDs powered via the 3x AA Batteries and the LDR powered by the Arduino/USB. I get the same result, LEDs do not power with a loss of light.
Serial monitor indicates the LDR is working as expected.
In the diagram attached, I have both the 3 LED Miniature house, and the 7 LED Miniature house drawn. Ultimately, this is what I would be going for. I have attempted to isolate each as described above to work out what is going wrong.
To reiterate the Arduino is connected via USB power to a PC. The 3xAA Batteries would be used only to power the 7 LEDs when light is low.
Here is the code I am using:
const int ldrPin = A5; // Photoresistor connected to analog pin A5
const int ledPin = 2; // LED connected to digital pin 2
const int ledPin2 = 7; // LED connected to digital pin 3
const int threshold = 250; // Threshold for light level detection
const int hysteresis = 20; // Hysteresis value to prevent rapid toggling
bool ledState = false; // Track the LED state
void setup() {
pinMode(ldrPin, INPUT); // Set the LDR pin as INPUT
pinMode(ledPin, OUTPUT); // Set the LED pin as OUTPUT
pinMode(ledPin2, OUTPUT); // Set the LED pin as OUTPUT
digitalWrite(ledPin, LOW); // Turn off the LED initially
digitalWrite(ledPin2, LOW); // Turn off the LED initially
Serial.begin(9600); // Initialize serial communication for debugging (optional)
}
void loop() {
int lightLevel = analogRead(ldrPin);
Serial.println(lightLevel); // Print a message (optional)
if (lightLevel < (threshold - hysteresis) && !ledState) {
digitalWrite(ledPin, HIGH); // Turn on the LED when it's dark
digitalWrite(ledPin2, HIGH); // Turn on the LED when it's dark
Serial.println("Darkness Detected!"); // Print a message (optional)
ledState = true; // Update the LED state
} else if (lightLevel > (threshold + hysteresis) && ledState) {
digitalWrite(ledPin, LOW); // Turn off the LED when there is light
digitalWrite(ledPin2, LOW); // Turn off the LED when there is light
ledState = false; // Update the LED state
}
delay(100); // Delay to prevent rapid LED toggling
}
Any help on what I'm missing or doing wrong would be great.