Quantcast
Channel: AirQ Networks Development Blog » arduino relay
Viewing all articles
Browse latest Browse all 9

Building a wireless thermostat using Arduino/1

$
0
0

Starting from this post, we’ll publish a series of tutorials that show how it’s simple to build custom and complex wireless solutions using Arduino and AirQ Networks products.

heating-systemThe first tutorial we publish is maybe the most simple but it’s the bare bone tutorial of all DIY and makers: a Wi-Fi thermostat. Unless you are one of those lucky people that live in that areas of the earth where there are 25°C during all the year, a thermostat is useful both during winter and summer. During winter a thermostat can control the heating system as well as conditioning system during the summer. But often devices are placed far from where we need to control them. And this is especially true for boilers that are usually placed inside specific rooms (see left picture).

So wireless is a crucial point to design a flexible and adaptive thermostat. But we want to go further. In the Internet of Things (IoT) era, we want to design a thermostat that is smart and that we can control from the web wherever we are.

Bill of material

This tutorial is divided in three parts. In the first part, we’ll design a really simple but functional thermostat able to control a remote boiler according a hardcoded temperature. To do this step we’ll need:

For the second part of this tutorial we’ll need:

For the third part we’ll need:

  • an Arduino Ethernet shield;

In addition to these components, a breadboard and some patches are required. However, you are totally free to arrange the project as you like.

First step

The first step of this tutorial is the simplest one, and it can be arranged in less then ten minutes. We’ll use a temperature sensor to detect the ambient temperature and when this temperature goes under a given threshold we’ll turn on the RELAY1 of an AirQ 305 control board. When this happen, a led connected to Arduino will turn on: this is really useful to understand that the boiler is turned on. In this tutorial we assume that the boiler is connected to the RELAY1, but in the real word this connection depends on the specific heating system.

The hardware setup

The hardware setup for this step is quite simple. The following diagram, made with Fritzing, clearly shows wiring.

thermostat-1_bb

Let’s describe it.

The first step is mounting the AirQ ShielD on Arduino. This is a trivial task, but if you are new to Arduino world we suggest to take a look at our survival guide.

Next, we have to interface the temperature sensor. The TMP36 is a temperature sensor by Analog Devices. It’s quite popular in DIY world, it’s inexpensive and it has a good measuring precision without calibration. We worked a lot in the past with this transducer, and we know that a 0.5°C precision can be achieved with a very little effort. There is only a trick to know if you want to take the best from it: it’s important to add a bypass capacitor between +3V3 and GND really close to the sensor. To be honest, it’s not a secret. It’s just what ADI says in its datasheet :-) . But there are a lot of tutorials around in the web where this requirement is omitted. So, if you are not interested in really high precision, you can avoid to put that capacitor. Otherwise it’s important. For the sake of completeness, the capacitor should be a ceramic SMD capacitor placed on the same PCB layer of the TMP36 lead connection. However, the type of application we are doing doesn’t demand this high precision.

Next, looking at the above draw, we can see another wire between AREF pin and +3V3 pin. We added that wire because we’ll use external voltage as voltage reference for the analog input. For several reasons we won’t address here, it’s not suitable use the default voltage reference to compute A/D conversion of TMP36 output. So, to achieve the best precision, it’s better to use external reference and connect AREF pin to the +3V3 output.

Finally, we added a led to signal that remote RELAY is ON (this tutorial assumes that the AirQ 305 control board is far away from Arduino). The resistor in the above draws is just a current limiting resistor that avoids the led destruction.

The code

The code for this part of the tutorial is straightforward. But we’ll spend some minutes to comment it.

#include <SoftwareSerial.h>
#include <sNET.h>

// This variabile sets the desired ambient temperature */
float desiredTemperature = 21.5;

//TMP36 Pin Variables
int sensorPin = 0;   //the analog pin the TMP36's Vout (sense) pin is connected to
                     //the resolution is 10 mV / degree centigrade with a
                     //500 mV offset to allow for negative temperatures

sNET snet(1);
AIRQ305 *board;

void setup()
{
  Serial.begin(19200);  //Start the serial connection with the computer
                         //to view the result open the serial monitor
  snet.begin();
  analogReference(EXTERNAL);
  pinMode(4, OUTPUT);
}

float getTemperature() {
  float voltage = 0.0;

  // We average the temperature to obtain a more stable value
  for(int i=0; i<10; i++)
    voltage += analogRead(sensorPin) * (3.3/1024.0);
  voltage /= 10.0; 

  return (voltage - 0.5) * 100;
} 

void loop()
{
  /* sNET::processMessages() is responsibile to process messages coming from 
     AirQ Networks devices and to update corresponding device object (in this
     example, the AIRQ305 object). User code should call this method as soon
     as possible and continuosly */
  snet.processMessages();

  float tempC = getTemperature();
  // Let's give some feedback to the console
  Serial.print(tempC); Serial.println(" degrees C");

  // Now convert to Fahrenheight
  float tempF = tempC * (9.0 / 5.0) + 32.0;
  Serial.print(tempF); Serial.println(" degrees F");

  if((board = (AIRQ305*)snet.getDeviceForDeviceID(5,0,1,3)) != 0) {
    if(tempC > desiredTemperature + 0.5) /* Temperature raised too much: turn off bolier */
      board->setRELAY1(OFF);
    else if(tempC < desiredTemperature - 0.5) /* Temperature decreased too much: turn on bolier */
      board->setRELAY1(ON, false);

    if(board->getRELAY1())
      digitalWrite(4, HIGH);
    else
      digitalWrite(4, LOW);
  }

  delay(2000);
}

 

The first 22 lines of code are related to the sketch setup and are almost self-explaining. desiredTemperature is the variable where we set wanted ambient temperature. In the next part of this tutorial we’ll allow the user to setup the temperature using switches but until then this is the way to do it. sensorPin contains the Arduino analog pin where the output of TMP36 is attached. In setup() routine we set the external reference for A/D to EXTERNAL AREF pin, and we configure the pin 4 as output: that pin is used to blink led on/off. getTemperature() function is where we do A/D conversion and compute the temperature provided by TMP36. According to its datasheet, TMP36 has an offset of 500mV to allow negative temperatures without the need for negative voltages. Moreover, the sensors gives an output of 10mV for each °C.  The formula inside getTemperature() function address this.

The rest of the sketch is all about temperature regulation. If ambient temperature is lower than (desiredTemperature – 0.5) °C, it turns the RELAY1 ON (we assume that boiler is connected to RELAY1). Otherwise, we turn it OFF. Finally, when RELAY1 is ON, we turn on the LED attached to digital pin 4.

The following video shows how thermostat works :-)

 

The source code of this sketch is inside the example subdirectory of sNET repository, or it can directly downloaded using this link.


Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images