Hi all,
This is my first project, please be gentle.
Aim: To control a greenhouse fan to be ON at ambient temperature (Ta) >= 25degC, and OFF at Ta <= 24.9 degC.
Materials: Arduino Uno, DS18B20 sensor, resistor, Mofset, 12V/380mA DC fan.
So far, I have had success poaching and modifying other code. In short, it works. BUT I think the code is messy and I am missing something in the mapping of the sensor value to control the fan speed.
Specifically, my serial fanspeed reports are >100%, and the full mA for the fan is not reached.
I can make the fan turn ON at sensor temp >25degC, and off below that, but I think my 'span' for the fan speed is wrong...
Any help would be appreciated.
Sincere thanks
42adam
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#define D1 3 // fan assigning the to arduino pin
#define ONE_WIRE_BUS 7 // thermometer
int fanPin = 3;
// Setup a oneWire instance to communicate with OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
pinMode(fanPin, OUTPUT); // sets the pins as outputs:
sensors.begin(); // Start up the library
}
float readSensorTemp() {
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temperature: ");
Serial.println(sensors.getTempCByIndex(0));
return sensors.getTempCByIndex(0);
}
void controlFanSpeed (int fanSpeedPercent) {
Serial.print("Fan Speed: ");
Serial.print(fanSpeedPercent);
Serial.println("%");
analogWrite(fanPin, fanSpeedPercent); // set the fan speed
}
void loop() {
float sensorTemp = readSensorTemp(); // Request sensor value
// Map (change) the sensor reading of <=24 to >=25 to a value between 0 and 255
int fanSpeedPercent = map(sensorTemp, 24, 25, 0, 255);
controlFanSpeed (fanSpeedPercent); // Update fan speed
delay(1000);
}