Go Down

Topic: Problems with ATTiny and it's ADC's (Read 13172 times) previous topic - next topic


Ok, I managed to get it working. Instead of calling it Pin 3, I called it A2. The HLT core would not let me do that, it wouldn't take pinMode(A2, INPUT);, or any of the A1, A2, A3 defines. This Tiny core does, and it reads the port. Seems to be reading a bit off, but I can apply a correction factor to it as long as it's consistant.
Thanks for the help troubleshooting this, guys.

Jack Christensen

Seems to be reading a bit off, but I can apply a correction factor to it as long as it's consistent.

This was still bothering me, so I gave it a shot. I get near perfect results with the code below. With the inputs tied to ground, three zeroes. Tied to Vcc, three 1023 readings. Connected to a voltage divider consisting of two 10K resistors from Vcc to ground, I get mostly 507 or 508, the three are all usually within one of each other. ATtiny85-20PU at 8MHz, Arduino-Tiny core.

Code: [Select]
#include <SoftwareSerial.h>
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/

int a1, a2, a3;
SoftwareSerial ser(1, 0);

void setup(void)

void loop(void)
    a1 = analogRead(1);    //dip pin 7
    a2 = analogRead(2);    //dip pin 3
    a3 = analogRead(3);    //dip pin 2
    ser << _DEC(millis());
    ser << ' ' << _DEC(a1) << ' ' << _DEC(a2) << ' ' << _DEC(a3) <<endl;


You never set the pins to input.  I had also read that you need to toss the first result when switching analog pins. So you're definitely having great success. You're using serial on pin 1, AND reading analog from it.... Apparently the pin numbering is nothing like I imagined.

Jack Christensen

Sep 25, 2012, 06:45 pm Last Edit: Sep 25, 2012, 06:53 pm by Jack Christensen Reason: 1
AVR pins default to input on power up/reset. Plus I'm lazy efficient XD  The readings I quoted were after it ran a bit, I missed the first reading actually, so they may have been off a bit, but I don't know.

The pin numbering makes me crazy too, what between the DIP pins, the Arduino pins, the port number (e.g. PB2) etc. With The ATtiny85 and Arduino-Tiny, for digital pins, the pin number matches the port (PBn) number, which is nice and straightforward. The argument to analogRead() is really the ADC MUX channel number, not a pin number. So SoftwareSerial ser(1, 0); refers to DIP pin 6 (PB1) and DIP pin 5 (PB0) respectively, and the calls to analogRead() correspond to the DIP pins in the comments in the code.


I think this sheet is very handy when dealing with the tiny's



Here's the output from my Python test program:
Code: [Select]

Zone 1 ADC = 740
Zone 2 ADC = 729
Zone 1 Voltage = 0.705
Zone 2 Voltage = 0.703
Zone 1 is 20.46C
Zone 2 is 20.27C
Zone 1 is 68.8F
Zone 2 is 68.5F

I changed my sketch to use the internal VRef, hence the higher ADC numbers than before. Anyways, using my fluke meter, pressing against the pins of the ATTiny itself, both analog 2 and 3 are seeing the exact same voltage, which was .704 during this testing. I'm not exactly sure why the CPU is reporting different numbers, but I apply a correction factor (vary the ARef voltage in the equation "ARef/1024") to get the correct voltage. It seems that if I get the voltage to read correctly, the temperature comes out correct.
Anyways, thanks for the help on this one guys.

Jack Christensen

Hmm, yes that is off a bit. As a test, I'd connect pins 2 and 3 together and then connect them to a single sensor. Or to a voltage divider or potentiometer. If they don't track very close, within a count or two, then I'd say the microcontroller has issues. If they do track, then I'd look to the sensor circuitry for differences. What sensors are being used, anyway?


I'm using TMP36 sensors. I'll give that a shot. I've got a whole bunch of these ATTiny85's, I'll try swapping a different one in.

Go Up