Introduction: Arduino BLE Thermostat With Mobile App

About: Part software developer, part maker.

A thermostat is the single most beneficial device that one can add to their home so it automatically maintains a steady and comfortable temperature in the home.

There are many types and variants of this simple device but in a nutshell, a thermostat is just a temperature-operated switch that controls a heater or a cooler depending on the setup and application.

In my example, I made a thermostat that can be attached to a central heating system, like a pellet burner, that can be then controlled via Bluetooth Low Energy from a mobile phone using a custom-built application.

The entire project is based on the XIAO nRF52840 BLE board from Seeed Studio and uses a DHT22 sensor to measure the temperature and humidity of the room.

Supplies

Step 1: Connecting the Circuit

To make a thermostat we need two basic things: a temperature sensor and a relay.

For the temperature measurement in my case, I used the DHT22 sensor which can measure from -40°C up to 80°C. The sensor reading is interfaced using a single pin and I have that pin connected to D1 on the XIAO BLE board.

The DHT22 sensor needs to be powered from 5V for better operation so its power pins are also connected to the 5v output on the microcontroller.

The relay is also powered from the 5v output on the XIAO board and to control it, I used the D2 pin which I later set as an output so I can turn on the relay by pulling the output LOW in the code.

A final SPDT switch is wired from the 3.3V output over to D3 so I can basically provide either a LOW or HIGH signal to D3 so I can adjust the operation mode later on with the code. This switch is entirely optional and can be omitted if you want the thermostat to be controlled only by Bluetooth, without any physical input.

Step 2: Programming the XIAO BLE Board

The XIAO BLE board can be programmed in many ways but I opted for using the Arduino IDE as that was most familiar to me.

To add the board to the Arduino IDE, we need to add the following URL inside the Aditional Boards Manager in the Arduino IDE preferences:

https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json

Once the board is added, we can now install it from the Boards Manager by searching for Seeed nRF52 Boards and installing the package with the same name. You can find the exact steps in the board documentation.

Step 3: Arduino Code for the Thermostat

Now that you can program the XIAO board via the Arduino IDE, you can download the thermostat code from the GitHub repo below:

https://github.com/bkolicoski/arduino-ble-thermostat

I won't go into too many details on the code, but in general, we first set up the Bluetooth service by using the ArduinoBLE library and defining all of its characteristics.

A characteristic is basically a feature that is connected to a specific functionality of the thermostat such as setting and getting the temperature, reading the state of the output relay, etc.

Each time a value changes, we then write to these characteristics, and the app is then notified about that change.

The most important part of the code is the section that reads the temperature from the DHT22 sensor, compares it with the current set temperature of the thermostat and if the temperature is lower than the set, it turns on the relay.

Once the temperature is reached the relay will be turned off until it's low enough for the cycle to repeat.

Since we do not want to turn on and off the heating too often, a hysteresis of 2°C is also added to the temperature range so the heating will be turned on once the temperature is lower by more than two degrees than the set temperature and will be turned off once it reaches two degrees above the set temperature.

Step 4: Making the Mobile App

Since I'm not a mobile app developer, I searched online for the simplest way of making an Android app. One of the results that poped out was the MIT App Inventor platform that looked simple enough and there were plenty of examples and articles on it so I choose that.

The app first scans for available devices nearby, and we can then select and connect to a specific device from the list of found devices.

Once a connection is established, the app displays the info about the current state of the thermostat indicating if the thermostat is on or off, the current set temperature, mode of operation, humidity, and the status of the relay output.

There are several code blocks in the app, but the most important ones are the block that sets the device for continuous reading of incoming data from the microcontroller and the block that actually receives that data and displays it based on the characteristic that sent it.

To play around with the app and modify it, you can download the .aia file from the GitHub repo and then import that file inside App Inventor so you can then create your own version of it.

Step 5: Principle of Operation

The thermostat has two modes of operation: Auto and Manual. These modes determine how the thermostat is turned on and the rest of the functionality is the same as previously explained.

In the default Auto mode, the thermostat is turned on by the application. Since the thermostat can follow the set temperature there is no need to turn it on or off each day but you can turn it on once and it will then track the set temperature for as long as needed.

If you then do not want to have the heating on, you can turn off the thermostat from the app so the thermostat will then enter the Off mode, where it will still report the temperature and humidity of the room but it will not turn on the output.

In this state, it is quite possible that somebody will want to turn on the heating and it might not have a phone on hand (yes, weird I know 😅) so they might want to turn it on Manually. That is the purpose of the extra switch that is connected to D3.

Once that switch is flipped, the thermostat now enters manual override mode where it continuously tracks the set temperature but it will stay on no matter if it is turned off from the app.

These two modes are indicated by the onboard RGB LED on the controller. Once the thermostat is turned on by the app, the red LED will be turned on and once the manual override mode is activated, the blue LED is turned on.

If the thermostat is already on by the app, and the manual override is turned on, both the blue and red LEDs will light up indicating that the thermostat is now turned on by the two methods and even if one of them is turned off, the thermostat will stay on until both of them are turned off.

Step 6: Next Steps

In the current state, the thermostat is really simple but it still packs a nice set of features.

The very first thing that it needs right now is some kind of an enclosure and I plan to make one for the upcoming winter season where I plan to use it with my central heating pellet boiler.

Additionally, as I mentioned in the video where I explain the thermostat, the XIAO BLE board has the ability to charge and work off a lithium cell so you can add that as a backup power source in case there is a power outage.

In terms of software, a lot more things can be added to the thermostat so it has schedules, inputs from other sensors, control of it via Home Assistant, and a lot more so for that I will ask you to join in in further development of it.

Feel free to fork out the repo and add features to it as you need, so we can make this a great open-source thermostat for people to use in their homes.

If you liked this project, I'll ask you to check out my other Instructables as well as my website and maybe even consider subscribing to my channel on YouTube.

Thank you for reading!

Microcontroller Contest

Participated in the
Microcontroller Contest