Using Photo Sensor As Analog Imput
by popkalab in Circuits > Arduino
1496 Views, 29 Favorites, 0 Comments
Using Photo Sensor As Analog Imput
Here we are using several photo resistors as buttons. The idea is when you cover the resistor is like you are pressing a button.
What We Need
- Arduino
- 100k resistor
- Photosensor
- Cable
- Male headers
- 100k resistor
- Photosensor
- Cable
- Male headers
Connecting the Sensor
To connect the photosensor we need to add a resistor. Our three
connections are the ground, VCC 5V and the data sent to
Arduino.
connections are the ground, VCC 5V and the data sent to
Arduino.
Placing the Sensors
Then we simply placed the senors an a plexiglas, for prototyping
we just stick them with tape.
we just stick them with tape.
Preparing Arduino Connection
The ground as well as the vcc has to be connected to one cable to connect it to the Arduino. The clearest is to solder it with the resistors to small piece of circuit board.
Data Input and Arduino Connection
For a good connection it is better to solder the data cables to
male headers so you can easily plug them to the Arduino (shown
in the red square)
Later we plug the output pins to the Arduino marked by the
yellow square
That is it! Follow bellow the arduino code. Have fun!
// select the input pin for the photosensor
int photo0 = 0;
int photo1 = 1;
int photo2 = 2;
int photo3 = 3;
int photo4 = 4;
int photo5 = 5;
// variable to store the value coming from the sensor
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
//digital out
int out13 = 13;
int out12 = 12;
int out11 = 11;
int out10 = 10;
int out9 = 9;
int out8 = 8;
void setup() {
Serial.begin(9600);
pinMode(out13, OUTPUT);
digitalWrite(out13,LOW);
pinMode(out12, OUTPUT);
digitalWrite(out12,LOW);
pinMode(out11, OUTPUT);
digitalWrite(out11,LOW);
pinMode(out10, OUTPUT);
digitalWrite(out10,LOW);
pinMode(out9, OUTPUT);
digitalWrite(out9,LOW);
pinMode(out8, OUTPUT);
digitalWrite(out8,LOW);
}
void loop() {
val0 = analogRead(photo0);
val1 = analogRead(photo1);
val2 = analogRead(photo2);
val3 = analogRead(photo3);
val4 = analogRead(photo4);
val5 = analogRead(photo5);
if(val4>400){
digitalWrite(out13,HIGH);
Serial.println("pressed!");
}else if(val4<350){
digitalWrite(out13,LOW);
}
if(val3>600){
digitalWrite(out12,HIGH);
Serial.println("pressed!");
}else if(val3<550){
digitalWrite(out12,LOW);
}
if(val2>900){
digitalWrite(out11,HIGH);
Serial.println("pressed!");
}else if(val2<850){
digitalWrite(out11,LOW);
}
if(val0>400){
digitalWrite(out9,HIGH);
Serial.println("pressed!");
}else if(val0<350){
digitalWrite(out9,LOW);
}
if(val5>630){
digitalWrite(out8,HIGH);
Serial.println("pressed!");
}else if(val5<615){
digitalWrite(out8,LOW);
}
}
male headers so you can easily plug them to the Arduino (shown
in the red square)
Later we plug the output pins to the Arduino marked by the
yellow square
That is it! Follow bellow the arduino code. Have fun!
// select the input pin for the photosensor
int photo0 = 0;
int photo1 = 1;
int photo2 = 2;
int photo3 = 3;
int photo4 = 4;
int photo5 = 5;
// variable to store the value coming from the sensor
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
//digital out
int out13 = 13;
int out12 = 12;
int out11 = 11;
int out10 = 10;
int out9 = 9;
int out8 = 8;
void setup() {
Serial.begin(9600);
pinMode(out13, OUTPUT);
digitalWrite(out13,LOW);
pinMode(out12, OUTPUT);
digitalWrite(out12,LOW);
pinMode(out11, OUTPUT);
digitalWrite(out11,LOW);
pinMode(out10, OUTPUT);
digitalWrite(out10,LOW);
pinMode(out9, OUTPUT);
digitalWrite(out9,LOW);
pinMode(out8, OUTPUT);
digitalWrite(out8,LOW);
}
void loop() {
val0 = analogRead(photo0);
val1 = analogRead(photo1);
val2 = analogRead(photo2);
val3 = analogRead(photo3);
val4 = analogRead(photo4);
val5 = analogRead(photo5);
if(val4>400){
digitalWrite(out13,HIGH);
Serial.println("pressed!");
}else if(val4<350){
digitalWrite(out13,LOW);
}
if(val3>600){
digitalWrite(out12,HIGH);
Serial.println("pressed!");
}else if(val3<550){
digitalWrite(out12,LOW);
}
if(val2>900){
digitalWrite(out11,HIGH);
Serial.println("pressed!");
}else if(val2<850){
digitalWrite(out11,LOW);
}
if(val0>400){
digitalWrite(out9,HIGH);
Serial.println("pressed!");
}else if(val0<350){
digitalWrite(out9,LOW);
}
if(val5>630){
digitalWrite(out8,HIGH);
Serial.println("pressed!");
}else if(val5<615){
digitalWrite(out8,LOW);
}
}