Introduction: Using Photo-Resistors Like Buttons

Thank you for viewing my Instructable and more importantly thank yourself for exploring deeper into Arduino and electronics. It has proven to be a very rewarding hobby and a pleasure to teach to students of all ages. This Instructable shows how to use Photo-Resistors like buttons. You might be thinking why not just use regular buttons like buttons. One of my favorite parts of working on projects is no reason is ever needed....but since you asked here is a few. Buttons are some times hard to put anywhere but on the bread board they were designed for. Also the concept of button debounce explains how these mechanical buttons arent always on/off. There is often multiple signals sent when only one click was desired. Search button debounce to find solutions to this problem. I like using Photo-Resistors or light dependent resistors (LDR) because there is no moving parts and more flexability.I will show you how to set up one or multiple LDR and then use that like a button for some other task. I show an example of using two LDR to sweep a Servo motor left and right at the end but the possibilities are always endless.

Step 1: Parts Needed​

In order to set up one Light Dependent Resistor (LDR), The following items could be used:

Arduino Uno and Arduino IDE

Bread Board

Photo-resistor (LDR)

Jumper Wires

10K ohm resistor

If you plan on making more than one button for more options you will another photo-resistor, 10k resistor and more jumper wires. besides that make plans for what items you will need for your specific goal. I will be using a Servo motor that will move left when one LDR is covered or right when another LDR is covered. experiment with different uses like controlling LEDs

Step 2: Diagram and Code

Here you can find the basic set up for what is known as a voltage divider. This will allow the LDR to read the amount of light hitting it and use those values in our sketch. The idea is that when you put your finger over the LDR, the values will change as it senses darkness and that will simulate clicking a button.one end of the LDR gets connected to the +5V pin. the other end of the LDR and the resistor both get connected to the same analog pin, say A0. Finally the other end of the resistor goes back to GND. i encourage you to tinker with different resitors to see what range of vaules you get. I used a 10k Ohm resitor and was getting values between 500-1023. This is a suitable range as i can just say anything under 750 is dark (button press) and over 750 means light (button unpressed) main sections of code are listed bellow. This is an example of using if statements to turn on and LED connected to pin 10 when the LDR reads a dark value and to turn the light off when there is a light value. Use the Serial monitor to check what values you are receiving.

void setup() {
Serial.begin(9600);

pinMode(A0,INPUT);

pinMode(10,OUTPUT);

}

void loop() {

int val = analogRead(A0);

Serial.println(val);

if (val < 750){

digitalWrite(10, HIGH);

}

if (val < 750) {
digitalWrite(10,LOW);

}

}

Step 3: Apply Your New Method to Anything

Anything a button can do is now an option for you. control lights, create a keyless piano, or as you see below in my quick video, you can use two LDR and signal a servo motor to sweep left or right at your wish! Thank you for Reading please share any questions comments and insight.

Comments

author
tomatoskins made it! (author)2017-06-19

What a cool idea! I always like seeing things used in unusual ways.

About This Instructable

181views

9favorites

License:

Bio: Always learning.....
More by tliguori330:Cereal Box Marble MazeElectrostatic WindsUsing Photo-Resistors Like Buttons
Add instructable to: