ADC: Attiny85 vs Atmega2560

I am trying to read temperature from LM335 on Attiny85. Have a simple piece of code that runs fine on Arduino Mega 2560. However when I transfer the code into Attiny85, it does not seem to read temperature correctly. Are there any differences in handling ADC by Attiny and Mega? I read that Attiny ADC is slower. I therefore have 1 sec delay after analogRead(A1). I also read a few threads on this topic here but none of them seem to explain exactly what might be wrong here. :confused:

Do you read the right pin? If A1 exists on ATTiny it is very probably ADC1 which is PB2, digital pin 2 (which is pin 7 of DIP package).

The attiny ADC is the same one as in the '328p and 2560 w/regards to timing and most other matters.
Like, they're pretty much identical. Same prescalar options, ADCs are both equally fast, unless the attiny core you're using does something weird/dumb and configures the prescaler differently than how one would expect.

There is no need to put a delay after analogRead() - analogRead() blocks until the read is complete anyway.

Most likely, you're reading the wrong pin.

Well, I use analogRead(A1), physical pin 7. I am sure I use the right pin because when I disconnect it I see immediate reaction of Attiny. Someone in another thread pointed out, that Attiny's ADC pins are providing different results. Seems weird, but I will give it a try.

Can you try another ATTiny? Another ADC pin? Check with DMM/scope if the value on the pin is the one you expect?
Digispark has USB pins connected to PB3 and PB4 but not to PB2 (it may the be cause of different readings reported). Still isn't it possible you have somehow something connected to the pin which is confounding the measurement? Such as programming device (if you program it directly) adding pull-up/down or just large capacitance?
All AVR ADCs are very similar, I don't thing there will be great difference in performance in general. But your particular chip may be damaged or connected in a wrong way - you should track every difference between Mega and Tiny layout and you may find the cause.

I also read in this thread that it is good to use the 0.1uF bypass capacitor from VCC to GND when dealing with ADC. I do not have it. Can this make me trouble?

I've used the attiny85 numerous times with ADC and no problems. Now, you may have a screwed-up set of core files ... you may wish to try DrAzzy's tiny core.

Hot Yet?

Ray

Well, I am not using any core. This is my program. I used an example from the link in the beginning of the program.

/*
 http://www.learningaboutelectronics.com/Articles/LM335-temperature-sensor-circuit.php
 *
 * Schematic:
 *   [Ground] --  [LM335] --+- [2k-resistor] --[Vcc (5 or 3.3v)]
 *                          |
 *                          |
 *                      Analog Pin 1
 *
 *
//       LM335      +-\/-+                    ATTINY85     +-\/-+
//                 1|    |8  Vcc          Ain0 (D 5) PB5  1|    |8  Vcc
//                 2|    |7               Ain3 (D 3) PB3  2|    |7  Ain1(D 2)
//                 3|    |6               Ain2 (D 4) PB4  3|    |6  PWM (D 1)
//             GND 4+----+5  Adj                     GND  4+----+5  PWM (D 0)
 */                                  


//#define MEGAmode 1           

#define xLow HIGH          
#define xHigh LOW

int LM335pin = A1;         
int relayPin = 3;          
int ledPin = 4;            

int relayOn=0;             

void setup() 
{
                                     #if defined(MEGAmode)
                                     ledPin = 13;
                                     Serial.begin(9600);                                     
                                     #endif

  pinMode (relayPin, OUTPUT);
  pinMode (ledPin, OUTPUT);
  digitalWrite (ledPin, xLow);
  digitalWrite (relayPin, xLow);

}
void loop() 
{
   int rawvoltage= analogRead(LM335pin);
   float millivolts= (rawvoltage/1024.0) * 5000;
   float kelvin= (millivolts/10);
   float celsius= kelvin - 273.15;
                                     #if defined(MEGAmode)
                                     Serial.print(celsius);
                                     Serial.println(" degrees Celsius");
                                     #endif
delay(1000);

  if ( celsius < 10 )    
  { digitalWrite (relayPin, xHigh);   
                                      #if defined(MEGAmode)
                                      Serial.println("++++++++++++++++++++++ Relay On");
                                      #endif
    relayOn=1;                        
  }
  
  if (relayOn == 1)                   
 
  { if (celsius > 20)                 
    {   digitalWrite (relayPin, xLow);
                                      #if defined(MEGAmode)
                                      Serial.println("------------------------ Relay Off");
                                      #endif
        relayOn=0;
    }  
  }
                                      
  delay(10000);      
  
}

Could you have an internal pullup or something enabled?

2K resistor could be too small. Try a larger value, like 6.8K or 10K.

gv445:
Well, I am not using any core. This is my program. I used an example from the link in the beginning of the program.

Without installing a third party hardware package (colloquially called a "core"), you can't program a tiny85 with the arduino IDE - there's no attiny85 option in the tools -> board menu. So you must have installed a core - which one?

gv445:
I also read in this thread that it is good to use the 0.1uF bypass capacitor from VCC to GND when dealing with ADC. I do not have it. Can this make me trouble?

You must have a 0.1uf capacitor from Vcc to Gnd right next to the chip. Not just if you're using the ADC, but for basic functionality - otherwise you can get all sorts of unspecified behavior. Decoupling caps are not optional.

gv445:
‚ÄúI am not using any core.‚ÄĚ

Do you have an Arduino Uno selected for the Board?
If so that probably won’t work. Find a core that supports the chip and select it. Just try DrAzzy core, i.e. in preferences add it in Additional Boards Manager URL and then select one of his boards so the IDE will use DrAzzy implementation of analogRead().

http://drazzy.com/package_drazzy.com_index.json

And if you don’t want to use a core then switch to Atmel Studio, or learn how to use Makefiles (that is what I do now) and use the Arduino core as a reference (and a puzzle).

OK, I confused core with the libraries that sometimes are included in the program itself. Of course I have Attiny core installed in Arduino IDE and do have Attiny85 as a choice in the Boards menu. Not sure, however, which core that is. Will try adding a capacitor now. Thanks!

Maybe it is a VREF problem ?

zoomx:
Maybe it is a VREF problem ?

What do you mean?

The ADC make a voltage measurement referred to VREF. On Arduino UNO and Mega VREF is tied to the 5V line and the 5V line came from USB or from the regulator if you use the power socket.
When you use USB it happens that the voltage and VREF it is close to 5V but not 5V, for example 4.95 V. When voltage is equal to VREF you get 1023 from the ADC.

You are using a sensor that give a current proportional to temperature, so you are using shunt resistor. Is it a precise resistor?