element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
The Learning Circuit
  • Challenges & Projects
  • element14 presents
  • The Learning Circuit
  • More
  • Cancel
The Learning Circuit
Documents Temperature Activated Fan! -- The Learning Circuit 16
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join The Learning Circuit to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: tariq.ahmad
  • Date Created: 17 Jul 2018 7:07 PM Date Created
  • Last Updated Last Updated: 18 Jul 2018 7:24 AM
  • Views 1697 views
  • Likes 9 likes
  • Comments 1 comment
Related
Recommended

Temperature Activated Fan! -- The Learning Circuit 16

image

element14's The Ben Heck Show

Join Karen as she shares her enthusiasm for teaching STEM subjects, gives you what you need to know to get started on electronics projects, and more.

Back to The Ben Heck Show homepage image

The Learning Circuit
sudo Sergeant
See All Episodes

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

In a previous segment, DaftMike builds a thermometer using an Arduino and a thermistor. He expands on this in order to show you how to build a temperature controlled fan for your desk. It monitors the room temperature to see if it gets too hot.  When it gets hot, the Arduino is used to turn on a fan so that you can cool off!

 

Make Your Own!

Parts & Products:

Product Name

UK Part No

US Part No

Part Link

Arduino Micro228519463W3544Buy NowBuy Now
Thermistor211293510M5320Buy NowBuy Now
Pi-mote RF Control243343877Y4284Buy NowBuy Now
Breadboard247475468Y6468Buy NowBuy Now

 

 

image

DaftMike goes over the code from last time.  It reads a thermistor and prints the temperature to the serial monitor.  He moves all the equations he wrote earlier into four separate functions.  A function is like its own little program.  In Arduino code there is always a setup and a loop function.  The setup is at the start and only runs once.  The loop comes after that and repeats continuously.  Taking the equations from last time, he re-writes them so that they each have their own separate function. The main loop uses these functions to execute the code. Structuring your program this way makes it easier for you to reuse parts of your code. For example, if you wanted to add another temperature sensor then all you would need to do is call these functions again and pass them a different value.  It saves you time as well as some program memory.  For it to work, he’ll need to add a couple more functions.  One for turning the fan on and one for turning the fan off.  He’ll also need to set up the built-in LED as an output to stand in for the fan during testing.  When that is done, he tests the code.

 

/********************************************************
    element14.com/thelearningcircuit
    Episode 16: Temperature Activated Fan
 ********************************************************/


// Voltage divider equation parameters
const int Vin = 1023;
const long R2 = 100000;
// If you want to measure your specific R2 resistor, you can input it's actual value here for potentially better accuracy


// Steinhart–Hart equation parameters
const long R0 = 100000;     // 100K thermistor
const int B = 3974;         // Beta value (K)
const float T0 = 298.15;    // 25°C


// Analog Pin used to read the thermistor
const int thermistorPin = A0;


// Number of readings used for average
const int numReadings = 10;


const int testLED = 13;


// Temperature thresholds for the fan in Celcius (to use Farenheit change 'T' for 'Tf' in the 'Temperature test')
int highThreshold = 28;
int lowThreshold = 27;


bool fanStatus;


/**********************************************************************************************************************/
void setup() {
  Serial.begin(9600);             // opens serial port, 9600 baud


  for (int i = 2; i < 7; i++) {   // setup pins 2-6 as outputs and make them low
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }


  pinMode(testLED, OUTPUT);       // setup the on-board LED as an output
  fanOff();                       // start with the fan off


}


/**********************************************************************************************************************/
void loop() {
  // Call our functions to get the temperature readings
  float Vout = readSensorAverage(thermistorPin);
  float R1 = getR1Value(Vout);
  float T = getTemperature(R1);
  float Tf = celciusToFarenheit(T);




  // Print the temperature in Celcius and Farenheit to the serial monitor
  Serial.print(T); Serial.print("C | ");   Serial.print(Tf); Serial.println("F");




  // Temperature test for turning the fan on/off (change 'T' to 'Tf to use Farenheit)
  if (T > highThreshold && !fanStatus) {
    fanOn();
  }
  else if (T < lowThreshold && fanStatus) {
    fanOff();
  }




}


