Use analogRead() with attiny13a

Hi

I tried to programm an attiny13a to make a cold chamber controller (a fan will start when it's too hot and stop when enought cold, controled by an solid state relay (ssr in this code)) Temperature set at 27C for test purpose)

int ssr = 0; //Digital pin for my solid state relay (or LED for test
int sensor = 3; //Analog pin for my LM335A sensor
void setup() 
{
  pinMode(ssr, OUTPUT);    
  //  Serial.begin(9600); //For debugging
}

void loop() 
{
  delay(5000);
  int temp = analogRead(sensor); //read the LM335A sensor

    if(temp > 617) //(((température voulue)+273,15)*10/5000*1024.0)(617=27C) compare temperature with desired one
  {
    digitalWrite(ssr, HIGH); //turn on ssr
  }

  else
  {
    digitalWrite(ssr, LOW); //turn off SSR
  }
  //  Serial.println(temp); //check temp value while debugging
}

I tried it on my arduino uno R3 and it work fine but when i put it on my attiny the digitalRead is stuck on 1023 when connected or 0 when grounded, This sketch turn on the LED when it raise 27 celcius (when i touch the sensor with my hand) and turn off when it cool down. It work on Uno R3 but not on attiny.

I used this shematic for my sensor: (but with the good pin number and 2K resistor, not as shown)

Did someone see anything in my code or know something about attiny known problem with analogRead?

Thank you!

Which core are you using for the t13?

This one:

and this one:
http://forum.arduino.cc/index.php?topic=89781.0

I also fixed the problem with the function delay as describe here:

I tried another one that i can't find now...

I suspect @smeezekitty numbered the ADC pins the same way they are traditionally number for the ATtiny85 processor.

int sensor = 3; //Analog pin for my LM335A sensor
int temp = analogRead(sensor); //read the LM335A sensor

ADC3 is physical pin 2 (PB3 - PCINT3/CLKI/ADC3). Is that how you have the sensor wired?

Note: You really should make those pin numbers const.

Yes I connected it to pin 2, my circuit is exactly that:

I'm a bit confused about pin number on datasheet vs IDE pin asigment, does the Arduino IDE recognise that I want an analog pin if I call an analogRead? The physical pin 2 are the analog and digital input 3 (I chose it to not take the chance with pin number :stuck_out_tongue: )

And about the const for pin number, I think I doesn't understant exactly what you mean... I'm a newbie programmer and I'm not a beast in english XD

Thank for your patience :slight_smile:

Making the pin numbers constants...

const int ssr = 0; // <<<<<<<<<<<<<<<<<<<< //Digital pin for my solid state relay (or LED for test
const int sensor = 3; // <<<<<<<<<<<<<<<<<<<< //Analog pin for my LM335A sensor

void setup() 
{
  pinMode(ssr, OUTPUT);    
  //  Serial.begin(9600); //For debugging
}

void loop() 
{
  delay(5000);
  int temp = analogRead(sensor); //read the LM335A sensor

    if(temp > 617) //(((température voulue)+273,15)*10/5000*1024.0)(617=27C) compare temperature with desired one
  {
    digitalWrite(ssr, HIGH); //turn on ssr
  }

  else
  {
    digitalWrite(ssr, LOW); //turn off SSR
  }
  //  Serial.println(temp); //check temp value while debugging
}

Making constants actual constants reduces the amount of Flash and SRAM your sketch uses which is extremely important with a processor like an ATtiny13.

Try this...

const int ssr = 0; // <<<<<<<<<<<<<<<<<<<< //Digital pin for my solid state relay (or LED for test

void setup() 
{
  pinMode(ssr, OUTPUT);    
  //  Serial.begin(9600); //For debugging
}

void loop() 
{
  delay(5000);
  int temp = analogRead( A3 ); // <<<<<<<<<<<<<<<<<<<< //Analog pin for my LM335A sensor // read the LM335A sensor

    if(temp > 617) //(((température voulue)+273,15)*10/5000*1024.0)(617=27C) compare temperature with desired one
  {
    digitalWrite(ssr, HIGH); //turn on ssr
  }

  else
  {
    digitalWrite(ssr, LOW); //turn off SSR
  }
  //  Serial.println(temp); //check temp value while debugging
}

Thank for unsigned tip :slight_smile: , my C learning on internet tutorial is a bit far...

I really don't understand why the attiny doesn't turn the LED on...

If I write:

if(temp =! 617)

if(temp <= 617)

or

if(temp >= 617)

Or if I set the if AND else to HIGH the LED doesn't turn on and With "3" or "A3" doesn't make any difference

But, if I upload the blink sketch with pin 0 The LED blink...

I also tried on two other chip (new) and I tried with or without "Burn Bootloader" before uploading sketch

You do not have the sensor wired correctly...
http://www.ti.com/general/docs/datasheetdiagram.tsp?genericPartNumber=LM335A&diagramId=24434

For the sensor it work just well on my Arduino Uno R3, your diagram just
doesn't show the adjust pin, I just don't read anything on analog pin (nor
grounded nor +5V to analog 3) I will try to find others cores on the forum,
I will post the result of my tests. :slight_smile:

I WILL program that Attiny and make it do what I want! :sunglasses:

Thanks again for your patience. I don't know if it is possible that I
just buy a "cheap" chip, I mean, I don't know if it is possible that I get
five non-working attiny13???

Crud. Your diagram is correct. Putting +5V on the bottom confused me. Sorry about that.

Try this: Remove the temperature sensor and resistor. Remove the delay from your sketch. Upload the new sketch. With the sketch running, connect A3 to GND. The LED should be dark. Connect A3 to +5V. The LED should be lit.

If that does not work, the problem has to be software. Try a different core.

That work now! :slight_smile: I downloaded the latest core13 version of smeezekitty (The core I used first
was an old version, I think) The analogRean was not working at all (no reaction to grounded or
+5V on analog pin)

The working core can be found here (permanent link to the latest version):

I will post a photo of my finished project with the diagram and final code :slight_smile: I hope that someone will
read it and don't waste time with outdated library found on google :wink:

Thank you again Coding Badly!

You are welcome. I'm glad to know you have it working.

Hi sivolc73, I have a problem like you did. But now, I don't know what must I do with the core13 that you gave link for download. Please help me!

1 Like