Chipkit Uno and LDR warning example

This example is like a dark activated alarm using an LDR and a LED, in this case when the value read in drops below a certain level the LED will light. In the real world you could make a dark activated night light or alarm.

Its a fairly basic circuit, I connected the LED to D8, no real reason. LDR still connects to A0

Schematic and Layout

Code

[codesyntax lang=”cpp”]

int sensorPin = A0; // select the input pin for the ldr
int ledPin = 8; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() 
{
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
  sensorValue = analogRead(sensorPin);
  if(sensorValue <= 400)
  {
    digitalWrite(ledPin, HIGH);
    delay(500);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
}

[/codesyntax]

Related posts

Chipkit Max32 and ADXL335 accelerometer example

Chipkit Max32 and LSM303 Accelerometer example

Chipkit Max32 and LSM303 Magnetometer example

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More