Author Topic: digispark attiny 85 temperature controlled led  (Read 3380 times)

nik.evia

  • Newbie
  • *
  • Posts: 3
digispark attiny 85 temperature controlled led
« on: March 23, 2017, 05:38:52 pm »
Hello to everyone . I am a very  newbie in this thing so please be patient ..
i want to make a program with digispark attiny 85 a led and an lm 35 . so when the temperature is above a limit the led will turn on and when it is ok led will be turned off.
can u help me with the code and tell me what pins to use because i have been confused???

nik.evia

  • Newbie
  • *
  • Posts: 3
Re: digispark attiny 85 temperature controlled led
« Reply #1 on: March 25, 2017, 05:07:56 pm »
Nobody??????????

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: digispark attiny 85 temperature controlled led
« Reply #2 on: March 26, 2017, 01:23:21 am »
The forums around here are pretty quiet... there's only a couple of us regulars ;)

So, you have a LM35, which outputs a analog voltage relative to the temperature. It is supposed to output 0.5v at 0C, and change by 0.01 for every degree, and produce a maximum voltage reading of 1v (this last bit is important later for sensitivity).

To start off with, you have to use a pin on the Digispark that can measure voltages (as opposed to on-off/high-low), and these on the Digispark are P2,
P3, P4 & P5. P3 and P4 are the USB pins, so lets avoid them. This leaves P2 and P5, either of which should be fine.

So now that the pin to use for temperature reading out of the way, other consideration, the LED pin, can be just about any, or just use the on-board led, so onto the code.

In a nutshell, you want something that does this

Code: [Select]

get temperature

if temperature higher than limit
   turn led on

if temperature lower than limit
  turn led off

rinse, repeat

So, the next question are what is the temperature, and how frequently does it have to check. Those aren't that important, as you can put stuff in the code to allow that to be easily changed and accounted for.

So, something like this might do (note, I don't have a LM35 to verify the results, so I leave that up to you... but it seems to work fine with the MCP9700As that I use, which operate very similarly to the LM35). Other things to be aware of are that the temperature will need calibrating somewhat... due to inaccuracies in the reference voltage and LM35, it won't give a 100% reading... so you'll need to work out what fudge factor/offset you need to correct its temperature reading. Also, there is no hysteresis how the code toggles the LED, so it toggles hard on that set voltage, rather than have a little 'padding' around it so the LED doesn't just keep toggling on and off when it is very near the temperature trigger point.

Hopefully this will get you started anyway ;)

Code: [Select]

/*
   If you divide 1.1V over 1024, each step up in the analog reading is equal to
   approximately 0.001074V = 1.0742 mV. If 10mV is equal to 1 degree Celcius,
   10 / 1.0742 = ~9.31. So, for every change of 9.31 in the analog reading,
   there is one degree of temperature change.
*/

#define LED_PIN 1  //P1
#define LM35_PIN 1 //Confusingly, analog 1 is P2

#define WARN_TEMP 35
#define TIME_BETWEEN_CHECKS 2000

// put your setup code here, to run once:
void setup()
{
  analogReference(INTERNAL1V1); //use internal voltage reference as will be more accurate / consistent
  analogRead(LM35_PIN); //reference voltage is actually set on the first analogRead after changed
  analogRead(LM35_PIN); //... lets just discard the first measurement as it may be off

  pinMode(LED_PIN, OUTPUT); //make it so we can actually turn the led on and off!
}

// put your main code here, to run repeatedly:
void loop()
{
  int reading = analogRead(LM35_PIN);
  float tempC = reading / 9.31;

//  read MCP9700A
//  float tempC = analogRead(LM35_PIN)*1.1/1024.0;
//  tempC = tempC - 0.5; tempC = tempC / 0.01;
 
  if (tempC >= WARN_TEMP)
  {
    digitalWrite(LED_PIN, HIGH); // led on as threshold exceeded
  }
  else
  {
    digitalWrite(LED_PIN, LOW); // led off as threshold not reached
  }

  delay(TIME_BETWEEN_CHECKS); // hang around for specified milliseconds
}


« Last Edit: March 26, 2017, 01:27:43 am by PeterF »

nik.evia

  • Newbie
  • *
  • Posts: 3
Re: digispark attiny 85 temperature controlled led
« Reply #3 on: April 01, 2017, 04:40:27 pm »
thank you very much my friend  it worked very good, i also used another pin for an outside led.
Do u know if i can control a 12v relay with the power output of digispark??

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: digispark attiny 85 temperature controlled led
« Reply #4 on: April 01, 2017, 08:15:34 pm »
If you mean your coil requires 12V to pull in the relay then you would need to drive 12V to the coil through an appropriate transistor with the transistor base connected to an output pin the digispark.  I'm sure you could find circuit examples on this if you search the net.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: digispark attiny 85 temperature controlled led
« Reply #5 on: April 02, 2017, 05:16:54 am »
Or one with an optoisolator. Does it have to be a 12v relay? You could use a module like the 1 channel version of this board, which you would be able to power from the same power source that powers the digispark, or if it had to be 12v, this one would do.

Otherwise if you want to make it yourself or you already have the relay, as exeng said, you'll need to look up a circuit (basically a resistor, transistor, diode and the relay) to let the 5v logic level pins of the digispark control the 12v needed for the relay coils. Don't forget to add the diode between the relay coil, as this protects the transistor from an EMF spike from when the coil switches off.