I am trying to work my dc fan when temp rises with pwm. When I connect it with arduino's power supply(5V), it works with analogWrite(fan, tempMin, tempMax, 188, 255). But when I try to connect it with a 12V power supply, transistor and diod, it doesnt move a inch.
This is my code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3, POSITIVE);
int tempPin = A1;
int fan = 3;
int temp;
int tempMin = 30;
int tempMax = 60;
int fanSpeed;
int fanLCD;
void setup() {
pinMode(fan, OUTPUT);
pinMode(tempPin, INPUT);
Serial.begin(9600);
lcd.begin(16,2);
}
void loop() {
temp = readTemp();
if(temp < tempMin) {
fanSpeed = 0;
analogWrite(fan, 0);
}
if((temp >= tempMin) && (temp <= tempMax)) {
fanSpeed = map(temp, tempMin, tempMax, 188, 255);
fanLCD = map(temp, tempMin, tempMax, 60, 100);
analogWrite(fan, fanSpeed);
}
Serial.println(analogRead(6));
lcd.print("TEMP: ");
lcd.print(temp);
lcd.print("C ");
lcd.setCursor(0,1);
lcd.print("FANS: ");
lcd.print(fanLCD);
lcd.print("%");
delay(200);
lcd.clear();
}
int readTemp() {
temp = analogRead(tempPin);
return temp * 0.48828125;
}
Note: There is also a lcd to show status which is connected to arduino's power supply which is from pc's usb connection.