Save more than 10%

Buy an AirQ 305 wireless control board in bundle with an AirQ ShielD and you'll save more than 10%
View product

Save more than 10%

Save up to 10%

Buy an AirQ 310 wireless control board, with 6 relays and battery backup circuit, and you'll get a free Arduino AirQ ShielD
View product

Save up to 10%

Building a wireless thermostat using Arduino/2

In first part of this tutorial we started developing a wireless thermostat using Arduino Uno and one AirQ 305 control board. In that tutorial we designed a really simple thermostat that doesn’t interact with the user: the desired temperature is fully hardcoded inside the code and user can’t see the ambient temperature. In the second part of this tutorial we’ll push our thermostat to a more interesting level, adding a LCD display and two switches that allow to increase/decrease the wanted temperature. Moreover, we’ll remove the tmp36 temperature sensor attached to Arduino and we’ll use a more practical AirQ 100 wireless temperature sensor.

Bill of material

For this part of the tutorial we need the following material:

  • One Arduino Uno.
  • One AirQ ShielD for Arduino Uno.
  • An AirQ 305 wireless relay board.
  • An AirQ 100 low-cost wireless temperature sensor: different from the fist part of tutorial, we’ll use a wireless temperature sensor since it’s more practical to have the sensor separated from the Arduino. With a wireless sensor we are totally free to place it wherever we need. However, if you need to keep the budget low, you can simply rearrange this tutorial to use the tmp36 sensor.
  • A character 16×2 LCD: we’ll use a cheap 16×2 LCD by Powertip (model PC1602F, which uses a compatible display driver for the Hitachi HD44780 display chip) but you are totally free to use every type of display.
  • Two SPST switches.

The hardware setup

The hardware setup is quite simple also for this part of the tutorial. The following diagram, made with Fritzing, clearly shows wiring. thermostat-2_bbThis first step is connecting the LCD display. Since LCD consumes just 3mA when used without backlight, we can safely power it using Arduino. Next, we connect Contrast and R/W pins to GND since it’s not useful for our project to control them by digital pins. Next, we connect the R/S and Enable pins to Arduino pin 5 and 4 and finally we connect the databus lines D3..D7 to Arduino pins 3..0 (we use 4-bit mode to interface LCD, since update speed is not demanding for our project). The final step is connecting the switches. To reduce components, we connect them to ground and we’ll use internal Arduino pull-up resistors. In this way, when the digital pin is HIGH it means that the switch is unpressed; when it’s LOW the switch is pressed. We connect switches to pins 7 and 6. Compared to the first part of this tutorial, we removed the LED that signals if RELAY1 is ON or OFF: we’ll show this info directly on LCD.

The code

Here again, the code to drive our wireless thermostat is really simple.

The first fifteen lines of code are all about declaring global variables and objects we use in the sketch. To drive LCD display we use excellent LiquidCrystal library and we create the lcd object passing as argument the digital pins where the LCD lines are connected to. Since this time we use two AirQ Networks wireless devices (one AirQ 305 relay board and one AirQ 100 wireless temperature sensor), we instantiate the sNET object passing the number 2 to the constructor: in this way, sNET library will create sufficient memory room to arrange messages coming from the two devices. lcdUpdate() is just a convenient function to update the two LCD lines without flooding our sketch with repeating lines of code. As the name suggests, float2char() is a function that converts a float to a string: since Arduino sprintf() function doesn’t support float conversion, we need a way to convert temperature to string in order to send it to the LCD. float2char() addresses this, giving a precision of one decimal (it simply truncates the float; eg: 18.24 -> 18.2). The setup() function is responsible to setup our sketch according the hardware configuration.  We configure the digital pins attached to switches as INPUT pins, enabling Arduino internal pull-up resistors (this allows us to save two external pull-up resistors). Finally, we wait until both AirQ 305 and AirQ 100 are connected to AirQ ShielD. The loop() function is the core part of our wireless thermostat. Thanks to the sNET library we can create a wireless device with just thirteen lines of code! Isn’t this amazing? 😀 The code is really self-explaining. If one of upBTN or downBTN buttons is pressed, we increase/decrease the desired ambient temperature. When the current temperature is lower, we turn RELAY1 ON; otherwise we turn it OFF.   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

Be Sociable, Share!

Posted on by admin in Arduino, Tutorials 2 Comments

2 Responses to Building a wireless thermostat using Arduino/2

  1. Pingback: Building a wireless thermostat using Arduino/1 | AirQ Networks Development Blog

  2. Pingback: Building a wireless thermostat using Arduino/3 | AirQ Networks Development Blog

Add a Comment