Code and circuit TMP36

I use 3 different codes, and the same circuit, but is not work.

Why?

Code 1

int sensorPin = 0;
int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float cold = 0;
float hot = 0;
void setup()
{
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
}
void loop()
{
digitalWrite(red, LOW); //switch off the LEDs
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
// read the temperature and convert the result to degrees Celsius
sensor = analogRead(sensorPin); // TMP36 sensor output pin is connected to Arduino analogue pin 0
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-400; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
// now lets turn the LEDs if it is hot or cold
if (celsius>=hot)
{
digitalWrite(red, HIGH); // the LED red will light and is time to move
}
else if (celsius<=cold)
{
digitalWrite(yellow, HIGH); // the LED red will light and is time to move
}
else
{
digitalWrite(green, HIGH); // everything normal
}

}

Code 2

int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
int sensorPin = 0;
int reading = 0;

void setup(){
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
Serial.begin(9600); // activate the serual output connection
}

float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float toocold = 10;
float toohot = 20;

void loop()
{
int reading = analogRead(sensorPin);
float voltage = reading * 5.0; // convert raw sensor value to millivolts
voltage /= 1024.0;
float celsius = (voltage - 0.5) * 100; // convert millivolts to Celsius

if (celsius <= toocold) {
digitalWrite (red, LOW);
digitalWrite (green, LOW);
digitalWrite (yellow, HIGH);
}

else if (celsius >= toohot){ // if statment to turn the LED Red if Over Max Temperature
digitalWrite (red, HIGH);
digitalWrite (green, LOW);
digitalWrite (yellow, LOW);
}

else { // in all other cases temperature is ok
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, HIGH);
}

}

Code 3

int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;

void setup(){
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
Serial.begin(9600); // activate the serual output connection
}

float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float toocold = 15;
float toohot = 20;

void loop()
{
sensor = analogRead(0);
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius

if (celsius <= toocold) {
digitalWrite (red, LOW);
digitalWrite (yellow, HIGH);
digitalWrite (green, LOW);
}

else if (celsius >= toohot){ // if statment to turn the LED Red if Over Max Temperature
digitalWrite (red, HIGH);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
}

else { // in all other cases temperature is ok
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
}

}

I look forward to hearing from Forum!

Att.

Hi,

Try a all in one function like this :

float temperature = ((analogRead(A0) / 1024.0 * 5000.0 ) - 500.0)/10.0;

And you may should setup all the variables before the "void setup()" part, it's easier to read.

The comparison with treshold looks fine.

Aloyse,

When you say to declare all the variables before the "void setup()" part, like this:

int sensorPin = 0;
int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float temperature = 0;
float cold = 0;
float hot = 0;
void setup()
{
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
}
void loop()
{
digitalWrite(red, LOW); //switch off the LEDs
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);

Do you suggest another way to do it?

Thanks.

Hi,
Do any of the LEDs light?
Check your protoboard, some do not have continuity along the sides shown as the red and blue lines.
Some protoboards have a break in the middle so you don't get current flow the full length.
See attachment.
Tom.... :slight_smile:

Protoboards.jpg

Hi Tom,

Yes, my protoboard as that problem. But I already did that... I think is well done!

But I'm get going crazy. I've everything correct. Maybe is like bobdabiulder was saying, the problem is the sensor...

Thanks.

Hi,
What voltage do you get at A0 when you measure with the DMM?
Have you tested that your LEDs are connected the right way round?
Do you have a 10K potentiometer that you ca use for testing?

Tom... :slight_smile:

Sorry, but how I do that?

I've to put the DMM in volts. Then I've to put the red in the A0 and the black in the Vout on the sensor that is in the protoboard. And the measure as to be: 5V

Hi,
No, keep the sensor connected to A0.
Place Black(Neg) of DMM to arduino gnd, Red(Pos) of DMM to A0, use DC volts range.

Tom...... :slight_smile:

Hi,
try this bit of code.

int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
int sensorPin = A0;
int reading = 0;
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float toocold = 10;
float toohot = 20;

void setup() {
  pinMode(red, OUTPUT); // tell Arduino LED is an output
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(sensorPin, INPUT);
  Serial.begin(9600); // activate the serial output connection
  
}


void loop()
{
  reading = analogRead(sensorPin);
  Serial.print("Analog Pin Value   ");
  Serial.println(reading);
  delay (1000);
}

You will need to open up the monitor facility in the IDE and set it to 9600baud.
You should see, every second, the raw AtoD value, it should increase if you warm the sensor with your finger.

Tom.... :slight_smile:

1 Like

Yes, is working!
Started with 158, then I warmed the sensor and increased to 171.

So, I measure the A0, and I got 0,7V

I forgot to say that I'm using 147 ohm resistances, not 220 ohm

Could this be a problem?

Hi,
Good, now you need to add the formula to get your temperature, use serial.print to check it, then add the ifs and elses.
Do it in stages, get a stage working before adding another.

Tom.... :relaxed:
Serial.print and Serial.println is your friend when it comes to debugging.

1 Like

I'm catching that the problem maybe could be the conversion formula to celsius.

Because, now I'm doing the same thing but with the analog value and the leds are lighting!

Hi,
Fine mate, good to see you are progressing.
What is the formula you are using?

Tom... :slight_smile:

a lot of different formulas:

1

float voltage = reading * 5.0; // convert raw sensor value to millivolts
voltage /= 1024.0;
float celsius = (voltage - 0.5) * 100; // convert millivolts to Celsius

2

voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-400; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius

3

voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius

4 (suggested here by Aloyse)

float temperature = ((analogRead(A0) / 1024.0 * 5000.0 ) - 500.0)/10.0

Hi,
Try this link, they explain the equations and all.

Tom..... :slight_smile:

1 Like

Sorry, but when I code the conversion formula and upload the code, the print should it be in Celsius degrees, shouldn't it?

Because, is not being shown in degree celsius

It's working.

But I'm not getting the results for the LEDs!!!

I don't understand...

Hey, I know this is somewhat off-topic, but, with the tmp36 sensor, be VERY careful to connect the pins correctly. If you don't, it get extremely hot. I didn't notice that, and after having it in for about 30 seconds with my looking at other things, I heard a "pop" noise, and now it is very unstable. I know it's easy to replace, but BE CAREFUL with it! :slight_smile:

The TMP36 is working. Why you keep saying that?