# Tiny85 and LDR Analogue Read

Discussion in 'Microcontrollers, Programming and IoT' started by haroldjclements, Dec 13, 2013.

1. ### haroldjclements

24
1
Oct 17, 2013
Hello Forum.

I have been searching for a couple of hours now and still cant find an answer.

I am a newbie to microcontrollers and have question on analogue reading. I have an ATTiny85 that I am programming in C (not using Arduino libraries). I have successfully programmed and blown to the chip the "hello world" of microcontrollers (blinking LED). I did not use any bootloader or firmware, I just programmed the chip "out the box".

Now, I want to read the value of a LDR to decide whether to switch the LED on. However, I am unsure how you do this in code. As I said, I am a newbie, and I not too sure need a firmware / bootloader or whether I can do this out the box as I did the blinking LED. And if I can do the "out the box", could anyone supply or point to an example?

Thank you very much for your time,
Harold Clements

2. ### BobK

7,680
1,685
Jan 5, 2010
You would not need a bootloader. If you program the chip directly, you can access any of its features that way. I would think there would be example code on the AVR site, but I am a PIC guy, so I couldn't tell you for sure.

Bob

3. ### KrisBlueNZSadly passed away in 2015

8,393
1,267
Nov 28, 2011
Hi Harold and welcome to Electronics Point

As Bob says, you don't need any special features to use the AVR's on-board peripherals, including the ADC. You just need to set the control registers correctly, and connect your LDR to a suitable pin.

The ADC measures voltage, not resistance. So you need to convert the resistance of the LDR into a voltage. The simplest way is to use a voltage divider, with the LDR as one resistor, and a normal fixed resistor as the other resistor. A typical value for the fixed resistor would be around 10k.

So you connect the LDR from the positive rail to the ADC input pin on the ATTiny85, and you connect a 10k resistor from that pin to the 0V rail. When the LDR is in darkness, its resistance is high, and the resistor will pull the input down towards 0V, so you will get a low number from the ADC. When the LDR is in light, its resistance is low, so it pulls the input up towards VCC, and the ADC will give you a higher value.

There are a number of registers that you need to set up in order to use the ADC. Start with the data sheet, with the chapter on the ADC - chapter 18. It explains the features of the ADC and includes a block diagram and a description of the bits that need to be configured to make it operate the way you want. Once you have done that, your code will be able to initiate a conversion, wait for it to finish, and read the converted value, as described later in that chapter.

There are also lots of application notes on the Atmel web site; I'm sure there is at least one that covers how to use the ADC.

4. ### haroldjclements

24
1
Oct 17, 2013
Thanks for the replies guys...

I am happier that I have been looking in the right place (ADC) and that I know how that I do not need a bootloader. I did see an example that was using the voltage divider method; I will check that out again now I know that that is what I am looking for.

As I say, I do appreciate the time and effort in answering my question.

Kind Regards,
Harold Clements