Use a Wii Balance Board with Linux

May 22, 2009

in Gadgets/Hack

In this post you’ll learn how to make a working Bluetooth weight sensor + fast real-time graphical display with about 200 lines of Python code. You can code any Nintendo Wii-like video game for Ubuntu/Linux and the Nintendo Balance Board very easily. Here’s a video demonstration:

This post assumes that you can already communicate with a Wiimote on Linux. See my earlier post if you haven’t done that.

You’ll need Subversion and autoconf installed if you haven’t already installed it. I think the official/full list of packages to install is

sudo apt-get install autoconf autogen automake gcc bluetooth libbluetooth2-dev libgtk2.0-dev pkg-config python2.5-dev flex bison subversion

You must also install the development package for BlueZ, which is the official Linux Bluetooth protocol stack:

sudo apt-get install libbluetooth-dev

Make a directory to hold your code:

mkdir ~/wiibalance
cd ~/wiibalance

Check out the CWiid (get it? CWiid? Seaweed?) library using Subversion:

svn co http://abstrakraft.org/cwiid/svn/

When I checked CWiid out, it was up to version 183.

Compile the library, e.g.

cd ~/wiibalance/svn/trunk
autoconf
./configure --libdir=/usr/lib (according to this page, you need the libdir parameter on Ubuntu)
make
sudo make install

Now apply the patch to CWiid to add the Balance board. Go to http://abstrakraft.org/cwiid/ticket/63, click on balance.diff, then at the bottom of the resulting page, click on “Original Format” to download the diff as a raw text file. Do the same to download the “weighdemo.py” file. Now apply the balance.diff patch:

patch --dry-run -b -p0 < balance.diff (this command tests that everything would apply without errors)
patch -b -p0 < balance.diff

Finally, compile the code and install it:

make
sudo make install

Then you can play with the weighdemo.py script. You’ll want to change the line “sys.path.insert(0, ‘/home/tbble/code/cwiid/svn/cwiid/python/build/lib.linux-x86
_64-2.5/’)” to point to the right place on your system, e.g. /home/youraccountname/wiibalance/svn/trunk/python/build/lib.linux-i686-2.5/ . And you may need to disable the “if wiimote.state['ext_type'] != cwiid.EXT_BALANCE” if statement. I also added a few lines to print out the values and calibration numbers for the four sensors. Here’s what I get when I run my program:

$ ./weighdemo.py
Put Wiimote in discoverable mode now (press 1+2)…
Type q to quit, or anything else to report your weight

right_top 2249 [2293, 4004, 5725]
right_bottom 1467 [1449, 3155, 4874]
left_top 12471 [12476, 14238, 16015]
left_bottom 6822 [6848, 8581, 10325]
-0.58kg
Type q to quit, or anything else to report your weight

right_top 3618 [2293, 4004, 5725]
right_bottom 3983 [1449, 3155, 4874]
left_top 14208 [12476, 14238, 16015]
left_bottom 9621 [6848, 8581, 10325]
82.18kg

In this run, the first example is weighing with nothing on the scale. The second example is with me standing on the scale.

Here’s what you need to know. First, absorb this info from the Wikipedia entry:

Although the Japanese packaging states that it is designed to support people weighing up to 136 kilograms (300 pounds) and the “Western” Balance Board up to 150 kg (330 pounds), they are actually the same board. The packaging differs due to laws in Japan and other nations regarding weights and measures. While the board only displays weight readings up to what is printed on the packaging, the actual physical structure of the board can withstand much greater force equivalent to around 300 kg (660 pounds).

Okay, so the Wii Balance Board is certified for 300 pounds (136 kg) in Japan and 330 pounds (150 kg) in the U.S. The Wii Balance Board has four sensors, so each sensor is certified for up to 136 kg / 4 = 34 kg per sensor in Japan or 150 kg / 4 = 37.5kg per sensor in the United States. Now that you’ve been schooled on that, the following Wii Balance Board calibration information from WiiBrew will make more sense.

