Wednesday, 6 November 2013

Interfacing Serial Devices - Part 1 - Working with the Xbee / BTBee PRO

Serial Devices

There are a lot of external modules that can add functionality to your project - RFID Readers, GSM Modems, GPS Modules, XBees, Bluetooth Modules. Such modules often have their own processing capabilities and provide a controlling serial communication interface. In our lesson on Serial Communicaiton (http://www.induino.com/2013/07/serial-communication-what-where-how.html  - Read this before proceeding!), we saw how to work with serial communication. Now we shall try and extend this to use Serial communication with various devices. Communicating with other devices is not much different from communicating with your computer over serial port.

Xbee - Overview

Xbee is a popular wireless module based on the 802.15.4 Protocol (now Xbee offers a variety of protocol support).  It operates on the 2.4GHz Frequenecy. Xbees provide a easy wireless serial interface and offer a number of functionalities (some of them listed below). Xbee modules can be configured by connecting them to a computer and using the XCTU application.
  • Basic Wireless Serial Interface - In this configuration, the Xbee module act as a wireless serial port. One module is connected to your computer as a serial port and another module is connected to your microcontroller's serial interface. Now any time you print anything from your microcontroller using the Serial.print() function, the same is transmitted across wirelessly to the xbee module connected to the computer and received as serial data by the computer
  • Remote Xbee Control Using Arduino - A pair of Xbees are configured in a mode called the API mode, that would let a remote xbee be controlled / monitored by issuing a command serially. One of the xbees is connected to the microcontroller and another one is configured to be a standalone device with outputs/inputs connected. Now the microcontroller can issue serial commands and control / read the I/O's of the standalone device
  • Standalone Xbee Networks - Xbees can be configured to form different types of networks. For eg. they a pair of xbees can be configured such that I/Os are paired. An input to one xbee's i/o would trigger a output on a corresponding i/o on a different Xbee

Considering the scope of this tutorial, we will stick to using the xbee for Basic Wireless Serial Interface. Xbee's(S1 / S2) by default come configured for the serial port mode, so all we need to do is basic connections. We can even use the same programs from our old Serial port tutorial

Connecting the Xbee to the Computer
You can use a USB Xbee board like the Simple Labs Induino Xbee Explorer to connect an Xbee Module to your computer. The board uses the same FTDI drivers that we used for our Induino R3 boards and the drivers will get installed automatically. A new COM port number will be generated for the board. Open the Arudino IDE, choose the new COM Port and open Serial Monitor. We can view our data here like we would for a normal serial port.


Connecting the Xbee to the Induino R3

You can use the Simple Labs Wireless Xbee/BTbee shield to connect the Xbee Module to the Induino R3.

Ensure that the Jumpers on the Shield are set like in the image below



Now Upload Program 3.1 from the Serial Communication Tutorial
Here's the Program
/*  Induino R3 User Guide - Program 3.1 - Serial Input / Button controlled Binary Counter with Serial Print
 This sketch increases a 3 bit number every time a button is pressed by the user and shows the output on 3 LEDs   
 */

int i = 0;  
char txt[]="The Current Value is : "; // A string stored in a character array
void setup()  
{  
  pinMode(11,OUTPUT);   // declare LED pins as output pins  
  pinMode(12,OUTPUT);  
  pinMode(13,OUTPUT);  
  pinMode(7,INPUT_PULLUP);// declare the Button as INPUT pin with internal Pull up enabled
  Serial.begin(9600); // initialize Serial Communication
  Serial.println("Starting the Program");// This will be printed only once
}  
void loop()  
{  
  if(digitalRead(7)==0 || Serial.available()>0)  // if the button is pressed  or Serial data is received
  {
    if(digitalRead(7)==0) // if the button is pressed
    {  
      if(i<7)        // if counter value is less than 7 or 3 bits  
        i++;        // increment counter value  
      else           
        i=0;        // reset counter to 0  
      while(digitalRead(7)==0);  // wait till button is released to avoid incrementing the counter again               
      delay(100);         // small delay to avoid debounce  
}
    if(Serial.available()>0) // if Serial data is received
    {
      int val = Serial.read(); // read 1 byte of data and store in the integer variable val. if the user sent 1, the ascii value for 1 is 49 so val will have the value 49
      val = val - 48; // 48 is the ascii value for 0. So if we did receive a 0 subtracting 48 would make the value of val as 0, same is true for the remaining numbers
      if(val>=0 && val <=7)// Check if Val is in the range of 0 to 7, we can display only values in this range and ignore all other values
      {
        i = val; // assign the counter to the value received from the Serial Port
      }

    }
    Serial.print(txt); // Print Descriptive test from the character array
    Serial.println(i); // print the current value
    int a=i%2;      // calculate LSB   
    int b=i/2 %2;     // calculate middle bit  
    int c=i/4 %2;     // calculate MSB   
    digitalWrite(11,a);  // write LSB 
    digitalWrite(12,b);  // write middle bit  
    digitalWrite(13,c);  // write MSB  
    
  }  
}

Now open the Serial Monitor with the COM port corresponding to the Xbee Explorer Board and key in Numerical Values as you would normally. You should see the same values being displayed on the LEDS on the microcontroller Board. If you have a 12V DC Power Adaptor, you can power your Induino Board using the adaptor and keep it away from your computer. Now you have a remote board that can be controlled from your computer using a serial interface!


There is a lot more you can do with your Xbees. Xbee's by themselves would require a dedicated tutorial. Perhaps we will do one later :)

BTBee Pro - Overview

The BTBee Pro is a Xbee Format Bluetooth Breakout Module based on the popular HC-05 Bluetooth Module. This module provides us with Serial Bluetooth Interface - Meaning which, we can transmit serial data over bluetooth.

Given that we are using Bluetooth and most Phones have Bluetooth, the number of project possibilities increases! Bluetooth control seems to be the In-Thing! hook up your Induino R3 with your Android phone over Bluetooth and zap you go... With a little bit of Android Expertise, you can double the potential of your Arduino Projects.

BTBee General Harware Overview
The BTBee Pro module has 2 Buttons, 3 LEDS(PWR, CON & STATUS) and 1 Slider switch. Ensure that the Slider switch is on the side marked 'upload'. When the Module is Powered on the PWR LED will glow.  When the device is powered and not paired, the Status LED will keep Blinking. When the Device is paired the CON LED will be ON and the Status LED will stop Blinking.

We can plug in the BTBee Pro module onto the Simple Labs Wireless Xbee/BTbee shield as we did with the Xbee and use the same program as above.

Controlling Using an Android Phone

  • Download the following App for your android phone BlueTerm
  • Switch On Your Bluetooth and Scan for Devices
  • The BTBee Pro will show up either as 'BTBee Pro' or as 'HC-05'
  • Use the Pairing Code '1234' to pair with the device (Pairing is not complete at this stage, Pairing will actually happen only when you establish communication with the device!)
  • Open Blueterm and use the Options Key on your Phone and Select the 'Connect' option
  • You will see a list of devices available, Choose the BTBee Pro / HC-05 device
  • The Device will now be paired, you can check the CON Led on the module to verify this
  • Now using the Bluterm's Terminal, send commands as you would from a Serial Monitor

You can use other apps as well... here are a few that we have developed (No source Code is not available for these Apps!)

Thats It For This Part! Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in

 Visit www.simplelabs.co.in for more interesting products


4 comments:

  1. How to use the gesture control app? What code should I upload to the induino r3?

    ReplyDelete
  2. I am using BTBee Pro with induino r3
    and I am using a app blue term to send data from android to induino.
    but the issue is that every time I send something from app it just displays the some extra character as below:
    þ
    þ
    þ
    þ

    this is the output of my serial monitor .

    ReplyDelete
    Replies
    1. ok. can you try all baud rates available in the arduino serial monitor? you need to change them in the program? It looks like this one is misconfigured on the baud rate.

      Delete
  3. How about when u wanna control it through an iphone/ipad?

    ReplyDelete