Saturday, August 12, 2017

Thermostat based on Arduino

This time we are going to build a Thermostat based on Arduino, temperature sensor, and relay.
You can find it on GitHub

This Thermostat gives you the possibility to drive multiple devices in order to control temperature. In my case, I have installed two fans on my attic in order to cool it down in summer. There are no windows, so I had to force airflow. The first fan gets started when the temperature reaches 35 degrees, second over 45.

You can control any reasonable amount of units, it's all configurable. You have also access to basic statistics for past two weeks:
Runtime of whole system

ON time for each relay

Statistics for 14 days

Statistics for 7th day

Configuration

The whole configuration is stored in Config.h. You can change PINs controlling relays, buttons, input for reading temperature, thresholds or timings.

Hardware


Buttons

Two buttons are dedicated for menu navigation and the third one resets statistics. They are stored in EEPROM (Storage.cpp) once a day. Pins assigned to buttons can be found in Config.h

Configuring Relays

Let's assume that we would like to have 3 relays:

  • ID:0, PIN: 1, Temperature setpoint: 20
  • ID:1, PIN: 10, Temperature setpoint: 30
  • ID:2, PIN: 11, Temperature setpoint: 40
  • First, you have to make sure that PIN of your choice is not already taken. All pins can be found in Config.h, they are defined by variables starting with DIG_PIN.

    You have to edit Config.h and configure PINs, thresholds and amount of relays. Obviously, some properties already exist, so you have to just edit them.

    const static uint8_t DIG_PIN_RELAY_0 = 1;
    const static uint8_t DIG_PIN_RELAY_1 = 10;
    const static uint8_t DIG_PIN_RELAY_2 = 11;
    
    const static uint8_t RELAYS_AMOUNT = 3;
    
    const static int16_t RELAY_TEMP_SET_POINT_0 = 20;
    const static int16_t RELAY_TEMP_SET_POINT_1 = 30;
    const static int16_t RELAY_TEMP_SET_POINT_2 = 40;
    

    Now we have to set up relays and controller, this happens in RelayDriver.cpp


        initRelayHysteresisController(0, DIG_PIN_RELAY_0, RELAY_TEMP_SET_POINT_0);
        initRelayHysteresisController(1, DIG_PIN_RELAY_1, RELAY_TEMP_SET_POINT_1);
        initRelayHysteresisController(2, DIG_PIN_RELAY_2, RELAY_TEMP_SET_POINT_2);


    Choosing Controller

    There two controllers available Hysteresis and PID

    Hysteresis Controller

    It's the one chosen in the example above, it has few additional configurations:

    const static uint32_t RELAY_DELAY_AFTER_SWITCH_MS = 300000; // 5 minutes
    const static uint32_t RHC_RELAY_MIN_SWITCH_MS = 3600000;
    

    RELAY_DELAY_AFTER_SWITCH_MS gives wait time for switching next relay. Imagine that configuration from our example would start working in 40 degrees environment. This would result in enabling of all three relays at the same time. This could eventually lead to high power consumption - depending on what you are controlling, an electric engine, for example, consumes more power during start. In our case switching relays has following flow: the first relay goes, wait 5 minutes, second goes on, wait 5 minutes, third goes on.

    RHC_RELAY_MIN_SWITCH_MS defines hysteresis, it's the minimum frequency for particular relay to change its state. Once its on, it will remain on for at least this period of time, ignoring temperature changes. This is quite useful if you are controlling electric motors since each switch has a negative impact on live time.

    PID Controller

    This is an advanced topic. Implementing such controller is a simple task, finding right amplitude settings is a different story. 

    In order to use PID controller, you have to change initRelayHysteresisController(.....) to initRelayPiDController(....) and you need to find right settings for it. As usual, you will find them in Config.h 

    I've implemented simple simulator in Java so that it's possible to visualize the results. It can be found in the folder: pidsimulator.
    Below you can see simulations for two controllers PID a P. PID is not perfectly stable because I did not apply any sophisticated algorithm to find right values.

    On both plots required temperature is set to 30 (blue). Current temperature indicates read line. The relay has two states ON and OFF. When it's enabled temperature drops by 1.5, when it's disabled it rises by 0.5.

    Software Design

    Message Bus

    Different software modules have to communicate with each other, hopefully not both ways ;) 

    For example: 
    • statistics module has to know when the particular relay goes on and off,
    • pressing a button has to change display content and it also has to suspend services that would consume many CPU cycles, for example, temperature reading from the sensor,
    • after some time temperature reading has to be renewed, 
    • and so on...
    Every module is connected to Message Bus and can register for particular events, and can produce any events.

    For example, pressing Next button results in the following flow:
    Some components have some tasks that needs to be executed periodically. We could call their corresponding methods from the main loop since we have Message Bus it's only necessary to propagate right event:

    LIBS

    Following libs are required to compile Thermostat:
    • https://github.com/maciejmiklas/Thermostat
    • https://github.com/milesburton/Arduino-Temperature-Control-Library
    • https://github.com/maciejmiklas/ArdLog.git

    No comments:

    Post a Comment