Introduction

Attiny is an 8-bit microcontroller created by Atmel in 1999. Attinys are small chips that often have fewer inputs/outputs than their competitors from Microchip, for example.

The advantage of an Attiny is its low power consumption and limited number of pins, which are well suited to small electronic circuits.

In this lesson, we’ll look at how to vary the intensity of a LED as a function of external light. To do this, the light will be captured by a photoresistor whose resistance will vary according to the light received:

Programming

Here’s the program to vary the intensity of the LED according to the luminosity received by the photoresistor:

int lecture_photoresistance = 0; // Variable for reading photoresistance value
int led =0; // Pin for the led
void setup()
{
  pinMode(led, OUTPUT); // Output the led
  pinMode(A1, INPUT); // Photoresistor is input
}

void loop()
{
  lecture_photoresistance = analogRead(A1); // We read the value coming from the photoresistor
  
  if(lecture_photoresistance > 500){ // If it's daylight, we turn on the led
  	analogWrite(led, 255);
  }
  
  else{
    analogWrite(led, 0); // If it's nighttime, turn off the night light
  }
}

Here is the Tinkercad simulation of our circuit with the photoresistor: