Introduction: Proximity-Sensing Pocket Squares

Picture of Proximity-Sensing Pocket Squares
My friends and I like to coordinate for our mutual friends' weddings.  Our friends L & E got married recently and we wanted to do something special to mark the occasion.  We settled on something elegant and simple -- pocket squares that could visualize the number of people within a certain proximity.  For each pocket square I sewed 4 LEDs to a LilyPad Arduino and programmed it to display the number of other pocket-square-wearers present, using the XBee's RSSI data for coarse grain distance approximation.

In this Instructable, I will show you how to make your own set of proximity-sensing pocket squares. 

I used the following materials per person:
  • 1 pocket square (doesn't have to be silk--in fact, a more sturdy material works a little bit better to help retain the shape)
  • 4 lights (I used white lilypad LEDs)
  • 1 LilyPad Simple Board)
  • 1 LiPo Battery
  • 1 XBee (Series 1)
  • 1 XBee Breakout board (I designed my own XBee Breakout for the LilyPad)

Step 1: Construction

Picture of Construction

To make the pocket square patch, cut out a small piece of fabric (I cut 4.5"x3.5"  to give myself some extra room).  Next, sew the plus side of each of the 4 LilyPad LEDs to an individual petal (PIN) on the LilyPad.  I used pins 5, 6, 9, and 10.  Lastly, using conductive thread, sew all the minus sides of the LilyPad LEDs together and connect to pin 11 (we will declare pin 11 ground using the Arduino software). 

Good work!  Now you're done with the construction!

I designed my own XBee Breakout Boards that easily plug into the male header on the LilyPad.  You could use the Lilypad XBee Breakout and sew the boards back to back. 

Step 2: RSSI Discussion

Picture of RSSI Discussion

The proximity-sensing depends on the XBee's ability to process RSSI data.  Here's a brief overview of how this works:

The Received Signal Strength Indicator (RSSI) is a measurement of power of the incoming packet. This means that as the signal travels further distances. the signal will attenuate and the RSSI value will decrease. This also means that if modules are separated by a barrier (brick wall) or if the incoming packet bounces around (reflections) before hitting the XBee, then the signal will also attenuate and the RSSI value will also decrease. This often leads to noisy and unreliable data.  But there is hope.  While we probably can't get fine grain detail of distance, we can tease out coarse grain information: "are the modules relatively close or relatively far?"

The attached image shows the RSSI values as a function of angle for the XBee modules with the chip antenna.  (For other antenna configurations please see the RF Module Antenna Manual).  As you can see, the pattern is highly anisotropic and we should expect that the orientation of the XBees will greatly effect the RSSI value.

I have also contacted Digi and asked if they would send globular data to show the radiation pattern in 3D.  I'll post it here when I receive it.

Step 3: XBee Discussion and Configuration

Before we continue, I should mention that there is a crucial difference between the XBee Series 1 and Series 2.  Aside from the fact that Series 2 uses the ZigBee protocol and Series 1 does not, Series 1 processes the power of the packet and encodes the value into the RX packet that the XBee API has access to.  Series 2 also processes the power of the packet; however, it does not encode the value into the RX packet.  Digi changed the structure of the RX packets for Series 2 and you have two choices to get the RSSI value.  You can either stop receiving packets and query the XBee to tell you what the RSSI value is (ATDB and this takes quite a bit of time) or you can use a trick discussed on the Arduino Message Board, where the RSSI value is represented through a PWM timer.

With that said, I chose XBee Series 1 to easily get access to the RSSI data.  We will need to use the API mode for the XBee to be able to fully process the RX packet.  My XBee settings were:

Channel: F
PAN ID: 3141
DH: 0
DL: 0 (I used the XBee Arduino API to set the value for broadcast mode 0xFFFF)

I have also attached the XCTU profile I used for those wanting to use my exact settings.  And if you need help configuring your XBee for API mode please see my other Instructable on this topic.

Step 4: Raw Data

Picture of Raw Data
This page, as well as the next two, are a discussion of RSSI data and filtering schemes.  If you wish to skip to the next stage of construction and the code, please go to step 7.

In order to set a threshold for the pocket squares' ability to detect each other, we need to get an idea of what our data looks like and how RSSI values are correlated to distance.  I configured 3 modules: A Base Module receiving data and streaming the RSSI value to my computer and two Mobile Modules that can be moved to any location.  The first plot shows an example trial run.  The two Mobile Modules were started at a distance 2 feet away in the same orientation.  At TimeStep 125 Module 2 was moved to a distance of 10 feet, then at TimeStep 300 Module 1 was moved to the same location as Module 2.  At TimeStep 480 Modules 1 and 2 were then moved back to their starting locations; however, Module 1 was rotated 180 degrees relative to the orientation of Module 2.

There are a few things I should point out here. 
  1. The RSSI value reported from the XBee increases as the distance increases
  2. The data, as expected, is pretty noisy with lots of random peaks. 
  3. Also as expected, orientation can change the RSSI value.  Looking at the last hundred TimeSteps of Module 1 we see that there is a 5 point increase compared to the start. 
  4. We can see a difference between  modules at 2 feet versus modules at 10 feet.  This means we can detect coarse grain distance (at least +/- 5 feet)!  Now all we need is the right filter...

Step 5: Filtering Data

Picture of Filtering Data

Wow! There are sooooo sooo sooo soo many ways to filter data.  The world is noisy and to pull out the useful information we need to filter the noise.  Our data can have sudden spikes or valleys.  That means the if we filter our data through averaging or a moving average our we would have to take many many points to filter out those spikes an valleys.  If we look at the data we could probably guess as to what the value should be versus measured value.  The Kalman Filter can mathematically help us do just that.  The Kalman Filter calculates what a value should be based on the difference between the measured and a expected value.

The Kalman Filter has been used in loads of applications, from missile guidance systems to cleaning up accelerometer data. 

Here are some good examples of Kalman filters and Arduinos:
Accelerometer example:
Another Kalman Arduino Example:
The Keyglove (Please fund!)

Step 6: Kalman Filter

Picture of Kalman Filter
Interactive-Matter has a good post on filtering sensor data with the Kalman Filter and even has a library already to go for the Arduino.   For those interested in the foundational principles and a good introduction to the Kalman filter please read:

I will also try and explain the filter here.

Recursively solve:
p = p+q;
k = p / (p + r); (gain)
x = x + k * (rssi_data – x);
p = (1 – k) * p;

That's it! 

Initial Conditions:
x = x_o (best guess)
p = p_o (estimated error)
r = r_o (sensor noise)
q = q_o (process noise) value between 0 and 1

Parameters in bold remain static through the simulation

So how do we choose our parameters?  Parameter choice is difficult and often requires lots of massaging.  To get some idea of how we should choose q and r, let's look at some extreme behavior:
  • If q is much bigger than r, then k =1 and x is just the measured value. 
  • if r is really small (very very close to 0), then k  = 1  and x is just the measured value
This means that q/r or r/q shouldn't be too big or too small and r should be too small.   Again, you will have to massage the parameter to get the data that you want.  I selected Kalman 3 because it's a good balance between lag time and filtering noise, where as Kalman 4 filters the noise nicely, but lags too much behind the data.

Actually, the mathematics/programming can be made even simpler if you assume that the process noise (q) and the sensor noise (r) don't change over time.  If you are convinced you have the right parameters, the Kalman Gain (k) will eventually stabilize to a single value.  You can pre-calculate what that value is and simplify the filter to:
x = x + k * (rssi_data – x);

For those also interested in Kalman filters used for RSSI data please read:

Step 7: Folding Your Pocket Square

Picture of Folding Your Pocket Square
Okay, now we're done with the construction and the data analysis.  Time to build the pocket square!
  1. To fold the patch into the pocket square, first lay the square out flat. 
  2. Place the patch upside-down in the upper-right-hand corner of the lower left quadrant, angling it so that the patch points toward the center of the square, with the lights at the top. 
  3. Fold the pocket square in half length-wise, then fold in half again, making a square that is one-quarter of the original size. 
  4. Now rotate this square 45 degrees (so that it looks like a diamond instead of a square) and fold the pointy sides in (like a burrito).  The patch should be nestled in the upper half of the burrito. 
  5. Lastly, fold the bottom half of the burrito up to meet the top.
  6. Turn the pocket square over--now the lights should show through the handkerchief and the folded pocket square should be ready to fit into your pocket!  (Blunt side down, pointy side up, lights facing out.)

Step 8: Adding Friends and Code

Picture of Adding Friends and Code

After you have completed one patch and square, simply add another, and another, and another.  For more and more, you will need more and more XBee Boards, more lights, more patches, more LilyPads, more pocket squares and more friends!

Lastly, I have attached the code I used for the project.  As always, lots of improvements can be made and hopefully I will get around to it tomorrow or the next day.


SeanY1 (author)2014-12-16

i dont understand what this does can somebody please explain to me?

quasiben (author)SeanY12014-12-18

Sure, a light is illuminated when it detects a neighbor within some proximity. For each neighbor, an additional light is illuminated up to 4

SeanY1 (author)quasiben2014-12-19

thanks, so 1 light is how many people? how many people does it take to illuminate 1 light? and how many to illuminate 4?

nerd7473 (author)2014-06-22


napok (author)2012-05-25

I tried to build a similar project on the basis of jewellery but when I tried to put your code into the Arduino IDE there were lots of errors.
Can you help me?
#include <XBee.h>
#include <NewSoftSerial.h>
#include "FilteringScheme.h"
It doesn't find either of the above.
I know there is now the Softwareserial instead of the newsoftserial, but that didn't help.
Do I have to paste the other files (except the .pde) also in somewhere?

omnibot (author)2011-04-22

Well, you know what they say;
-There's only 10 kinds of people, those who understand binary and those who don't.

quasiben (author)omnibot2011-04-22


DIY-Guy (author)quasiben2011-09-13

Could it be said that there are even 11 kinds of people? Those who don't know binary, those who do, and those who forget how to convert it in their heads?

MaXoR (author)omnibot2011-04-24

Lol, you made an old programmer smile today.

DesignerUserName (author)omnibot2011-04-24

This comment made my day.

jimmiek (author)omnibot2011-04-24

You sound like Rat from "The Core"

porcupinemamma (author)omnibot2011-04-24

hey hey whoo hoo I actually understood that joke. (epic for me guys)

ModMischief (author)2011-04-23

This is awesome.

My first thought: high-tech hanky code!

espdp2 (author)ModMischief2011-05-03

I've seen it before. Didn't have a CLUE that it meant anything. Thanks for the edu-ma-cation! :-)

Chakazuluu (author)2011-04-24

Hmm..possibly this can be used to attach to my keys & set of a buzzer when I get to far from them. I am always searching for my keys.

espdp2 (author)Chakazuluu2011-05-03

Triple-Like. This is genius! Maybe if I could use a Nano, cuz I hate big keyrings.

Rayney (author)2011-04-24

These would be great for a Con or some other crowded event! Depending on the range it would be helpful to find your friends.

quasiben (author)Rayney2011-04-25

The 1mW version of the XBee has a range of 300ft outdoors. The Pro line (60mW) can go up to a mile! I wanted the pocket square to only light when neighbors were close. If I didn't do this the lights would always be on.

Rayney (author)quasiben2011-04-25

Wow, I wouldn't need something with that kind of range unless I was going somewhere like DragonCon that ranges over several hotels in Atlanta.

quasiben (author)Rayney2011-04-25

Remember, that's 300ft outdoors where nothing blocks your signal. In doors, the range will be reduced; especially, with thick cos play costumes and humans inside of them

quasiben (author)Rayney2011-04-24

That's a great idea! You could have an LED array tied to distance for each of your friends!

Rayney (author)quasiben2011-04-25

Oooo, that would be great. It would be very helpful for someone like me, I am in a wheelchair and can't see over the crowds. Now I just wish I was savvy enough to actually make some lol

Sulaiman89 (author)2011-04-25

Wow that was a lot more complicated than it looks, impressive! What's the total price for each set and the individual prices for each part? Thanks

quasiben (author)Sulaiman892011-04-25

LilyPad Simple Board: $20 Lilypad LED x4: $4 XBee: $19 Custom XBee Board: $12 Pocket Square: $5-65 (depending on material) Total: $65 per Pocket Square (Assuming a $10 pocket square)

Rayney (author)quasiben2011-04-25

So it would be cheaper if you sew your own pocket squares. Actually, you don't even have to sew them, my dad made some using an iron on binder stuff (like StitchWitch) and some fabric that was on sale. In that case you could make many of them for less than $1 a piece. (Just the squares, not all the other stuff)

quasiben (author)Rayney2011-04-25

Yeah. You could possibly save on expense if you used a different wireless module, but that might introduce a whole new set of problems :)

brazell (author)2011-04-25

That rocks!!!

NaturalCrafter (author)2011-04-25

I really need to do a tech project. I love these ideas..Thanks.

imakethings (author)2011-04-24

good ...may be useful for security purposes!

maxiaxie (author)2011-04-24

whats the proximity on these bad boys? 5 metres? can you increase that?

porcupinemamma (author)2011-04-24

Totally out of my intellectual league. You must be so much fun as a guest at parties. Way to go!