Attiny85 light sensor switch

Step 1: Parts List
  • Arduino Attiny85
  • LDR (Light Dependent Resistor) 5MM Photoresistor
  • 1 Channel 5V Relay Module
  • 10k resistor
Step 2: Wiring

Attiny85 light sensor switch_Elec-Cafe

Step 3: Code
// Pins
int sensorPin = A2; //Analog Pin
int lightPin = 2; //Digital Pin

// Variables
int lowThreshold = 400; //low light level
int highTreshold = 500; //high light level

void setup() {
pinMode(sensorPin, INPUT);
pinMode(lightPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(sensorPin); //Read the sensor
if (sensorValue < lowThreshold){ //If light level is low, switch light on
digitalWrite(lightPin, HIGH);
}
if (sensorValue > highTreshold){ //If light level is high, switch lights off
digitalWrite(lightPin, LOW);
}
}