Each sensor returns 2 bytes of data and also has six bytes of calibration data. Think of it as a 16 bit number and three calibration numbers, that are also 16 bits apiece. The three calibration numbers correspond to the sensor reading for 0 kg, 17 kg, and 34 kg (those numbers should look familiar if you look at the previous paragraph). So if sensor #1 gives a value of 5725 and the 34 kg number is also 5725, then that sensor is reporting exactly 34 kg of weight on it.

The WiiBrew page says “Calculating the weight on each sensor simply involves interpolating between the two calibration values your reading falls between (or using the higher two values if your reading exceeds the highest calibration value), and the total weight on the board is the sum of these [four sensor] values.”

With that info, let’s go back to one of my sensor readings and convert it to actual kg. Take the reading “right_top 3618 [2293, 4004, 5725]“. The sensor value is 3618, which is between the 0 kg calibration number of 2293 and the 17 kg calibration number of 4004. So the weight on the right_top sensor is 17kg * (3618-2293)/(4004-2293) or 17kg * 0.7744 or 13.2 kg. I tend to put my weight on the back of my feet, which is why 13.2 kg is a little lower than my total weight (82 kg) divided by the four sensors, which would be 20.5 kg per sensor.

If your eyes haven’t completely glazed over, the upshot is that the Balance Board has four independent weight sensors, each calibrated to kilograms, and you can read those sensors in real-time over Bluetooth. Translating the sensor values into weight only takes four lines of Python code. From there, you can do anything you want with that data. Play a game, hook your Wii Balance Board up to Google Earth, or you can even automatically upload your weight to a Google Spreadsheet.

For example, you can run the command “sudo apt-get install python-pygame” to install pygame. Then go back to this page and download the scalesgui.py and system.ini files as you did before (click on them, then click on “Original Format” at the bottom of the resulting page). Make scalesgui.py executable with “chmod a+x scalesgui.py” and comment out the “EXT_BALANCE” if statement, then run “./scalesgui.py” and this is what you’ll see:

Scalesgui program

This is a snapshot, but the program runs just as fast as any video game. You can lean and move the circle around really fast. And in the bottom right, you can see that I weigh 81.74 kg, which is about 180 pounds.

I know this is a long post, but the upshot is that with 10-15 minutes of work, you can use a Wii Balance Board as a real-time sensor with Linux. I hope you have fun!

{ 31 comments… read them below or add one }

Matthew Swanson May 22, 2009 at 12:03 pm

Matt, you never cease to amaze me, GREAT STUFF! I love our WII FIT man.

Matt Cutts May 22, 2009 at 12:10 pm

I couldn’t believe that the Wii Balance Board is essentially just a Bluetooth device. It’s not hard to hook up on Linux, and it doesn’t take a lot of code hacking..

bob rains May 22, 2009 at 12:24 pm

Off-topic but great for a laugh on a Friday. Inspired by a reputation management blog post I was reading I went to Google and searched (Bob Rains Sucks) your blog is number 1.

Yeah, I LOL’d a little.

Brian White May 22, 2009 at 12:43 pm

You can get fit AND drive your cats nuts!

Matthew Swanson May 22, 2009 at 1:26 pm

Yeah, this could be a giant mouse…. imagine scrolling down on a site by leaning back on the balance board.. :)

Wes May 22, 2009 at 2:50 pm

Can Linux give your video camera some image stabilization?

Michael Martin May 22, 2009 at 4:05 pm

Are you going to demo that at Google IO next week? :)

I know you are presenting on Thursday but I will be covering the Android sessions and Maile’s panel on Wednesday before flying back to San Diego – will you be at Google IO Wednesday floating around?

Else I will see you at SMX Advanced the week after as you speak just after me that Tuesday.

,Michael Martin

djerba May 22, 2009 at 5:10 pm

Great :)
Inspiring tutorial ! Things are moving !
It would be a day when our children couldn’t use our boaring keyboards and mouses :)
I think Sensors applied on Google Sky Map application for Android are announcing new objectives for Google.

Thanks Matt

Matt Cutts May 22, 2009 at 10:17 pm

Michael Martin, I probably will be floating around on Wednesday. If you see me, say howdy!

Maurice May 23, 2009 at 3:25 am

I wonder if you could use this as diy controller for flying or racing games and i bet ther could be some interesting uses for live music performance if you hooked it up to Middi and used it to controll playback from somthing like Live.

of course we want to see video of your cats on teh board :-)

Tomas Hastings May 23, 2009 at 3:54 am

Matt, I’m not impressed until I see your cats playing Tuxracer using this as a controller ;)

Hans May 23, 2009 at 2:43 pm

Very cool,

Had not heard or thought about trying to connect Wii & Wii-stuff to Linux. Maybe I should try this if I get to take some vacation sometime.

Im rather positive surpriced of the balance board (got one some weeks ago). Good for the mood general and some morning practise positive affect the work I get done before lunch.

Joshua Ziering - Master Cock Blocker May 23, 2009 at 10:04 pm

Love the post. I just did a project with an iPhone and a linksys router to make a home brew RC rig. This is soo much cooler though because:

1) You can now make wii style games for linux.

2) Were one step closer to a bad ass flight sim for linux with working rudder pedals. Niceeee.

Josh

converter May 24, 2009 at 10:23 pm

This is too nerd…
I just love to use it with wii fit~

Remi van Beekum May 25, 2009 at 12:05 am

Didn’t you read the manual of the Balance Board?
You should always take your shoes off before stepping on the balance board.
And don’t jump on it ;-)

IT UK May 25, 2009 at 5:44 am

Wow.
I never thought you could connect a Wii to a Linux. Thanks!

MSG May 27, 2009 at 11:03 am

Has anyone successfully use cwiid to connect both a wiimote and a balance board simultaneously? Any ideas how? (I am getting socket connect error when I try to do that). I can connect to either wiimote or balance board separately though.

pengy May 27, 2009 at 12:50 pm

Excellent. Just got this working on my eeepc 901 running Ubuntu Jaunty. Any idea what I need to do next to make it control tuxracer ?

Joseph Morin May 30, 2009 at 2:05 pm

Matt you have way too much time on your hands. ;)

SEOMalc June 3, 2009 at 7:28 am

I’m presuming you have all seen http://www.youtube.com/watch?v=Jd3-eiid-Uw&feature=player_embeddedApparently

Looks like the Wii is destined for greatness.

MSG June 3, 2009 at 10:05 am

… never mind my previous comment. It was the bluetooth adapter’s fault. I bought the iogear bluetooth adapter and now I can connect to multiple remotes at the same time. Yeepee!

The little scale app is a nice example. Thanks. Btw, considering the 34kg threshold per sensor the center of mass will be inaccurate whenever any sensor(s) exceeds their pressure counts. One can go around fixing this, and still get an accurate center of mass position whenever ONE sensor exceeds its threshold. You should be able to derive the “faulty” sensor’s actual weight simply by knowing the other three sensor’s weights and the total (pre-scaled) weight. Say sensor 1 exceeds its 34kg threshold => the actual weight is w1 = w – (w2+w3+w4). You may be able to solve the weight distribution even for TWO sensors exceeding their thresholds (with some more complicated math and static mechanics). I don’t think you can do it with three or (obviously) four sensors exceeding their thresholds.

oniboy June 5, 2009 at 8:54 am

Pengy, ive been trying to get this to compile on Jaunty without any luck. Make keeps telling me that glib & gtk are missing but I know theyre not. How did you manage to get this installed ?

Richie June 8, 2009 at 3:58 am

Good old Wii Fit board! Great for weighing your baggage before you go away on you holidays so you don’t get charged for over weight baggage! Not sure about getting it hooked up to my Linux computer though, bit OTT!!!

Donal July 4, 2009 at 5:47 am

Hi When I run autoconf I get the following error (on Ubuntu 9.04 Jaunty Jackalope):

