Hello, I've been working on this for a while a simple TMP36 temperature sensor which I get a reading from and if it is below 70 degrees a led goes on and if above it goes off and it will not work when I upload it to my attiny85. It works perfectly when I test it out on my Arduino as the first code shows. The second code is where I am having an issue I adjusted the code to work with an Attiny but the led always stays on. Any idea where I am going wrong here, please help.
int sensorPin = A0; // TMP36 sensor pin designation on the Ardiuno
int led = 13; // LED pin designation on the Ardiuno
void setup()
{
Serial.begin(9600); // Start the serial connection with the computer
pinMode(led, OUTPUT); // Set the pin as output
}
void loop()
{
float reading = analogRead(sensorPin); // Read raw number from the pins sensor
float voltage = reading * 5.00;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100;
float temperatureF = (temperatureC * 9 / 5) + 32;
if(temperatureF < 70.000)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
Serial.print(voltage, 3); // Shows the sensor reading
Serial.println( ); // Print a new line
Serial.print(temperatureF, 3); // Shows the temperature in F
Serial.println( ); // Print a new line
Serial.println( ); // Print a new line
delay(1000);
}
Here is the code I upload to an Attiny.
int sensorPin = 3; // TMP36 sensor pin designation on the AtTiny (IC PIN 2)
int led = 0; // LED pin designation on the AtTiny
void setup()
{
//Serial.begin(9600); // Start the serial connection with the computer
pinMode(led, OUTPUT); // Set the pin as output
pinMode(sensorPin, INPUT); // Set the pin as input
}
void loop()
{
int reading = analogRead(sensorPin); // Read raw number from the pins sensor
float voltage = reading * 5.00;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100;
float temperatureF = (temperatureC * 9 / 5) + 32;
if(temperatureF < 70.000)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
//Serial.print(voltage, 3); // Shows the sensor reading
//Serial.println( ); // Print a new line
//Serial.print(temperatureF, 3); // Shows the temperature in F
//Serial.println( ); // Print a new line
//Serial.println( ); // Print a new line
delay(1000);
}
If you are only using the analog pin for analogRead(), no need to call pinMode().
Which physical pin are you using for the LED? Have you tried a simple blink sketch to make sure it is working?
There are different declarations of reading in the two sketches:
float reading = analogRead(sensorPin); // Read raw number from the pins sensor
...
int reading = analogRead(sensorPin); // Read raw number from the pins sensor
But since you then use it in a calculation that converts to float, not sure that matters.
Hi Ray - I think the int may matter so I changed it to float. Also, I'm using physical IC pin 5 for the LED. I also tested blink and fade sketches to make sure everything is working with the attiny and the upload and those sketches do work. I also tried changing the below code to 1,000,000 degrees and it still did not work and I know it's less then a million degrees in here.
I'm using IC pin 2 for the input. And it looks like using int sensorPin = A3; instead of int sensorPin = 3; makes a difference I think. I changed the code below to 12 and the led goes on and when I touch the tmp36 a few seconds later the led goes off. So it looks like I'll have to try to debug using the method Erni suggested.
I have the same issue with ATtiny85, it seems that the analog inputs on attiny have very low impedance, loading the sensor pin, while the better high impedance of Arduino ATMega328P works well.
I tried to measure the volts from the sensor, when I connect the central pin (sensor) to Arduino Uno or ATMega328P, the TMP36 outputs the correct voltage for the correct temperature (example 16° C = 0.7V), while when I connect it to an analog input on ATTiny85, it outputs around 0.1 or less volts.
I do not know if my analysis is right, someone more electronics expert will tell me eventually.