featured

Arduino Bluetooth Temperature & Humidity Sensor

Bluetooth devices are widely used in many consumers products, and many Arduino-compatible projects that were funded on Kickstarter are using Bluetooth. One example is this Arduino-compatible board with Bluetooth onboard. So why not use this technology for home automation ?

Bluetooth is fast, low-power, and like WiFi you can communicate with such devices straight from a computer or a mobile device because you usually have Bluetooth integrated in your device. The other cool thing is that with this project, you will be able to change the sketch running on your Arduino via Bluetooth, without having to plug any cables!

In this project, you will learn how to connect a Bluetooth module to Arduino, transmit measurements from a temperature & humidity sensor to your computer, and display the data in a nice Python interface. Let’s start!

Hardware requirements

The base of this project is an Arduino Uno board, along with the Adafruit Bluefruit Bluetooth board. You will also need a DHT 11 temperature & humidity sensor (but you could plug any sensor of your choice), a 4.7K Ohm resistor, a 10 uF capacitor, and a breadboard and some jumper wires.

Software requirement

You will simply need the Arduino IDE installed for this tutorial, along with the DHT sensor library and the Python PySerial module.

Hardware configuration

The hardware configuration consists in two parts: connecting the Bluetooth module to the Arduino board, and then connecting the DHT sensor. It starts by plugging the Bluetooth module on the breadboard:

IMG_7819

You then need to connect the power for the Bluetooth module: the 5V coming from the Arduino board, and the Ground pin:

IMG_7820

Now, we have to make the connections from the Bluetooth board to the Arduino so that both can communicate via the Serial connection. Connect the Bluetooth TX pin to the Arduino RX (pin 0), and the RX pin to the Arduino TX (pin 1). You also have to connect the DTR pin of the Bluetooth board to the Arduino Reset pin (which is next to the 3.3V pin), via the 10uF capacitor (the negative side of the capacitor is marked with a gray line, and should be on the side of the Arduino Reset pin). The picture below shows the Bluetooth module fully connected:

IMG_7822

Finally, you need to plug the DHT temperature sensor to your project. Pin number 1 goes to the Arduino 5V, pin number 2 to Arduino pin 7, and pin number 4 to Arduino Ground. Finally, place the 4.7K resistor between pin number 1 and 2 of the DHT sensor. This is what you should end up with:

IMG_7824

To help you out, the following picture summarises all the connections:

Screen Shot 2014-02-11 at 10.03.36

Testing the Bluetooth module

We’ll now test the Bluetooth module to see if everything is connected correctly. You need to pair the Bluetooth module with your computer first. It depends on your OS, but you will usually have a “Bluetooth preferences” menu to search for new Bluetooth devices:

Screen Shot 2014-01-28 at 09.51.53

Once the device is paired with your computer, you can reopen the Arduino IDE and test if the Bluetooth connection is working. In Tools>Serial Port, you should have new choices for your Bluetooth device. Choose the second one:

Screen Shot 2014-01-28 at 09.57.27

You can now work with the Arduino IDE as if the Arduino board was directly connected to your computer: you can plug your Arduino board to an external source of power like a battery, and use the Bluetooth connection to upload sketches. To try it out, just load the “Blink” sketch, and click on upload: after a while the sketch should be uploaded (it takes longer than with a USB cable) and the onboard LED of the Arduino Uno should blink.

Writing the Arduino sketch

We now need to write the code for the Arduino, so it measures the temperature & humidity when it receives a given command on the Serial port. This command will later be sent by your computer, but for now we’ll just make a simple test to make sure the Arduino part is working.

The core of the sketch is to make the Arduino answer with the temperature & humidity measurement on the Serial port when a given character is received. I chose the character “m” for “measurement” to make the Arduino send the measurements over the Serial port. This is the code that does exactly that:

This is the complete sketch for this part:

Now upload the sketch (via Bluetooth of course!), open the serial monitor, and type in “m” and click send. This is what you should see on the Serial port:

serial_comm_screenshot

This means whenever the Arduino will receive the character “m”, it is going to return to correct measurements (in this case the temperature was at 20 degrees Celsius and the humidity was at 37 %).

Building the interface

Finally, we need to build an application running on your computer to send the order to get new measurements from the Arduino board, retrieve the data, and display it on your screen. I chose Python for this interface but it’s quite easy to interface with the Serial port with PySerial, and it is also easy to build an interface with Tkinter which is installed by default with Python.

I won’t detail every piece of the code here because it’s way too long, but you can of course find the complete code in the GitHub repository for this project.

The Python code starts by initialising the Serial connection:

Then, the core of the Python script is the measure() function that is continuously executed every second:

Then, the rest of the script simply displays this data on a simple Tkinter window. You can get the complete code from the GitHub repository for this project. Finally, type “python sensor_gui.py” in a terminal. This is what you should get:

sw_screenshot

Congratulations, you just built a Bluetooth temperature & humidity sensor ! This video goes over the main steps of the project:

This is the list of the parts that were used in this project:

Of course, you can use the code found in this project to interface other sensors to your computer via Bluetooth, for example ambient light sensors or motion sensors. Each new Bluetooth module that you add will appear as a new Serial Port on your computer, so you can perfectly have several of these Bluetooth-based sensors in your home and build a whole home automation system from it!

  • Camillo

    Hi! Great! Very interesting!
    Can You power it from batteries?
    What is the max range You tested?
    Can You connect to the sensor also from Raspberry Pi or mobile phones?
    Thank you!

    • http://www.marcoschwartz.com Marco Schwartz

      Hello Camillo,

      Thanks! It definitely can be powered by batteries, however I would suggest then building a striped-down version of Arduino so it doesn’t deplete the batteries too quickly (like in http://www.openhomeautomation.net/arduino-battery/). For the range, it worked without problem in all my flat (about 60 sqm), and I also tested it in a 2-floors house, where it only worked in a given floor because there was concrete in the middle of the house.

      Sure, it could also be connected to a Raspberry Pi (which can run Python software out of the box) but you will need an extra Bluetooth dongle (about $4). You can also used it with a mobile phone, however not iOS devices because of some limitations of the Bluetooth software on these devices.

  • http://www.epluse.com/en/products/humidity-instruments/humidity-sensor/ humidity sensors

    A really intresting and cool work. Keep it up man :)
    Always learn new things from you. thx

  • Sathya

    Great Work Marco.. interesting.. how can we connect to the mobile phones using Bluetooth and display the sensor data in phones. ?

    • http://www.marcoschwartz.com Marco Schwartz

      Hello Sathya, good point. It is possible with Android, I’ll have an article on that soon!