The Bluetooth Sensor

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

The Bluetooth Sensor/Controller Project

January 2008
This project was motivated by an inuiry on the !icroso"t#public#compact"ramewor$
news%roup# The %ist o" the uestion was& '(ow can ) connect a switch input to my
Poc$et PC& so that ) can detect chan%e*o"*state o" the switch+, The problem is that
Poc$et PC-s& in %eneral& and other PC-s& such as some noteboo$ computers have no .or
very limited/ %eneral*purpose hardware )/0# Thus& it can be hard to connect them to
real*world inputs#
1nother issue that ma$es such )/0 problematic is that you have to actually connect
wires or a cable between your computer and the sensor2 This certainly reduces the
portability o" the computer#
0ne thin% that many Poc$et PCs and
noteboo$ computers have is a
inter"ace# Bluetooth is an
industrial speci"ication "or wireless personal
area networ$s .P13s/# Bluetooth provides a
way to connect and e4chan%e in"ormation
between devices such as mobile phones&
laptops& PCs& printers& di%ital cameras& and
video %ame consoles over a secure&
%lobally unlicensed short*ran%e radio "reuency# The Bluetooth speci"ications are
developed and licensed by the Bluetooth Special )nterest 5roup#
06& but ) don-t see anythin% in this 'o""icial description, o" Bluetooth that su%%ests that
Bluetooth is %ood method to use "or this simple 7 or "or other even more comple4 7
applications# 1nd& by itsel"& that objection is correct# (owever& i" we combine a simple
microcontroller board that has a serial communications port& with a serial Bluetooth
adapter& we now have a mechanism "or sendin% and receivin% wireless data between
our Poc$et PC or other computer and the microcontroller board# 8e can use the
microcontroller to provide the inter"ace to our sensors .anythin% "rom a simple switch to
much more comple4 'stu"",/ and also to provide a way "or us to send commands and
control si%nals "rom the computer to the microcontroller9
:et-s start by selectin% some o""*the*shel" hardware that we can use to reali;e this
project#
There are several commercial Bluetooth serial adapters< in %eneral each is "unctionally
euivalent to the others in this cate%ory# Popular manu"acturers are =uatech& Soc$et
Communications& )05ear& and more# ) chose the )05ear !odel 5BS >0?# )t is
comparately low cost& about @AB& with shippin%& and it is readily available# ) purchased
my adapter thou%h eBay#
There also are 0C! Bluetooth inter"aces that mi%ht be inte%rated directly into a low*
cost solution#
There are do;ens o" microcontroller boards that mi%ht be used# ) based by choice on
these characteristics#
:ow cost& naturally
Case o" pro%rammin% .a B1S)C lan%ua%e variant would be ideal/
Compact "orm "actor
Simple power reuirements .ideally it can be powered "rom the same power
supply as is used by the Bluetooth adapter
) chose the Paralla4 B1S)C Stamp 2 !odule .@B0/ with a B1S)C Stamp Super
Carrier board .@20/ to provide the serial inter"ace circuitry& power supply& and
prototypin% space "or my own circuitry# The simpler B1S)C Stamp Carrier Board .@?B/
also would have wor$ed#
)t is hard to predict what sort o" hardware sensors or controls mi%ht be used in a real*
world application o" these ideas& so ) decided to '$eep it simple, and to show how one
mi%ht inter"ace two di""erent sensor types .two switches "or di%ital input& and two
variable resistance sources "or analo% input 7 one a thermistor "or temperature
measurement& and the other a photo*resistor "or li%ht measurement/# To simulate an
output to be controlled "rom the computer& ) chose a :CD to turn on and o"" by command
"rom the computer# 3aturally& more comple4 inputs and outputs may be used in your
own application9 Total cost o" this additional hardware was about @?0 7 thus& the
project cost was on the order o" @?A0& when shippin% costs are included#
The B1S)C Stamp has ?E )/0 ports# For the hardware that )-ve described above& ) used
"our ports to input sensor data& and a sin%le port "or output# The documentation that is
provided with the B1S)C Stamp %oes into detail about the electrical inter"ace& so )-ll
leave that area alone# The "ollowin% "i%ures show how each sensor and output was
desi%ned and connected to the BS2 module#
Fi%ure ? .:CD 0utput connected "rom BS2 Port
E/
GB H 200 I
D? H small Ged :CD
Fi%ure 2 .Switch )nput to BS2 Ports J and
?0/
GK & GB H ?06 I
Fi%ure > .Photo*resistor )nput to BS2 Port 8/
GE H 200 I
G? H J6 7 E006 I Photo*resistor& C:B!B
C? H 0#0? LF
Fi%ure K .Thermistor )nput to BS2 Port K/
G2 H 200 I
GA H ?6 I Thermistor& 3TC
C? H 0#? LF
) considered addin% a P)G! Passive )n"raGed detection !odule .1!P ?80*200/& but
decided that the point had already been made# (ad ) done so& the schematic "or this
detector would have been uite simple& and is shown in the ne4t "i%ure#
Fi%ure B .P)G! )nput to BS2 Port 2/
The P)G! ?80*200 in"rared module is a compact ?80M "ield o" view motion sensor# This
provides the user with a di%ital active hi%h output indicatin% when the sensor has
detected motion within a ran%e o" 20 "eet#
Microcontroller Code
8ritin% and debu%%in% code "or the Paralla4 BS2 is strai%ht "orward# Download the
B1S)C Stamp Cditor .)DC/ "rom Paralla4# Connect a PC serial port to the Carrier Board
with the BS2 module installed in the soc$et provided# Connect power to the Carrier
Board .use a JN battery& or connect to a EN or hi%her supply/# 8rite your pro%rams and
download them to the BS2 "or testin%# The instructions and e4amples provided by
Paralla4 illustrate the process& and a "ew minutes should %et you %oin%#
The B1S)C lan%ua%e synta4 used will seem both "amiliar and alien# )t is much more
terse than NB& and structured more li$e the ori%inal "orms o" B1S)C .50T0 labelname
is encoura%ed& not discoura%ed< simple subroutines are the only structured elements/#
8e need to reco%ni;e that the %oal o" our microcontroller pro%rammin% is utility over
"orm 7 and the B1S)C lan%ua%e that is provided is utilitarian#
(ere is a view o" the B1S)C Stamp )DC#

Fi%ure E Basic Stamp Cditor .)DC/
The Carrier Board has a J*pin GS2>2 "emale connector that we use "or BS2
pro%rammin% and debu% "rom the B1S)C Stamp Cditor# This port also is the one that
we will use to connect out Bluetooth adapter# Because Paralla4 uses some hardware
handsha$in% lines to identi"y the BS2 .and durin% pro%rammin%/ that are not supported
by the Bluetooth adapter& all pro%rammin% and debu%%in% should be done usin% a
standard GS2>2 adapter 7 this can employ either a standard GS2>2 port& or a OSB
serial adapter# 0nce a pro%ram has been debu%%ed usin% standard hardware& the
Bluetooth adapter can be substituted "or the cable connection to provide the 'real*world,
wireless connection between sensor/controller and our Poc$et PC or other computer#
Be"ore be%innin% pro%rammin%& it would be appropriate to create a serial protocol that
will support the data and commands that we will be employin%# This protocol need not
be comple4 7 and the one that )-ve created is uite simple 7 but it should be "le4ible
enou%h to be e4panded to meet whatever may be needed in the "uture# 3aturally& the
actual hardware that we are employin% creates and upper boundary on the comple4ity
o" this "uture#
The Serial Protocol
Commands may be sent "rom the host computer# ) have only one thin% to control .the
red :CD on the microcontroller board may be turned on or o""/& so the command
structure can be simple# ) chose to use an 1SC)) .all te4t/ protocol to ma$e thin%s as
easy as possible#
Command Data Terminator Gemar$
P ? none Turn :CD on
P 0 none Turn :CD o""
P
1ny other
alphanumeric
character
none
Onde"ined#
(owever& this allows
us to add numerous
commands to a""ect
other
microcontroller
"unctions
1 command strin% "rom host computer to controller will loo$ li$e thisQ
'P?,
0r
'P0,
The response protocol "rom the microcontroller to the host computer also is simple# )-ve
enhanced responses to add both a Gesponse type character and a Terminatin%
character 7 so responses can be o" variable len%th# (ere is that part o" the protocol#
Gesponse
Character
Data Terminator Gemar$
S ?R0&?R Carria%e Geturn S?>T Switch ? on H 0& o""
H ? .active :ow
lo%ic/
S 2R0&?R Carria%e Geturn S?>T Switch 2 on H 0& o""
H ? .active :ow
lo%ic/
: Nalue R0*EBB>BR Carria%e Geturn S?>T :i%ht level&
measured by
chan%e o"
resistance in the
sensor# Practical
measurements will
ran%e "rom ? .very
bri%ht illumination/
to perhaps B0000
.very little
illumination/
T Nalue R0*EBB>BR Carria%e Geturn S?>T Temperature
measured by a
chan%e o"
resistance in the
sensor will ran%e
"rom >B .about B0M
C/& BK .about 2KM
C/ to perhaps AE
.about 0M C/
So& "or e4ample a response strin% o" 'TBK, U vbCr represents a temperature o"
appro4imately 2KM C# 1 response strin% o" ':?KK, U vbCr indicates low intensity li%htin%&
perhaps about >0 :OV# 1 response strin% o" 'S?0, U vbCr indicates that switch ? is
closed& while 'S2?, U vbCr indicates that switch 2 is open# Temperature and li%ht levels
are not calibrated& and the ran%e o" values is non*linear# :ineari;ation should be done
on the host computer 7 these techniues are well described elsewhere#
(ere is B1S)C Stamp code that implements the hardware inter"ace and serial protocol#
This code consists o" two main processes# First& variables are de"ined that a used later
in the code# 3e4t& BS2 port pins J and ?0 are de"ined as inputs# Then& the !ain
processin% loop is entered .label !ainQ/#
)n the !ain processin% loop& the serial port is polled to see i" a command has been
received# This pollin% uses the SCG)3 statement with a timeout i" no data have been
received# )" data are received be"ore the ?000 mS timeout& then those two bytes .only
two bytes are allowed in the SCG)3 statement& and the "irst character must be a 'P, 7
the second characters is the actual command/ are processed# )" the command
character is an 1SC)) ? .decimal value KJ/& then the :CD is lit& while i" it is an 1SC)) 0
.decimal value K8/ then the :CD is e4tin%uished# )" some other command character is
received& the entire command is i%nored# The possibility that other characters mi%ht be
received allows this command interpreter to be e4panded in the "uture#
1"ter a command has been processed& e4ecution continues with the label 0therQ# This
code con"i%ures ports K and 8 "or resistance measurement< it then calls subroutines in
seuence to output sensor status# The "irst subroutine tests switch state on ports J U
?0 and reports their state# 3e4t& the resistance o" the thermistor is measured and that
value reported& then the resistance o" the photo*resistor is measured and reported#
8hen all sensor states have been reported& the pollin% loop resumes at !ainQ#
)" no valid commands are received be"ore the ?000 mS timeout o" the SCG)3 statement&
the timeout results in callin% the sensor status routines .0therQ label/ directly# Thus& in
absence o" a command& the microcontroller reports its sensor status automatically every
?000 mS# 8hat this means is that the host pro%ram can simply monitor and interpret
data "rom the sensors& whether or not a command has been issued# )" a command is
issued& sensor status is reported immediately# By e4tendin% the SCG)3 timeout
parameter& this automatic reportin% interval can be a "reuent or in"reuent as desired#
0" course& automatic reportin% is a "eature that can be s$ipped entirely& too#
Using the Bluetooth Serial Adapter
The Bluetooth adapter has a set o" dipswitches that con"i%ure mode and serial speed#
The de"ault mode .slave/ and speed .JE00 bps/ are "ine "or our application# 8hile the
adapter supports hi%her serial speeds& these are not needed and mi%ht burden the
microcontroller& ma$in% operation less reliable#
There is one important hardware consideration when usin% the )05ear BT adapter .this
may or may not be true on other adapters/# The )05ear adapter employs hardware
"low control# Thus& CTS .Clear To Send/ must be raised .(i%h on the GS2>2 inter"ace/
"or it to output data that has been received "rom the host computer# The BS2 serial
inter"ace does not connect the CTS pin& so it should be connected to GTS at the J*pin
serial connector on the Carrier Board# Connect pins A and 8 to%ether to assure receipt
o" data#
Connect power to the Bluetooth adapter& and "ollow the manu"acturer-s instructions "or
pairin% the adapter with your host computer .Poc$et PC or other/# The "ollowin% "i%ures
show these steps# Fi%ures A U 8 illustrate this on a Dell 14im VB?v Poc$et PC#
Fi%ure A Pairin% Steps ? U 2
Fi%ure 8 Pairin% Steps > U K
0nce pairin% has been completed& it is possible to write and debu% Compact
Framewor$ code that uses the Bluetooth Nirtual Serial Port .SPP/ that these steps have
created# Fi%ure J shows a simple application in action#
Fi%ure J 1 Compact Framewor$ 1pplication
Chec$ the :i%ht :CD chec$bo4 and clic$ the Send Command 5et Status button to li%ht
the :CD on the microcontroller board and to view sensor data in the te4t bo4# :i$ewise&
unchec$ the :i%ht :CD chec$bo4 and clic$ the Send Command 5et Status button to
e4tin%uish the :CD on the microcontroller board and to view sensor data in the te4t bo4#
)n all o" the host computer pro%rams& you would want to add additional parsin% to
actually interpret the sensor data in a more complete way#
Pairin% the Bluetooth serial adapter with a des$top/noteboo$ or similar computer would
be done in a similar way to that "or the Poc$et PC# Follow the steps described in the
Bluetooth adapter documentation# The "ollowin% "i%ures show that process "or such a
host#
Fi%ure ?0 Bluetooth Pairin% on a Des$top or 3oteboo$ computer
0nce paired& you can write a host application usin% your lan%ua%e and environment o"
choice# The "ollowin% "i%ure shows host applications written in Nisual Basic 200B and
NBE& respectively#
Fi%ure ?? Des$top (ost Pro%rams
Complete source code "or these e4amples& and more e4tensive B1S)S Stamp e4amples
are included on the CDG0! that accompanies my boo$& Nisual Basic Programmers
Guide to Serial Communications, 4
th
Edition. See the Boo$s lin$ on
www#hardandso"tware#net "or more in"ormation#

You might also like