Attiny85 AnalogRead Problems

Anyone ever run into a problem with your attiny not reading your analog pins correctly? Im getting readings like -1023 or 1023 or 0 value? How did you resolve the issue?

Here's what my code looks like now :

code as of 5/6/2015 8pm:

   
    int THERMISTORPIN = A1;
    int ADCcount = 0;
    int switchPin = PB1;
    int LED = PB0; // led 


    // button latch
    byte ButtonState;
    byte lastState = LOW;
    byte count = 0;
  
     //brightness level of each different mode 
    int low = 50; 
    int med = 140;
    int high = 255;             
    int dim = 0;
   
  
    void setup() 
    { 
 
     pinMode(switchPin, OUTPUT);
     pinMode(LED, OUTPUT);
 
      }


     void loop() 
     {

      ADCcount = analogRead(THERMISTORPIN);  
   
       delay(100);


      ButtonState = digitalRead(switchPin);

      if(ButtonState && ButtonState != lastState)  // button latch, no debounce   needed.
       {
          if(count < 4)                    
          count += 1;  // same as count = count + 5;
          else 
          count == 0;
       }    
  
        lastState = ButtonState;
  
       if (count == 0)
      {
          analogWrite(LED,0);
       } 
        else if (count == 1) 
             {
                 if(ADCcount < 500)
                       {
                         analogWrite(LED,high);
                        }
          else
            { 
               if(ADCcount > 525)
                    {
                       analogWrite(LED,low);
                     }
                 }            
              } 
             else if (count == 2 )
               {
                 analogWrite(LED,med);
                } 
          else if (count == 3) 
                {
                     analogWrite(LED,low);
                 }
          if(count >=4)
                {
                   count = 0;
                 }
          delay(50);
     }

dyIZY.png

How is the thermistor wired ?

      if (count == 0)
      {analogWrite(LED,0);} 
        else if (count == 1) { if(ADCcount < 500) {analogWrite(LED,high);}
          else{ if(ADCcount > 525){analogWrite(LED,low);}
           }            
              } 
          else if (count == 2 ){analogWrite(LED,med);} 
          else if (count == 3) {analogWrite(LED,low);}
          if(count >=4) {count = 0;}

Have you gone out of your way to make this section of code difficult to understand ?

To my mind this layout is preferable

  if (count == 0)
  {
    analogWrite(LED, 0);
  }
  else if (count == 1)
  {
    if (ADCcount < 500)
    {
      analogWrite(LED, high);
    }
    else
    {
      if (ADCcount > 525) 
      {
        analogWrite(LED, low);
      }
    }
  }
  else if (count == 2 )
  {
    analogWrite(LED, med);
  }
  else if (count == 3)
  {
    analogWrite(LED, low);
  }
  if (count >= 4)
  {
    count = 0;
  }

but I realise that it is a matter of opinion.

   if (count < 4) // This will check to see if the count is within a range of  0 - 255
      // anything over that, it will reset count back to 0. Of course
      count += 1;  // same as count = count + 5;

I am always suspicious of code where the comments don’t match the code. Am I to believe the comments or the code ?

@UKHeliBob Id believe the code over the comments… I didn’t proof read my comments. I changed the code more suitable for everyone else. I prefer having it closer together. Just preference I suppose.

8d6fb759783c7724c35b76b93c09d595c0645336.png

Are you sure that pin 7 on an ATtiny85 is an analogue in pin and if so that it maps to A3 in the Arduino IDE ? From my limited reading A3 is mapped to pin 2

See https://learn.sparkfun.com/tutorials/tiny-avr-programmer-hookup-guide/attiny85-use-hints

I should point out, however, that I am not on strong ground here having never used the device.

I re-read sparkfuns article you attached to no avail. I've even switched the analogRead pin from A1-A3 still I dont get a vaild reading.

another good resource I found with a wiring diagram and sample code was this : connecting sensors with attiny

and it still didnt solve my problem.

