Mit Elegoo Mega R3 und einem Fotowiderstand steuern, wie viele LEDs leuchten, so wie eine Art Helligkeitsanzeige. Auch im Seriellen Plotter zu sehen.
Vor alle LEDs ist je ein 220 Ohm Vorwiderstand.

Verdrahtung:
Anode → je ein 220 Ω Widerstand → Arduino‑Pin
Kathode → GND
LDR Pin1 → 5V
LDR Pin2 → A0
10 kΩ Widerstand Pin1 → A0
10 kΩ Widerstand Pin2 → GND
Code:
// Anzahl der LEDs
const int ledCount = 8;
// Pins der LEDs (hier: 10 LEDs an Pins 22–29)
int ledPins[ledCount] = {22, 23, 24, 25, 26, 27, 28, 29};
// LDR / Fotowiderstand
// LDR + 10k Widerstand als Spannungsteiler an A0
const int ldrPin = A0;
void setup() {
// LED-Pins als Ausgang setzen
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop() {
// LDR-Wert einlesen (0–1023)
int sensorValue = analogRead(ldrPin);
// Serielle Plotter-Ausgabe
Serial.print("LDR: ");
Serial.println(sensorValue);
// Umrechnen auf Anzahl der LEDs, die leuchten sollen
// Beispiel: 0 = 0 LEDs, 1023 = alle LEDs
int ledsOn = map(sensorValue, 0, 1023, 0, ledCount);
// LEDs entsprechend schalten
for (int i = 0; i < ledCount; i++) {
if (i < ledsOn) {
digitalWrite(ledPins[i], HIGH);
} else {
digitalWrite(ledPins[i], LOW);
}
}
delay(50);
}
Youtube:
