ATtiny85 Hangs after 3 to 10 seconds

I have an ATtiny85 that reads thermistor and sends the data over 433Mhz. But, after a few readings it stops outputting, and i need to manualy reset that chip. Why does that chip hang? I am running it on a breadboard. Using a 12VDC 1A center pin positive power supply with lm7805.

//THE THERMISTOR IS A 10K THERMISTOR
//                                          DIAGRAM
//  GROUND-----------10K RESISTOR-----------INPUT A7-----------10K THERMISTOR-----------5V

#include <VirtualWire.h>

int Thermostat = A1;
float ThermostatReading = 0;
float Res = 0;

float TempK = 0;    //Tempature in K
float TempC = 0;    //Tempature in C
float TempF = 0;    //Tempature in F
float L1 = 0;
float L2 = 0;
float L3 = 0;
float L4 = 0;
float L5 = 0;
float AvgK = 0;
int Avg = 1;        //Average the temp

char Temp[7];

void setup()
{
  // Initialise the IO and ISR
  vw_set_tx_pin(0);          //Setting up the transmitter pin
  vw_setup(2000);            // Bits per sec
}

void loop()
{
  //Get the Thermistor resistance
  Res = analogRead(Thermostat);
  Res = 1023 / Res - 1;
  Res = 10000 / Res;

  //Calculate the Temp
  ThermostatReading = analogRead(Thermostat);
  TempK = log(10000.0 * (1024.0 / ThermostatReading - 1));
  TempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * TempK * TempK)) * TempK);
  
  //Average the Readings
  if (Avg == 1)
  {
    L1 = TempK;
  }
  if (Avg == 2)
  {
    L2 = TempK;
  }
  if (Avg == 3)
  {
    L3 = TempK;
  }
  if (Avg == 4)
  {
    L4 = TempK;
  }
  if (Avg == 5)
  {
    L5 = TempK;
    AvgK = (L1 + L2 + L3 + L4 + L5);
    AvgK = (AvgK / 5);
    TempC = AvgK - 273.15;
    TempF = TempC * 1.8 + 32;
    dtostrf(TempF, 6, 2, Temp);
    vw_send((uint8_t *)Temp, strlen(Temp));
    vw_wait_tx();
    delay(200);
    Avg = 0;
  }
  if (Avg <= 0 || Avg >= 6)
  {
    Avg = 1;
  }
  Avg = Avg + 1;
  delay(200);
}

Where did you find the formula for the temperature calculation? From the sensor data sheet?

  1. The variable res does not appear to be used.
  2. If analogRead() returns 0, you will get a divide by 0 error in the TempK calculation.

Do you have a 0.1uf cap between Vcc and Gnd right next to the chip? (for DIPs, I normally put it over the top) This is necessary for reliable operation, but unfortunately many tutorials don't mention it...

I don't think the reading will ever be 0 with the wiring described (though with a bad connection - and he is using breadboard - it could be)...

Yes, I guess you have to assume the wiring diagram is incorrect and he has really got the sensor on A1 as the code implies.

6v6gt:
Where did you find the formula for the temperature calculation? From the sensor data sheet?

  1. The variable res does not appear to be used.
  2. If analogRead() returns 0, you will get a divide by 0 error in the TempK calculation.

I got the formula from arduino playground I beleive. And, I removed the res part of this. I adapted the code over from an ATtiny84, I was printing resistance and TempC and TempF. But, since i only want to send TempF i got rid of res.

DrAzzy:
Do you have a 0.1uf cap between Vcc and Gnd right next to the chip? (for DIPs, I normally put it over the top) This is necessary for reliable operation, but unfortunately many tutorials don't mention it...

I don't think the reading will ever be 0 with the wiring described (though with a bad connection - and he is using breadboard - it could be)...

I do not have a 0.1uf cap between Vcc and Gnd. I have a pull-up resistor 10K on Vcc to Reset.

I shortned the code a bit, and simplified it.

//THE THERMISTOR IS A 10K THERMISTOR
//                                          DIAGRAM
//  GROUND-----------10K RESISTOR-----------INPUT A7-----------10K THERMISTOR-----------5V

#include <VirtualWire.h>

int Thermostat = A1;
float ThermostatReading = 0;

float TempK = 0;    //Tempature in K
float TempC = 0;    //Tempature in C
float TempF = 0;    //Tempature in F
float AvgK = 0;
int i;

char Temp[7];

void setup()
{
  // Initialise the IO and ISR
  vw_set_tx_pin(0);          //Setting up the transmitter pin
  vw_setup(2000);            // Bits per sec
}

void loop()
{
  for (i = 0; i < 5; i++)
  {
    //Calculate the Temp
    ThermostatReading = analogRead(Thermostat);
    TempK = log(10000.0 * (1024.0 / ThermostatReading - 1));
    TempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * TempK * TempK)) * TempK);

    //Running Average
    AvgK = AvgK + TempK;

    delay(200);

    if (i == 4)
    {
      AvgK = (AvgK / 5);
      TempC = AvgK - 273.15;
      TempF = TempC * 1.8 + 32;
      dtostrf(TempF, 6, 2, Temp);
      vw_send((uint8_t *)Temp, strlen(Temp));
      vw_wait_tx();
      delay(200);
      AvgK = 0;
    }
  }
}

lineman2208:
I do not have a 0.1uf cap between Vcc and Gnd.

Well, add one, it is not optional. Without it the chip can unexpectedly crash or reset.

DrAzzy:
Well, add one, it is not optional. Without it the chip can unexpectedly crash or reset.

