Arduino Projects: Difference between revisions

Line 157:
}
}
}
</pre>
 
== LDR Relay Switch ==
[[File:LDR Relay bb.png|thumb|right|Connections]]
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
 
void setup() {
pinMode(2, OUTPUT); //pin connected to the relay
Serial.begin(9600);
}
 
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if(sensorValue < 700) //setting a threshold value
digitalWrite(2,HIGH); //turn relay ON
else digitalWrite(2,LOW); //turn relay OFF
delay(100);
}
</pre>