~/wiibalance/svn/trunk$ ./configure –libdir=/usr/lib
checking for gcc… gcc
checking for C compiler default output file name… a.out
checking whether the C compiler works… yes
checking whether we are cross compiling… no
checking for suffix of executables…
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ISO C89… none needed
checking for gawk… gawk
checking for flex… flex
checking lex output file root… lex.yy
checking lex library… -lfl
checking whether yytext is a pointer… yes
checking for bison… bison -y
checking for python… python
checking for pthread_create in -lpthread… yes
checking for hci_devid in -lbluetooth… yes
checking for dlopen in -ldl… yes
checking how to run the C preprocessor… gcc -E
checking for grep that handles long lines and -e… /bin/grep
checking for egrep… /bin/grep -E
checking for ANSI C header files… yes
checking for sys/types.h… yes
checking for sys/stat.h… yes
checking for stdlib.h… yes
checking for string.h… yes
checking for memory.h… yes
checking for strings.h… yes
checking for inttypes.h… yes
checking for stdint.h… yes
checking for unistd.h… yes
checking for stdint.h… (cached) yes
checking bluetooth/bluetooth.h usability… yes
checking bluetooth/bluetooth.h presence… yes
checking for bluetooth/bluetooth.h… yes
checking for bluetooth/l2cap.h… yes
checking for bluetooth/hci.h… yes
checking linux/input.h usability… yes
checking linux/input.h presence… yes
checking for linux/input.h… yes
checking for linux/uinput.h… yes
checking for library containing strerror… none required
./configure: line 4678: syntax error near unexpected token `GTK,’
./configure: line 4678: `PKG_CHECK_MODULES(GTK, $pkg_modules)’

Can anyone help
Thanks
Donal

Dylan Tweney July 13, 2009 at 4:37 pm

Matt, this is such a cool hack that we’d like to incorporate it into Wired’s “beer robot” project — a homemade kegerator that we want to enhance with Wired-worthy digital interactivity. We’re thinking a Wii balance board underneath the keg might be just the thing to help drive a Twitter account that could alert us if it’s running low, for instance.

If you — or anyone reading this blog — are interested in helping us in exchange for free beer (seriously!), please drop me a line: wired (at) tweney.com.

Lennart Regebro July 15, 2009 at 1:50 am

Donal: Make sure all packages are installed, then run autoreconf. Note the re, running autoconf again won’t help. :)

Matt Wallace July 26, 2009 at 4:39 pm

Hi, This is an excellent post and I’ve spent the last few hours thinging about the fun I can have with this!

Unfortunately when I run the scalesgui.py script, I get the following error:

~/wii$ ./scalesgui.py
Please press the red 'connect' button on the balance board, inside the battery compartment.
Do not step on the balance board.
Traceback (most recent call last):
File "./scalesgui.py", line 96, in
wiimote.rpt_mode = cwiid.RPT_BALANCE | cwiid.RPT_BTN
AttributeError: 'module' object has no attribute 'RPT_BALANCE'

wmgui and wmimput work fine for the wiimote, I just can’t get the board working!

Thanks in advance for any help,

M.

Oscar Rosales August 28, 2009 at 4:11 pm

I have the same problem using kubuntu and Wii using weighdemo.py :

Put Wiimote in discoverable mode now (press 1+2)…
Traceback (most recent call last):
File “/home/orosales/Escritorio/weighdemo.py”, line 59, in
sys.exit(main())
File “/home/orosales/Escritorio/weighdemo.py”, line 16, in main
wiimote.rpt_mode = cwiid.RPT_BALANCE | cwiid.RPT_BTN
AttributeError: ‘module’ object has no attribute ‘RPT_BALANCE’

What can you solve this?

praxis September 7, 2009 at 2:56 pm

For people with this error:


...
./configure: line 4678: syntax error near unexpected token `GTK,’
./configure: line 4678: `PKG_CHECK_MODULES(GTK, $pkg_modules)’

I solved it by installing libtool, and running autoreconf and ./configure

sudo apt-get install libtool
autoreconf
./configure --libdir=/usr/lib

ayllu September 13, 2009 at 1:01 am

Great job!!! I think will be a great idea that linux gamer developers can make new games using wii hardware

ayllu September 20, 2009 at 4:17 pm

you know if it work whit dolphin, de wii emulator

Leave a Comment

If you have a question about your site specifically or a general question about search, your best bet is to post in our Webmaster Help Forum linked from http://google.com/webmasters

If you comment, please use your personal name, not your business name. Business names can sound salesy or spammy, and I would like to try people leaving their actual name instead.

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: What you should do next week

Next post: I’m back!