MATLAB Examples

Continuous monitoring of wireless network of temperature sensors using MATLAB® and XBee® (Part 1)



One problem with living in Boston is that my apartment is old and drafty. I have one thermostat that is located in the center of the apartment, and I often find that even though the thermostat shows a nice and toasty 73 F, I'm sitting in my bedroom or the office freezing. I have often wanted a way to monitor the temperature throughout my apartment so I could tune my home heating system to more uniformly heat my apartment.

Over the next few blog posts, I'm going to demonstrate how I used MATLAB to monitor the temperature throughout my apartment. Based on the data I collected, I can tune my heating system by adjusting the valves on my radiators and the settings on my thermostat.

The key ingredient in this system is a wireless network of temperature sensors built using XBee® modules. The MathWorks MATLAB Hardware Team recently published an example of how to wirelessly control and monitor an LED using XBee and MATLAB. I built upon the code they provided to make interfacing between MATLAB and the XBee network quick and simple.

To start with, in this post I'll talk about building the wireless temperature sensors.

Parts and Tools

Temperature Sensor Edge-Nodes

(1 edge-node per room with up to 4 temperature sensors each)

The temperature sensor network consisted of one edge-node (with one XBee) per room in my apartment, but the system is designed so that you can start with one or two edge-nodes and expand later if necessary. In addition, each edge-node can read up to four voltage signals, so you can measure the temperature outside, next to the windows, next to the radiator, and on the other side of the room with one edge-node.

Computer Interface

The computer interface was an XBee with an XBee Explorer USB.

Tools and Miscellaneous Supplies

  • Soldering iron
  • Solder
  • Wire cutters
  • Wire strippers
  • (optional) Needle-Nose pliers
  • (optional) Heat shrink
  • (optional) Electrical tape
  • (optional) Helping-hands

Configuring the XBee Modules

Before starting, I needed to update the firmware on the XBee modules using the X-CTU software provided by Digi. Instructions for updating the firmware are included in the example posted on the File Exchange.

For this project, I used the API mode of the XBee modules, which gave me better access to the analog input pins on the individual modules than I would have had with AT mode. The one XBee that was plugged directly into my computer needed to be configured as a coordinator. I used the firmware 21A7, "ZigBee Coordinator, API Mode". I configured the remaining XBee modules as end devices (firmware 29A7, "ZigBee End Device, API Mode"), but they could also be configured as routers (firmware 23A7, "ZigBee Router, API Mode").

Temperature Sensor

The temperature sensor I used for this project is the TMP36. This inexpensive temperature sensor uses a three wire interface: Ground, Power, and Signal. The power supply can range from 2.7V to 5.5V, which meant it could share a power supply with the XBee.

Figure 1: TMP36 Temperature Sensor

The output signal is a voltage linearly proportional to the temperature:

$T = \frac{V_{out}}{10mV/^{\circ}{\rm C}}-50^{\circ}{\rm C}$

Living in the US, I'm used to Fahrenheit temperatures.

$T = \frac{9 V_{out}}{50mV/^{\circ}{\rm F}}-58^{\circ}{\rm F}$

The maximum analog input voltage on series 2 XBees is 1.2V, giving me a temperature range of -58°F to 158°F (-50°C to 70°C), which was a wide enough range for my needs.

Assemble XBee Breakout Boards

For each edge-node, I used one breakout board. These did not come assembled, so some soldering was necessary to attach both the 2mm 10pin XBee sockets as well as male headers. As I was using a second XBee Explorer USB for testing, I soldered male headers onto the XBee explorer as well.

    When soldering the sockets, be careful not to use too much solder, as too much solder can flow down into the socket.

The final product looked like this:

Figure 2: Assembled XBee Breakout Board

Test Circuit

Before building my edge-nodes, I wanted to test the XBee modules and temperature sensors to make sure that everything was working as expected.

For testing, I plugged the coordinator into my computer via the XBee Explorer USB, and I connected two of the remaining XBees onto a single breadboard. One XBee was plugged into an XBee Explorer USB, and the other was plugged into an XBee Breakout Board, with both XBees sharing a single power rail. Power was provided by an old cell phone charger through the USB port on the XBee Explorer USB, but you could also plug it into a computer. The 3.3 V voltage regulator on the XBee Explorer USB provided just enough power for both XBees® and some temperature sensors.

    Be careful that you connect to the 3.3 V power, not the 5 V USB power, as the maximum supply voltage on the XBee modules is 3.6 V.

I connected two TMP36 temperature sensors to pins 19 and 20 of both XBees® so I could test out analog voltage input.

Figure 3: Testing Circuit (Fritzing file)

    Note that the power and ground pins are in different positions depending on whether you are using an XBee Breakout Board or an XBee Explorer USB.

Hello XBee!

Once the testing circuit was assembled, it was time to make sure I could talk to the XBee network from MATLAB.

Open the connection to the coordinator XBee: The COM port will have to be changed to reflect the COM port used by your XBee explorer. You can get this information from X-CTU.

>> xb = xbee('COM3');

Check that I am able to talk to the coordinator by requesting the settings. These settings should match what you set in X-CTU.

>> getLocalParameters(xb)
          PANID: 0000000000000000
Operating PANID: F054514C95CA5757
      Address64: 0013A20040B181B2
      Address16: 0000
       BaudRate: 9600
FirmwareVersion: 21A7
HardwareVersion: 194B

Now search to see what other XBee modules are on the network:

>> addrs = discoverNetwork(xb)
addrs =

Now I can read the voltage on pin 19 of the first XBee:

>> v19 = readVoltage(xb, 19, addrs{1})
v19 =

And the voltage from pin 20 on both XBees:

>> v20 = readVoltage(xb, 20)
v20 =

And convert from voltage to degrees Fahrenheit:

>> v = [v19; v20]
v =
>> tempC = v/0.01-50
tempC =
>> tempF = tempC*9/5 + 32
tempF =

Designing the Edge-Nodes

Each edge-node consisted of 1 to 4 temperature sensors. I placed the temperature sensors at the end of varying lengths of telephone wire so I could position the temperature sensors through each room, including one sensor in the radiator.

The edge-nodes were powered with 2 x C-batteries each.

Here is a wiring diagram for one edge-node with 4 temperature sensors:

Figure 4: XBee Edge-Node Circuit (Fritzing file)

Building the Edge-Nodes

Step 1: Attach female headers to protoboard

a) Break 2 lengths of female header 10 pins long. You will likely lose one female pin for every break, so factor that in when ordering. In a pinch you can use one 10 pin header and one 9 pin header, as you will not need to connect anything to pin 11 on the XBee. In the picture below step 2d you can see where I used hot-glue to repair a partially broken female header.

b) Using the XBee Breakout Board as a template, solder the female headers in place on the protoboard.

Figure 5: Protoboard with XBee Headers

Step 2: Attach temperature sensors

a) Strip the insulation off both ends of the telephone wire.

b) On one end of the wire slide short sections of heat shrink over each individual wire. The heat shrink will help prevent the pins in the temperature sensor from touching.

c) Solder the temperature sensor to the telephone wire. I recommend using the red wire for power and black wire for ground (see reference image above).

For some edge-nodes I put two temperature sensors at the end of one wire. The two sensors can share power and ground, so only four wires are needed for both sensors. This allows me to compare the readings from these two temperature sensors as an internal check of each sensor's accuracy.

Figure 6: Connected Temperature Sensors

d) Solder the other end of the telephone wire to the protoboard. All the temperature sensors share the same power and ground connections, but the signal wire from each temperature sensor should connect to one of pins 17, 18, 19, or 20 on the XBee. In the picture, these are the four wires on the top-right edge of the protoboard. On an XBee, these are the upper-right four pins (with the logo facing up and readable).

Figure 7: XBee Analog Input Pinout (Fritzing file)

Figure 8: Protoboard with wires

e) Solder the wires from the battery pack to the power and ground pins.

Figure 9: Battery pack connected

f) Attach the XBee breakout board and XBee

Figure 10: Complete XBee edge node without XBee

Figure 11: Complete XBee edge node with XBee

Figure 12: Final product

Testing the Edge-Nodes

Once I had assembled the edge-nodes, I wanted to test them out. I put fresh batteries in each edge-node, and plugged my coordinator XBee into my computer using the XBee Explorer USB.

Open the connection to the coordinator XBee:

xb = xbee('COM3');

Check that I plugged in the coordinator, and didn't accidently mix up the XBees.

ans =

Good, 1 means I have the coordinator. Now search to see what other XBee modules are on the network:

addrs = discoverNetwork(xb)
addrs =

I have four edge-nodes, so I should get four addresses back. Now I can read the voltage on pin 20 on all four XBees:

[v,src] = xb.readVoltage(20)
v =
src =

And convert from voltage to degrees Fahrenheit:

tempC = v/0.01-50
tempF = tempC*9/5 + 32
tempC =
tempF =

Next time

Now that I have assembled the edge-nodes, and tested to make sure that I can get readings from each edge-node, next time I will go through the process of gathering long-term temperature data.