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.
You then need to connect the power for the Bluetooth module: the 5V coming from the Arduino board, and the Ground pin.
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).
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.
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.
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.
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:
byte c = Serial.read (); // If a measurement is required, measure data and send it back if (c == 'm'){ int h = (int)dht.readHumidity(); int t = (int)dht.readTemperature(); // Send data (temperature,humidity) Serial.println(String(t) + "," + String(h)); }
This is the complete sketch for this part:
// Bluetooth temperature sensor #include "DHT.h" // Pin for the DHT11 sensor #define DHTPIN 7 #define DHTTYPE DHT11 // Create instance for the DHT11 sensor DHT dht(DHTPIN, DHTTYPE); // Setup void setup(void) { dht.begin(); Serial.begin(115200); } void loop(void) { // Get command if (Serial.available()) { // Read command byte c = Serial.read (); // If a measurement is required, measure data and send it back if (c == 'm'){ int h = (int)dht.readHumidity(); int t = (int)dht.readTemperature(); // Send data (temperature,humidity) Serial.println(String(t) + "," + String(h)); } } }
Now upload the sketch (via Bluetooth of course!), open the serial monitor, and type in “m” and click send.
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:
serial_speed = 115200 serial_port = '/dev/cu.AdafruitEZ-Link06d5-SPP' ser = serial.Serial(serial_port, serial_speed, timeout=1)
Then, the core of the Python script is the measure() function that is continuously executed every second:
# Measure data from the sensor def measure(self): # Request data and read the answer ser.write("m") data = ser.readline() # If the answer is not empty, process & display data if (data != ""): processed_data = data.split(",") self.temp_data.set("Temperature: " + str(processed_data[0])) self.temperature.pack() self.hum_data.set("Humidity: " + str(processed_data[1])) self.humidity.pack() # Wait 1 second between each measurement self.after(1000,self.measure)
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.
Congratulations, you just built a Bluetooth temperature & humidity sensor!
This is the list of the parts that were used in this project:
- Arduino Uno
- Adafruit Bluefruit Bluetooth board
- DHT 11 temperature & humidity sensor
- 4.7K Ohm resistor
- 10 uF capacitor
- Breadboard and some jumper wires
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!