I added a 50v 0.1uf cap per your instructions. And, I also added a SPST reset button. I also soldered for this to rule out the breadboard. And, after sometimes one reading it hangs, sometime after 30 readings it hangs. After it hangs I press the reset button I added and it goes again, but hangs eventualy.

//THE THERMISTOR IS A 10K THERMISTOR
//                                          DIAGRAM
//  GROUND-----------10K RESISTOR-----------INPUT A7-----------10K THERMISTOR-----------5V

#include <VirtualWire.h>

int Thermostat = A1;
float ThermostatReading = 0;

float TempK = 0;    //Tempature in K
float TempC = 0;    //Tempature in C
float TempF = 0;    //Tempature in F
float AvgK = 0;
int i;

char Temp[7];

void setup()
{
  // Initialise the IO and ISR
  vw_set_tx_pin(0);          //Setting up the transmitter pin
  vw_setup(2000);            // Bits per sec
}

void loop()
{
  for (i = 0; i < 10; i++)
  {
    //Calculate the Temp
    ThermostatReading = analogRead(Thermostat);
    TempK = log(10000.0 * (1024.0 / ThermostatReading - 1));
    TempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * TempK * TempK)) * TempK);

    //Running Average
    AvgK = AvgK + TempK;

    delay(200);

    if (i == 9)
    {
      AvgK = (AvgK / 10);
      TempC = AvgK - 273.15;
      TempF = TempC * 1.8 + 32;
      dtostrf(TempF, 6, 2, Temp);
      vw_send((uint8_t *)Temp, strlen(Temp));
      vw_wait_tx();
      AvgK = 0;
    }
  }
}

Temp TX Schematic.pdf (94.6 KB)

This is the schematic from your pdf:
Temp-TX-Schematic.png
There might be some issue with the LM7805.
You should allways use capacitors with the LM7805 too, take a look at the datasheet (section: typical applications): 0.33µF on input and 0.1µF on output. Otherwise the voltage may oscillate.

Data sheet showed non adjustable output regulators do not need caps

The first datasheet for the LM7805 from a Google search contradicts your claim / confirms what @uxomm said.

This would be a good time for you to post a reference to your datasheet.

Input cap required if great distance from the input voltage. It is not!
Output cap is not required for stability.
Read everything! Even *

At start of loop turn on LED. At some point in loop turn it off. When the ATTiny hangs is the LED on or off? If it changes it is probably a power problem. Otherwise it is software problem and you may localize where the ATTiny hangs by changing location where you turn off the LED...

lineman2208:
Read everything! Even *

I did. You did not. I can prove you did not from the rest of your quote.

lineman2208:
Input cap required if great distance from the input voltage. It is not!

That is not what is written in that footnote. I suggest you carefully reread that footnote.

A screenshot is not a reference.

I am not going to play anymore of this childish game. Someone else will have to help you.

It's TI's datasheet:

I do not know whether this applies to all manufacturer's versions of the 7805. I would always include the capacitor.

DrAzzy:
It's TI's datasheet:
http://www.ti.com/lit/ds/symlink/lm340.pdf

I do not know whether this applies to all manufacturer's versions of the 7805. I would always include the capacitor.

I don't think my lack of a cap is the problem. And that is a screen shot of the ti data sheet. So how is that admissible? The cap is there to smooth the ac sine wave. From my diagram of the circuit you can see it is powered from a 9vdc battery. So no sine wave. I multimeter over the chip and get a solid 5.01vdc.

Smajdalf:
At start of loop turn on LED. At some point in loop turn it off. When the ATTiny hangs is the LED on or off? If it changes it is probably a power problem. Otherwise it is software problem and you may localize where the ATTiny hangs by changing location where you turn off the LED...

I think I will have to try this suggestion.

It is generally a good idea to place at minimum a bypass capacitor at the input power connection of each integrated circuit, so the tiny85 should have one even if it is not needed for the regulator. @lineman2208 are you a lineman? perhaps some explanation of what those bypass capacitors are doing. They are not really smoothing, they change the circuits natural oscillating frequency. They are sort of like the weights that are hung on the long runs of a power line, that keep them from developing damaging mechanical oscillations. Anyway, do whatever you want.

ron_sutherland:
It is generally a good idea to place at minimum a bypass capacitor at the input power connection of each integrated circuit, so the tiny85 should have one even if it is not needed for the regulator. @lineman2208 are you a lineman? perhaps some explanation of what those bypass capacitors are doing. They are not really smoothing, they change the circuits natural oscillating frequency. They are sort of like the weights that are hung on the long runs of a power line, that keep them from developing damaging mechanical oscillations. Anyway, do whatever you want.

Alright, my hands are up. I don't want to be the ***hole that pushes away people that help them. The datasheet says 0.22uf and another user said 0.33uf. So, my gut says follow the datasheet, correct?

Well I am not saying follow the datasheet, I am saying put a .1uF at the tiny (if you have a .22uF try that). If that does not do the trick then add a cap at the power supply also. If it is still locking up then you have a software issue (and I am not good at software).

ron_sutherland:
Well I am not saying follow the datasheet, I am saying put a .1uF at the tiny (if you have a .22uF try that). If that does not do the trick then add a cap at the power supply also. If it is still locking up then you have a software issue (and I suck at software).

I have a 0.1uf cap over the tiny. Vcc to Gnd. And, I have a 10K pull up resistor on reset pin. I will add a 0.1uf cap on 7805. From input to gnd. I do not have a .22uf cap or 0.33uf cap.