Home AVR Attiny85 LDR and LED – arduino version

Attiny85 LDR and LED – arduino version

In this example we will connect an LDR to an Attiny85, once the value read drops below a certain value we will switch an LED on.

For this example we used the Arduino IDE to write our code. We’ll post a different version using Atmel Studio or equivalent soon.

Schematic

LDR and LED

LDR and LED

Code

int analogPin = 3; //pin 2
int LEDPin = 1; //pin 6
int val = 0;

void setup()
{
  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin,LOW);
  delay(2000);
  digitalWrite(LEDPin,HIGH);
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  if(val < 200)
  {
    digitalWrite(LEDPin,HIGH);
    delay(200);
  }
  else
  {
    digitalWrite(LEDPin,LOW);
  }
}

Links

You may also like