Pages: 1 [2]   Go Down
Author Topic: Problems with ATTiny and it's ADC's  (Read 4506 times)
0 Members and 1 Guest are viewing this topic.
Tacoma, WA
Offline Offline
Full Member
***
Karma: 4
Posts: 192
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Brian from Tacoma, WA
Arduino evangelist - since Dec, 2010.

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3511
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include <SoftwareSerial.h>
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/

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

void setup(void)
{
    ser.begin(9600);
}

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;
    delay(1000);
}

Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Tacoma, WA
Offline Offline
Full Member
***
Karma: 4
Posts: 192
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Brian from Tacoma, WA
Arduino evangelist - since Dec, 2010.

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3511
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AVR pins default to input on power up/reset. Plus I'm lazy efficient smiley-lol  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.


* ATtiny85.PNG (32.85 KB, 972x413 - viewed 46 times.)
« Last Edit: September 25, 2012, 11:53:11 am by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1029
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.akafugu.jp/images/microcontroller_reference_sheet.pdf
Logged

Tacoma, WA
Offline Offline
Full Member
***
Karma: 4
Posts: 192
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the output from my Python test program:
Code:
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.
Logged

Brian from Tacoma, WA
Arduino evangelist - since Dec, 2010.

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3511
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Tacoma, WA
Offline Offline
Full Member
***
Karma: 4
Posts: 192
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Brian from Tacoma, WA
Arduino evangelist - since Dec, 2010.

Pages: 1 [2]   Go Up
Jump to: