Version 10

    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

    The Learning Circuit
    sudo Sergeant
    See All Episodes

     

    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

     

     

    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.