// Import the necessary libraries
#include <DHT.h>
#include <Wire.h>
// Define the DHT11 sensor pin
#define DHTPIN 2
// Define the DHT11 sensor type
#define DHTTYPE DHT11
// Create a DHT object
DHT dht(DHTPIN, DHTTYPE);
// Define the MOSFET 2N7000 pin
#define MOSFETPIN 3
// Initialize the variables
float temperature;
int fanSpeed;
void setup() {
// Initialize the serial port
Serial.begin(9600);
// Initialize the DHT11 sensor
dht.begin();
// Set the MOSFET 2N7000 to LOW
digitalWrite(MOSFETPIN, LOW);
}
void loop() {
// Read the temperature from the DHT11 sensor
temperature = dht.readTemperature();
// Calculate the fan speed
fanSpeed = map(temperature, 20, 80, 0, 100);
// Set the MOSFET 2N7000 to the corresponding fan speed
digitalWrite(MOSFETPIN, fanSpeed);
// Print the temperature and fan speed to the serial port
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" degrees Celsius");
Serial.print(" Fan speed: ");
Serial.print(fanSpeed);
Serial.println("%");
// Delay for 1 second
delay(1000);
}