Trying to use a TMP36 to control a IRF520

Hello, I'm having a problem trying to get a TMP36 to control an IRF520.

The point of all of this to make a miniature radiator. As I've been told by my teacher, the TMP36 measures the temperature and if the temperature is below a certain centigrade the IRF520 will allow current to pass through and into a resistor which will heat up and when it reaches the desired temperature it will turn off.

What I want to happen is the TMP36 to register a temperature of 25 celsius, if lower: turn on MOSFET and if Higher: Turn MOSFET off

What I think happens is that my TMP36 us registering a temperature, but something in my code does not let the IRF520 pass current into my resistor
(Instead of the resistor I am using a little light to show if the current is flowing)

My code:

//TMP36 Pin Variables

int sensorPin = A0; 

#define control 2

void setup()

{
  Serial.begin(9600);  

  pinMode(control,OUTPUT);// define control pin as output
}
 
void loop()      
               
{
 int reading = analogRead(sensorPin);  

 float voltage = reading * 5.0;

 voltage /= 1024.0; 

 Serial.print(voltage); Serial.println(" volts");

 float temperatureC = (voltage - 0.5) * 100 ;  

 Serial.print(temperatureC); Serial.println(" Grader C");

 delay(1000);

 {

  if(temperatureC < 25) {

 digitalWrite(control,HIGH); // turn the MOSFET Switch OFF

  }

  else(temperatureC > 25 ); {

    digitalWrite(control,LOW); // turn the MOSFET Switch ON

  }
 }
}

There is not current limit resistor for the LED.

It looks like the yellow wire (to the LED anode) shorts the power supply.

The IRF520 MOSFET is not a logic level device so will not fully turn on with 5V. Use an IRL520 instead

What value of temperature do the serial prints tell you?

Please read the forum guidelines to see how to post code. Pictures of code are not desirable.

I am positive the yellow wire doesnt short out the power. I currently don't have a IRL520 and don't have the time to order one. Is it possible to work around this problem.

The serial prints says 19 - 20 degrees Celsius

To turn on an IRF520 completely, the gate-source voltage needs to be about 10V.

In the red rectangle it seems that both the black and red wires are plugged into ground terminals.
in the yellow rectangle the yellow wire goes from - on the bottom of the breadboard to + on the top. That is not a short?

Change this:

else(temperatureC > 25 ); {

To

else if(temperatureC > 25 ); {

Better:

else if(temperatureC > 26 ); { // 1 degree deadband to prevent ON / OFF jitter

5V on the IRF520 gate should allow enough current through to light an LED WITH a 470 Ohm current limit resistor in series. Not a power resistor though. What is the value of that resistor?
Replace that orange jumper in the LED cathode circuit with a 330 or 470 Ohm resistor.

The red cable is connected to 5V and the black to GND. And the yellow cable is going from - to -.

I do have a better power supply but I don't know how to use it correctly. It gives 17V which should be more then enough right??

The resistor I am going to use is a Cement 5W10 Ohm J

17V / 10Ω = 1.7 Amps * 17V = 28.9 Watts. Your 5W resistor will burn up! To get 5W from 10Ω would be only 7.07V. That could be done with PWM but I don't believe the IRF520 can do it without overheating and failing.
Did you get your LED circuit to work?

The point is to make it heat up so much that it will radiate heat like a radiator, although I don't want to burn it up completely. No I did not get my LED circuit to work, the LED flickers on a little but it does not light up.

I removed some stray brackets, should work now, you can try the 5W resistor, just be careful. Do you know how to do PWM?

//TMP36 Pin Variables

int sensorPin = A0;
#define control 2

void setup()
{
  Serial.begin(9600);
  pinMode(control, OUTPUT); // define control pin as output
}

void loop()
{
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  Serial.print(voltage); Serial.println(" volts");
  float temperatureC = (voltage - 0.5) * 100 ;
  Serial.print(temperatureC); Serial.println(" Grader C");
  delay(1000);
  if (temperatureC < 25) {
    digitalWrite(control, HIGH); // turn the MOSFET Switch OFF
  }
  else if(temperatureC > 26); {
    digitalWrite(control, LOW); // turn the MOSFET Switch ON
  }
}

Hi

I think you will find your LED problem is caused by one too many semicolons on this line

  else(temperatureC > 25 ); {
//                        ^
    digitalWrite(control,LOW); // turn the MOSFET Switch ON

  }

The else condition ends with the semicolon so the digitalWrite(control, LOW); statement is always executed. Replacing it with this:-

  else(temperatureC > 25 ) {

    digitalWrite(control,LOW); // turn the MOSFET Switch ON

  }

should fix your issue with the LED flickering.

Hope this helps

Ian

@IanCrowe
Good catch, I missed it! :-[ K++

Not quite. Take the semicolon out but put the if back in i.e.

 else if(temperatureC > 25 ) {
    digitalWrite(control,LOW); // turn the MOSFET Switch ON
  }

Steve

Good news, the LED works, and when it gets below 25 I turn on and off over 26. But the readings from the TMP36 is going haywire, it goes from 29 celsius to 16 C. What exactly does the capacitor do and is it necessary to have it at the TMP36

slipstick:
Not quite. Take the semicolon out but put the if back in i.e.

 else if(temperatureC > 25 ) {

digitalWrite(control,LOW); // turn the MOSFET Switch ON
  }



Steve

Yep, My bad

I read the code from JCA34F's reply (#5) where I spotted the extra semicolon but copied it from the OP's original post and forgot to add the missing if.

Mea Culpa

Ian

Great team work!

Steve

slipstick:
Great team work!

Steve

Aye.

A phrase springs to mind involving the words "pygmies", "shoulders" and "giants" :slight_smile:

Ian