This sketch guide you to create a LED animation transition using LDR ( Light Dependent Resistor) with Arduino. Photo Resistor are variable resistor which change resistance depending on the light hitting the sensor. When you put your hand closer to the sensor, you block an increasing the amount of light, which increases the resistance of the LDR. as you move your hand away the amount of light hitting the surface of LDR increase thus decreasing the resistance.
The change in the resistance of the LDR(Light Dependent Resistor) will affect the voltage being read at one of the Arduino’ Analog Input (A0). As resistance increase, the voltage drops (and vice versa).
V = IR ( V = Voltage / I = Current / R = Resistance )
Voltage reading will be used to select which LED to light up.
Electronic Parts Required
- Arduino
- 9x LED
- 9x 220k Resistors
- 1x LDR (Light Dependent Resistor)
- Jumper Wires
Arduino Sketch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
int photoRPin = 0; // Define the analog pin photoresistor connected to (A0) void setup() { // initialise the LED Pins as OUTPUTS for (int i=4; i<14; i++){ // Loop to increment pinMode (i, OUTPUT); } } void loop(){ //Turn off all the LEDs before continuing for (int i=4; i<14; i++){ digitalWrite(i, LOW); } int photoRead = map(analogRead(photoRPin), 120, 600, 4, 13); /* You need to Make sure the value of photoRead does not go beyond the values of 4 and 13 */ int ledPin = constrain(photoRead, 4, 13); /* Turn the LED HIGH for a fraction of a second */ digitalWrite(ledPin, HIGH); delay(10); digitalWrite(ledPin, LOW); } |