Using DHT11,Arduino Data to controll speed of fan and write to ThingSpeak

Greetings,

I need guidance and possible assistance with a Temperature monitoring and controll project that I am working on. I already have done one and working it uses a DHT11 sensor, arduino UNO, Ethernet shield and 1CH relay module.

Operation: The temperature data from the DHT is read and sent to ThingSpeak for monitoring and when temperature reaches and passes pre-defined temperature the relay is activated and fun turns on to ful speed, it turns off once it done.

Request: I want to now replace the relay with the Mosfet switch (already done) as in attached schematic and vary the speed of the fan in propotion to the temperature coming in via the arduino and maintain the temperature at set-point (Example 28 degrees Celsius). I want to keep using the DHT11 and also continue sending data to my ThingSpeak channel.

#include <SPI.h>
#include <Ethernet.h>
#include "ThingSpeak.h"

byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; //ETHERNET SHIELD MAC ADRESS
EthernetClient client;



#include "DHT.h"

#define DHTPIN 9

#define DHTTYPE DHT11
int relay_pin = 6;

DHT dht(DHTPIN, DHTTYPE);

unsigned long myChannelNumber = XXXXX ; //ThingSpeak Channel ID
const char * myWriteAPIKey = "XXXXXXXXXXXXX"; //PRIVATE WRITE KEY to ThingSpeak

void setup() {
  pinMode(relay_pin, OUTPUT);
  digitalWrite(relay_pin, HIGH);
  Serial.begin(9600);
  dht.begin();
  Ethernet.begin(mac);
  ThingSpeak.begin(client);
}

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  if (t > 35)
  {
    digitalWrite(relay_pin, HIGH);
    Serial.println (F("Fan is On"));
    delay(10);
  }
  else {
    digitalWrite(relay_pin, LOW);
    Serial.println (F("Fan is off"));
  }
  delay(1000);

  Serial.println("======================================");
  Serial.println("Temperature Monitoring");
  Serial.print("Temperature= ");
  Serial.print(t);
  Serial.println(" C");
  Serial.print("Humidity= ");
  Serial.print(h);
  Serial.println(" %");
  Serial.println();

  ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey);
  delay(15000);
  
  int x = ThingSpeak.writeField(myChannelNumber, 2, h, myWriteAPIKey);
  if (x == 200) {
    Serial.println("Channel update successful.");
  }
  else {
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  //delay(15000);
  ThingSpeak.setField(1, t);
  ThingSpeak.setField(2, h);
  delay(20000);
}

Basically I need the line of code to replace or modify this one..to controll the fan the way I want:

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  if (t > 35)
  {
    digitalWrite(relay_pin, HIGH);
    Serial.println (F("Fan is On"));
    delay(10);
  }
  else {
    digitalWrite(relay_pin, LOW);
    Serial.println (F("Fan is off"));
  }
  delay(1000);


What model is the MOSFET? Often, unsuitable models are sold for use with Arduino by ignorant vendors.

PaulRB:


What model is the MOSFET? Often, unsuitable models are sold for use with Arduino by ignorant vendors.

It an IFR540N, It is working but with the code not changed, it still functioning as a relay. I used the mosfet because of that I am now going to change the speed of fan instead of just switching it to full speed.

Unless you need to refer to something particular someone said in a previous reply, please use REPLY not Quote. When you do use Quote, just quote the smallest part, not the whole post, and not images.

Unfortunately, that's one of the unsuitable MOSFETs I was talking about. It might be ok for small currents, but your fan is not going to get the maximum available voltage using that MOSFET. The suitable type is called a "logic-level MOSFET".

The usual approach to controlling the fan like you want us to use the "PID library". You can install it from the library manager and take a look at some of the example code you get with the library.

Thank you very much, I will try the PID library and get back with feedback.

I have tried to use the PID library examples in my code as seen below, The problem is my project does the opposite of what I expect; When I set my setPoint to for example (30), the fan speed increases as temperature decreases below 30 and it stops when temperature reaches 30 and above

I am not sure whether it is my Tuning values or what?

// PIDController - Version: Latest
#include <PIDController.h>
#include <SPI.h>
#include <Ethernet.h>
#include "ThingSpeak.h"
#include "DHT.h"

#define DHTPIN 9
#define DHTTYPE DHT11
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; //ETHERNET SHIELD MAC ADRESS
EthernetClient client;


PIDController pid; // Create an instance of the PID controller class, called "pid"


int outputPin   = 3;    // The pin the digital output PMW is connected to


DHT dht(DHTPIN, DHTTYPE);

unsigned long myChannelNumber = xxxxx; //ThingSpeak Channel ID
const char * myWriteAPIKey = "xxxxxxxxxx"; //PRIVATE WRITE KEY to ThingSpeak

void setup () {
  Serial.begin(9600);   // Some methods require the Serial.begin() method to be called first
  pinMode(outputPin, OUTPUT);

  digitalWrite(outputPin, HIGH);
  dht.begin();
  Ethernet.begin(mac);
  ThingSpeak.begin(client);

  pid.begin();          // initialize the PID instance
  pid.setpoint(30);    // The "goal" the PID controller tries to "reach"
  pid.tune(3, 2, 1);    // Tune the PID, arguments: kP, kI, kD
  pid.limit(0, 255);    // Limit the PID output between 0 and 255, this is important to get rid of integral windup!
}

void loop () {
  float sensorValue = dht.readTemperature();  // Read the value from the sensor
  int output = pid.compute(sensorValue);    // Let the PID compute the value, returns the optimal output
  analogWrite(outputPin, output);           // Write the output to the output pin
  delay(30); // Delay for 30 ms

  Serial.println("======================================");
  Serial.println("Temperature Monitoring");
  Serial.print("Temperature= ");
  Serial.print(sensorValue);
  Serial.println(" C");
  Serial.println();

  ThingSpeak.writeField(myChannelNumber, 1, sensorValue, myWriteAPIKey);
  delay(1000);

  ThingSpeak.setField(1, sensorValue);

  delay(2000);
}

Try:

  int output = pid.compute(sensorValue);    // Let the PID compute the value, returns the optimal output
  if (sensorValue > 30) analogWrite(outputPin, output); else analogWrite(outputPin, 0);           // Write the output to the output pin

it stops when temperature reaches 30 and above

How far above? Above 30, the fan should start to come on again, but slowly, and faster as temp increases.

It stopped exactly at 30 degrees. I adjusted the code, instead of sending the pid computed value to output pin, I replaced with the actual temperature "sensorvalue". It is now propotional but as you early suggested the Mosfet at 50 degrees draws upto 1.1V , I will have to get the Logic level mosfet

void loop () {
  float sensorValue = dht.readTemperature();  // Read the value from the sensor
  int output = pid.compute(sensorValue);    // Let the PID compute the value, returns the optimal output
  analogWrite(outputPin, sensorValue);           // Write the output to the output pin
  delay(30); // Delay for 30 ms

Now you are not using the PID any more. If that works for you, ok, but you can delete all the code related to PID.