Code cooling fan

can someone write me a code to run a fan as the temperature increases?

i have the components of the arduino uno R3 starter kit

Welcome to the forum

As your topic does not relate directly to the use of IDE 1.x it has been moved to the Programming category of the forum. Please take care when creating topics

As to your question, the forum is not a code writing service unless you are prepared to pay for it. Are you prepared to pay ?

Use the InterNet, @peppe_09

#include <DHT.h>

const int tempPin = 2; // DIO
const int fanPin = 3; // PWM

DHT dht = DHT(tempPin, DHT22); 

void setup() {
  dht.begin();
  pinMode(fanPin , OUTPUT);  
}

void loop() {
  float temp = dht.readTemperature(); // Celsius
  if (isnan(temp)) return;

  int fanSpeed = map(temp, 20, 40, 0, 255);
  fanSpeed = constrain(fanSpeed, 0, 255);
  analogWrite(fanPin, fanSpeed);
}

Hi @peppe_09
welcome to the arduino-forum.

Yes I can write such a code.
But only if you would tell what kind of temperature-sensor what kind of voltage and power-consumption your fan has.

Though you did not even use the word "please" I will not write it for you.

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.