HELLO THERE!!!
How are you guys? I hope everyone is fine and in a good shape. This week, I would like to share with you guys on controlling LED brightness using LDR(photoresistor/Light Dependent Resistor). Now let's get started,shall we?
OK, first thing's first, Items needed:
HARDWARE:
1.Arduino UNO
How are you guys? I hope everyone is fine and in a good shape. This week, I would like to share with you guys on controlling LED brightness using LDR(photoresistor/Light Dependent Resistor). Now let's get started,shall we?
OK, first thing's first, Items needed:
HARDWARE:
1.Arduino UNO
2.LDR x1
3.LED x1
4. Resistor (220 ohms) x1
SOFTWARE:
1. Arduino IDE
After obtaining the items, let's assemble the circuit. The circuit is fairly simple. One leg of the LDR is connected to the 5V while the other leg are connected to the GND and analog pin of Arduino. The sketch below will depict on the assembly of the circuit:
The assembly of the circuit |
After assembling the circuit, let's code it. The code is pretty straightforward, we read the LDR readings and use it to control LED's brightness. The code is shown below:
int ldr = A0; //ldr analog pin
int val;//value to keep analog readings
int LED = 13;//LED digital pin
void setup() {
pinMode(LED,OUTPUT);//Declare led as output
Serial.begin(9600);//serial communication at 9600 bps
}
void loop() {
val = analogRead(ldr);//keep the analog readings in the val
Serial.println(val);//serial print val
analogWrite(LED,val);//Light up LED using the val value
}
Upload the code to the Arduino and that's it!! There are many improvements that can be made from this experiment such as adding more LED's and LDR to have more controls. Well, that's all from me this week, guys. See you next week and Happy Tinkering!!!
Comments
Post a Comment