Skip to content

Instantly share code, notes, and snippets.

@samilkorkmaz
Last active February 25, 2020 14:06
Show Gist options
  • Save samilkorkmaz/95c8cfb00f781d9d8d9e68f1dca24aef to your computer and use it in GitHub Desktop.
Save samilkorkmaz/95c8cfb00f781d9d8d9e68f1dca24aef to your computer and use it in GitHub Desktop.
Arduino Nano: LED brightness control with LDR
//Reference: https://www.instructables.com/id/Automatic-Lights-Using-LDR-Brightness-Control
int ledPin = 9;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int ldrValue = analogRead(A6); //with brightness, LDR resistance decreases, voltage increases at A6 which results in higher ldrValue. 5V = 1024 (10bit)
int brightness = map(ldrValue, 100, 1000, 255, 0);
Serial.print(ldrValue);
Serial.print(", ");
Serial.println(brightness);
analogWrite(ledPin, brightness);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment