MATLAB Examples

Wireless control and monitoring of an LED using XBee

In this application, MATLAB® is used to wirelessly control an LED using two XBee Series 2 modules. A mini photocell is used to wirelessly monitor the state of the LED. By integrating MATLAB into the workflow of wirelessly controlling and acquiring data from sensors, the power of MATLAB scripting and feature rich libraries can be incorporated into your wireless sensor experiments.

Contents

Required Hardware

Required Software

This setup has been tested on a Windows 7 64-bit OS running MATLAB R2013b. X-CTU software installer on Windows 7 was used to setup the XBee modules. Once the XBee modules are set up as a coordinator and router, they can be used from MATLAB on either Windows or Linux OS.

XBee Setup - Router and Sensor Setup

XBees are wireless transceivers from Digi that implement the Zigbee protocol. They are simple devices that can be used to set up serial communications between applications. We will be constructing a point-to-point network, meaning two devices are going to communicate with each other. One of the devices must be a Coordinator (all XBee networks require a Coordinator) and the other must be a Router (an 'endpoint' device). XBee coordinator module forms the root of the network tree and it stores information about the network. In this example the XBee coordinator is always connected to MATLAB. The XBee router module (acting as an end device) will be connected to the sensor setup and will perform the required measurements. The XBee router module will be set up next.

Download and install X-CTU software from Digi International. The following graphics are from the X-CTU ver. 5.2.8.6 installer on Windows 7 operating system. Newer versions of X-CTU software can be used to set up the XBee modules to the firmware indicated below as well.

  • After installation, connect the XBee module to be set up as a router to your computer and start X-CTU.
  • X-CTU will automatically detect the available COM ports:

  • Select the USB Serial port the XBee Explorer is connected to (COM15 is used in this example).
  • Click on the Modem Configuration tab.
  • In the Modem Configuration tab, select XB24-ZB from the Modem drop-down menu, ZIGBEE ROUTER API from the Function Set drop-down menu and 23A7 from the Version drop-down menu.

  • After making the selections mentioned above, click the Write button to download the firmware. The XBee router module is now ready.
  • Disconnect the XBee router module from the computer and connect it to the LED and mini photocell setup as shown below. The XBee router module is connected to a breadboard using an XBee USB Adaptor. On the breadboard, the XBee router's pin 19 is connected to an LED, and pin 20 is connected to a photocell. Placing the photocell as close as possible to the LED will lead to better results.

XBee Setup - Coordinator Setup

  • Connect the second XBee module to be set up as a coordinator to your computer.
  • Find the connected XBee module in X-CTU by selecting the appropriate COM port.
  • Click on Modem Configuration tab, select XB24-ZB option from the Modem drop-down menu, ZIGBEE COORDINATOR API from the Function set drop-down menu, and 21A7 in the Version drop-down menu.

  • After making the selections mentioned above, click the Write button to program the firmware.
  • This XBee module will stay connected to the computer (and MATLAB) for the entirety of this example.

MATLAB® Connectivity Verification

Before wirelessly controlling the LED, let us verify the connectivity between MATLAB and the XBee coordinator module connected to the computer. Note that the below example requires only one XBee router to be active in the network. If more than one router is active then the below code has to be changed appropriately to accept the response from multiple XBee routers.

Close X-CTU software and execute the following commands in the MATLAB Command Window to verify connection with the XBee coordinator module. The COM port number will have to be changed to a value appropriate for your setup. X-CTU will display the COM port number the XBee coordinator module is connected to.

x = xbee('COM15');

getLocalParameters(x)
           PANID: 0000000000000000
 Operating PANID: 594D5B84B6A24904
       Address64: 0013A20040ABB72B
       Address16: 0000
        BaudRate: 9600
 FirmwareVersion: 21A7
 HardwareVersion: 1943

The displayed parameter values will be identical to those displayed by the X-CTU interface, thereby verifying connectivity between MATLAB and the local XBee coordinator module.

Wireless LED control and monitoring using a photocell

MATLAB is used to wirelessly turn on and off the LED connected to pin 19 of the XBee router module. To monitor the state of the LED, MATLAB is also wirelessly reading the analog voltage of the photocell placed adjacent to the LED. The photocell voltage is measured 5 times before turning on the LED to read the ambient light condition, then 5 times while the LED is on, and 5 times after turning off the LED.

% Read analog voltage of the photocell at pin 20 of the XBee router
% before turning on the LED
 for numCount = 1:5
    voltageBeforeLED(numCount) = readVoltage(x, 20);
end

% Turn on the LED connected to pin 19 of the router
writeDigitalPin(x, 19, 1);

% Read photocell voltage at pin 20 of the XBee router while LED is on
for numCount = 1:5
    voltageDuringLED(numCount) = readVoltage(x, 20);
end

% Turn off the LED
writeDigitalPin(x, 19, 0);

% Read photocell voltage after turning off the LED
for numCount = 1:5
    voltageAfterLED(numCount) = readVoltage(x, 20);
end

Plot photocell voltage values

Concatenate the photocell voltage readings acquired before turning on the LED, while the LED was on and after turning off the LED. Then generate a plot to visualize the changes in the photocell voltage based on the LED state.

plotData = [voltageBeforeLED, voltageDuringLED, voltageAfterLED];

figure;
plot(plotData);
title('Wireless Photocell Voltage Measurement');
xlabel('Sample Number');
ylabel('Voltage');
grid on;

% Clear the xbee connection.
clear x

As shown in the plot, the photocell voltage readings are higher when the LED is turned on. This example illustrates MATLAB being used to wirelessly control an LED and monitor the LED state using a mini photocell. Wireless communication was achieved by using two XBee series 2 modules.