Last active
February 25, 2020 14:06
-
-
Save samilkorkmaz/95c8cfb00f781d9d8d9e68f1dca24aef to your computer and use it in GitHub Desktop.
Arduino Nano: LED brightness control with LDR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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