I know somebody knows the answer to this issue, it's gotta be something rudimentary. Maybe boards.tx file?

heres the datasheet from atmel: Atmel ATtiny85 Datasheet

Do you know of anyone who might know how to solve this issue?

Core?

i’ve selected, 8MHZ internal

Here: ATtiny85 Purchased from digikey

Is that what you mean? @CodingBadly

Physical pin 7 should be Arduino pin 2, and Analog pin 1... But you say you've tried it. That's the only arduino pin mapping I've seen for the 85...

And that's how it seems to work on my tiny85's. I'm using the core in my github sig, which may behave differently.

Are you getting the right values if measure the voltage on the pin with a volt meter? You say you're seeing 0 and 1023 - could it be that the connections to the thermistor and/or resistor are loose, so sometimes one of them isn't connected, so the voltage immediately goes to ground or vcc?

It's critical, always, to isolate whether a problem is hardware or software as soon as possible in the debugging.

@DrAzzy

We might be using the same core, I added mine from hi-low tech group to the compiler through the boards manager. hi low tech group how to program an attiny85

The readings ill get on the voltmeter is 4.98vcc and 4.85 at the end of thermistor.

I figured out where the values are by repeatedly changing the if statement values until the LED wen't low. Id narrow it down to a few points. Incredibly crude, if you ask me but ill do whatever to make this work.

I built a soldered pcb and the same model on a breadboard. They both are sitting next to me and react the same way. so (I) believe it's not hardware.

If you have it connected as shown, you're saying that:

At the point marked NTC, you're getting 4.85v?

Does it change when you put your finger on the thermistor?

That seems awfully high for ambient temperature - are you sure you're using the right value of resistor for the thermistor you're using?

@DrAzzy,

At NTC my measurement is 2.5v, then when ive pinch the thermistor with my fingers the voltage drops to 2.2v

4.95 is the vcc, I got the 4.95 - 4.85 from measuring the vcc to the ground of the thermistor not at the ntc point. whoops.

BlueStallion: Is that what you mean? @CodingBadly

No. You had to install something for the IDE to support programming t85 processors. What did you install? Where did you get it?

I assume from Reply #8, you installed the core available from there.

@Coding Badly

I assume from Reply #8, you installed the core available from there.

That's a correct assumption. Is there anything I was suppose to do to the file's once they are installed?

Reading an analog pin seems like elementary thing to do.. so I thought.

I’ve isolated just the analog read code, and it’s not changing the led output when i pinch the thermistor. Regardless of setting the if statement adc values higher or lower.

int LED = PB0; 
int ADCcount = 0;
int NTC = A1; // THERMISTOR

void setup(void) 
  {
    pinMode(LED,OUTPUT);
  }
 
void loop(void) { 
  
  ADCcount = analogRead(NTC);
 
 if (ADCcount > 525)
  {
    analogWrite(LED,25);
  }
    else 
  { 
     if(ADCcount < 520)
      {
      analogWrite(LED,255);
      }
   }

 delay(150); 
     
}
 void setup()
    {

     pinMode(switchPin, OUTPUT);
     pinMode(LED, OUTPUT);

      }

Shouldn't switchPin be INPUT ?

@klubfingers, You'd think so but it works as an output, my switch case works very well, never skips while being held down or out of count sequence.

BlueStallion:

int LED = PB0;  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


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

void loop(void) {

   analogWrite(LED,25);

     analogWrite(LED,255);

}

Arduino pin numbers are specified as a whole number. For the t85 processor that whole number is typically between 0 and 5. If this works…

  int LED = PB0;

…it is purely coincidental.

         if(count < 4)                   
          count += 1;  // same as count = count + 5;
          else
          count == 0;

No.

http://www.gammon.com.au/forum/?id=12153#trap3

          count += 1;  // same as count = count + 5;

And I wasn't referring to that extremely useless comment.

Does this make any difference...

...

void loop(void) { 
  
  ADCcount = analogRead( 1 );

...