Hey guys,
Currently I´m working on a circuit board for controlling a fan depending on temperature (in my case for a car or bike etc.).
The design and code derives from a project I have done in the past, which is working flawlessly until now.
For the new design I just added a pin header to switch between different modes and changed to a different MOSFET, because the availability of the old one became terrible.
I´m using an Atmega328P with internal clock at 8MHz, programmed by an Arduino Uno as ISP, which I use for debugging (reading data from the Atmega and sending it to my PC via Serial).
So in theory everything should work fine if you started from a known, working design... but it doesn´t.
I encountered two problems:
The first problem is that the pulldown resistor (R3) is not working. By that I mean no matter how big the value is, the Atmega is not able to pull the pin of the bridge driver high to enable it.
I tried values from 4.7k to 47k, but no difference. So I started to look after errors in my code regarding the logic and inversed it. Still no difference. I´m really scratching my head now, because this part of the pcb/code was working in the old design.
The second problem that the MOSFETs and the capacitor (C9) are getting really really hot after a short time. I run an old, small fan of a 3D-printer hot end for testing. My old design is capable of around 20A and is running very cool under these conditions.
Sadly, I can´t find the code of the old Arduino project....
Here is my code (still under construction ) and I attached pictures of my schematic/PCB)
//Defining Pins
#define TPin PC0
#define TLowPin PC1
#define THighPin PC2
#define PWMMinPin PC3
#define PWMOutPin PD5
#define Mode1Pin PD6
#define Mode2Pin PD7
#define EnablePin PB0
//Constants
const int ntc_nom = 5000;
const int PWMMax = 255;
//Variables for temp measurement
float TRaw = 0;
float TU = 0;
float TR = 0;
float T = 0;
//Variables for parameters
int TLow = 0;
int THigh = 0;
int PWMMin = 0;
int PWMOut = 0;
int Mode1 = false;
int Mode2 = false;
int FanState = 0;
//This makes printing values much easier (for debugging)
void printValue(String text, float value, String text2 = "");
void setup() {
//Pin setup
pinMode(TPin, INPUT);
pinMode(TLowPin, INPUT);
pinMode(THighPin, INPUT);
pinMode(PWMMinPin, INPUT);
pinMode(PWMOutPin, OUTPUT);
pinMode(Mode1Pin, INPUT_PULLUP);
pinMode(Mode2Pin, INPUT_PULLUP);
pinMode(EnablePin, OUTPUT);
Serial.begin(9600);
//Reading the potentiometers to get the parameters and mapping them to fixed values
TLow = analogRead(TLowPin);
TLow = map(TLow, 0, 1023, 0, 120);
THigh = analogRead(THighPin);
THigh = map(THigh, 0, 1023, 0, 120);
PWMMin = analogRead(PWMMinPin);
PWMMin = map(PWMMin, 0, 1023, 0, 255);
//Turning off the mosfet driver at startup
digitalWrite(EnablePin, LOW);
}
void loop() {
//Reading the mode pins to choose the mode
Mode1 = digitalRead(Mode1Pin);
Mode2 = digitalRead(Mode2Pin);
//Mode 1
if(Mode1 == 0 && Mode2 == 1) {
//Getting the temperature
TRaw = analogRead(TPin);
TU = TRaw * 5 / 1023;
TR = ntc_nom * (5 / TU - 1);
T = -28.42 * log(TR) + 234.33;
//Turning fan on when TLow is reached and setting PWM signal according to the parameters
if(T >= TLow){
PWMOut = map(T, TLow, THigh, PWMMin, 255);
PWMOut = constrain(PWMOut, PWMMin, 255);
analogWrite(PWMOutPin, PWMOut);
digitalWrite(EnablePin, HIGH);
FanState = 1;
}
//If temperature drops below TLow the fan is switched off
else{
analogWrite(PWMOutPin, 0);
digitalWrite(EnablePin, LOW);
FanState = 0;
}
//Debugging
Serial.println("MODE 1");
printValue("T: ", T);
printValue("TLow: ", TLow);
printValue("THigh: ", THigh);
printValue("PWMMin: ", PWMMin);
printValue("Mode1: ", Mode1);
printValue("Mode2: ", Mode2);
printValue("PWMOut: ", PWMOut);
printValue("FanState: ", FanState);
Serial.println();
delay(1000);
}
//Mode 2
if(Mode1 == 1 && Mode2 == 0){
analogWrite(PWMOutPin, 200);
digitalWrite(EnablePin, HIGH);
FanState = 1;
//Debugging
Serial.println("MODE 2");
printValue("T: ", T);
printValue("TLow: ", TLow);
printValue("THigh: ", THigh);
printValue("PWMMin: ", PWMMin);
printValue("Mode1: ", Mode1);
printValue("Mode2: ", Mode2);
printValue("PWMOut: ", PWMOut);
printValue("FanState", FanState);
Serial.println();
delay(1000);
}
//Mode 3
if(Mode1 == 1 && Mode2 == 1){
//coming soon
}
}
void printValue(String text, float value, String text2 = "") {
Serial.print(text);
Serial.print(value);
Serial.println(text2);
}