Thursday 30 October 2014

ATTiny85 Tutorial 4 – Reading Analog (Variable) Input

This is the 4th tutorial in the Freetronics Experimenters Kit converted to ATTiny85.

With this tutorial, the main changes from the original tutorial is again the pin assignments. But, also, the ATTiny85 is not connected to the PC via the USB cable, so Serial.begin, Serial.print and Serial.println are redundant. I have removed them from the sketch.

ATTiny85 Pins - Project 4

In this tutorial, the light sensor is connected to first Analog Digital Comparator pin (physical pin 7 ADC1). In your sketch, the analog pins are A1, A2 and A3 … so for the purpose of this tutorial, we’re using A1. The LED is connected on pin 0 … got that, A1 and 0 … right, let’s move on.

The modified sketch is as follows.

int led = 0;
int lightLevel;

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

void loop()
{
  lightLevel = analogRead(A1);
  digitalWrite(led, HIGH);
  delay(lightLevel);
  digitalWrite(led, LOW);
  delay(lightLevel);
}

Within the loop function, the ATTiny85 reads the value of the light sensor, this gives a value of 0 – 5V. The value is read as an integer value from 0 – 1023. This value is assigned to the lightLevel variable that is used to set the blink rate of the LED. The more light there is, the slower the blink rate.

breadboard - Project 4

The yellow wire connects the light sensor to A1 on the ATTiny85 and the LED is connected to 0 on the ATTiny85.

To test this circuit, I powered it up and then turned on my LED lamp above the sensor … as you would expect, the blink rate slowed down, then I swung the lamp away from the sensor to give an analog light variation and the blink rate sped up as less light was hitting the sensor … all working as you would expect.

Tutorial 4 circuit running.

Again, I programmed the ATTiny85 using my ATTiny85 ICSP and powered the breadboard using my 5V power regulator.

That’ll do for now, I’ll come back to these tutorials next week.

Check out the rest of the tutorials here.

No comments:

Post a Comment

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.