2: Arduino Tutorial- Reading sensor data! (Super Easy)

Enough of blinking LEDs. Now, lets move on to something more interesting, like conditionally turning on an LED. The aim of the tutorial is to familiarise you with the process of using almost any sensor with Arduino.

Let us first get the Arduino to do something, and then understand why we did, what. 

Step 1: Hook up the following circuit. You will need:
  1.  1 x Breadboard
  2.  1 x Arduino (any will do)
  3.  1 x 1kΩ resistor
  4.  1 x 330Ω resistor
  5.  1x LDR (light-dependent-resistor)
  6.  1x LED (any colour)

Circuit Diagram


int ldrPin=A0;
int led=13;
int sensorValue;

void setup() {
pinMode(ldrPin, INPUT);//declare LDR pin as INPUT
pinMode(led, OUTPUT); //declare LED pin as OUTPUT
Serial.begin(9600); //begin serial communication

void loop() {
  sensorValue=analogRead(ldrPin); //read LDR value
  Serial.println(sensorValue); //print value to serial monitor
  if(sensorValue < 200){
    //change the condition according to your LDR's readings
    digitalWrite(led, HIGH); // turn on LED if sensor value <200
    //turn it off otherwise
    digitalWrite(led, LOW);
  delay(50);//delay to keep the serial monitor readable