/**********************************************************************************************************************/
float readSensorAverage(int analogPin) {    // take multiple readings from our thermistor and return the average value
  float Vout;
  int readings[numReadings];                // array to store readings
  for (int i = 0; i < numReadings; i++) {
    readings[i] = analogRead(analogPin);    // read from the analogPin into the array
    Vout += readings[i];                    // sum the readings so far
    delay(10);                              // allow the ADC to settle between measurements
  }
  Vout /= numReadings;                      // divide the running total by the number of readings to get an average
  return Vout;


}


float getR1Value(float Vout) {              // Voltage Divider equation to calculate R1
  float R1 = (R2 * (Vin - Vout)) / Vout;
  return R1;
}


float getTemperature(float R1) {            // Steinhart–Hart equation β(Beta) parameter version
  float T = (1.0 / ((1.0 / T0) + (log((R1 / R0)) / B))) - 273.15;
  return T;   // T is in Celcius
}


float celciusToFarenheit(float Tc) {        // Celcius to Farenheit conversion
  float Tf = (Tc * 9.0) / 5.0 + 32;
  return Tf;


}


void fanOn() {
  // this sequence turns our remote controlled socket on
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(100);
  digitalWrite(6, HIGH);
  delay(250);
  digitalWrite(6, LOW);


  Serial.println("turning the fan on...");
  digitalWrite(testLED, HIGH);
  fanStatus = HIGH;


  turnOffRemotePins ();


  delay(1000);


}


void fanOff() {
  // this sequence turns our remote controlled socket off
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  delay(100);
  digitalWrite(6, HIGH);
  delay(250);
  digitalWrite(6, LOW);


  Serial.println("turning the fan off...");
  digitalWrite(testLED, LOW);
  fanStatus = LOW;


  turnOffRemotePins ();


  delay(1000);


}


void turnOffRemotePins () {         // save power by 'turning off' the remote pins
  for (int i = 2; i < 7; i++) {
    digitalWrite(i, LOW);
  }
}




/*********************************************************************************************************************
   ENER314 RF-Transmitter Board datasheet:
   https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf
   
   Steinhart–Hart equation β(Beta)
   https://en.wikipedia.org/wiki/Thermistor#B_or_%CE%B2_parameter_equation
*********************************************************************************************************************/

 

After testing the code, he notice that when the temperature gets close to the trigger point it flashes.  This is not what he wants.  The way to fix this is to add what’s called hysteresis.  Hysteresis is the dependence of the state of a system on its history. It’s when the output of your system, in this case the fan or the LED for now, lags behind the state of your input, which in this case is the room temperature. Returning to the code, instead of a single trigger point, he’s set some thresholds to check. There’s a high threshold and a low threshold. As long as one number is lower than the other you can use code to check if the temperature is higher than the high threshold and switch the fan on for true.  You can then use a conditional statement to perform a check to see if the temperature is lower than the low threshold to turn the fan off.  He proceeds to test the new code.  Once the code is fixed, you can replace your test LED with a real fan. Arduino pins can only supply a small amount of current, around 20 milliamps.  This is good for LED but you’re going to need additional circuitry for something more powerful like a fan.  You could use a relay.  A relay is a type of switch that’s controlled by a magnetic coil.  In this case, DaftMike wants to use an existing fan that plugs into the mains so he uses a remote controlled outlet, originally designed for Raspberry Pi.

  • threshold
  • temperature reading
  • ardbeginner
  • magnetic coil
  • stem_projects
  • tlc
  • ground pins
  • loop
  • voltage divider
  • trigger point
  • arduino_vcp
  • arduino micro
  • arduino ide
  • power rail
  • arduino_tutorials
  • breadboard
  • led
  • thermistor
  • temperature sensor
  • gnd
  • relay
  • thelearningcircuit
  • e14presents_daftmike
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
  • ralgat
    ralgat over 5 years ago

    I like the concept, but I am too lighty skilled to follow what the hardware is to put together.  It seems somewhat obscured with more explain, while the if then else explain was detailed.  I guess I ned more Arduino schooling so presto whammo will be enough.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube