You are on page 1of 16

ReportTechnologyMasterclassMartijnJansen

AnOpticalMouseSensoras
anextensionforLEGONXT

MartijnJansen
m.e.p.jansen@student.tue.nl
S030887
Index:

1.Introduction

2.SensorChoice

3.Hackingtheopticalmouse

4.HackingLEGONXT

5.Implementation

6.PossibleApplications

7.References

8.Appendix
1.Introduction

AssignmentDescription
ThisprojectispartoftheTechnologyMasterclassofIndustrialDesignattheTechnicalUniversity
Eindhoven.Duringthisclass,Masterstudentsgettheopportunitytodiveintosomeverydiverse
elementsoftechnologylikeelectronics,mechanics,programmingintelligentrobotsandcreatinga
newinnovativesensorforLEGONXT.Thisreportdescribestheelaborationofthelatterassignment.

Deliverables
workingmodeloftheextensionpack
reportinMSWorddescribingtheextensionpack
technicaldocumentationoftheextensionpack,suchastechnicaldrawingsandUMLmodels
photosoftheextensionpack
movieoftheextensionpackinactionuploadedinhighqualitytoSurfMediaandavailablefor
downloadfromthere.

2.SensorChoice

Forarobotthatisabletomoveitself,itisessentialtoknowinwhatenvironmentitisandwherein
thatenvironmentitislocated.Location,distanceandspeedarevariablesthatcanhelparobot
determineitscurrentstate.AGPSsensorisabletogivethesevariables,butarequiteexpensiveand
notasaccurateasyoumightneed.Acomputermouseismoreaccurateandcandetectmovement
smallerthanamillimeter.ForthisreasonIdecidedtolookatcomputermiceasanewsensorfor
LEGONXT.

Anoldcomputermouse(fig.1)detectsmovementmechanicallythroughaballinsidethatcanrotate
inanydirection.Therearethreewheelsinsidethatarepositionedagainsttheball,sothatthewheels
areturningwhentheballrolls.Thesewheelschoplightbeamsinfrontoflightsensorssoa
movement,speedofmovementanddirectioncanbedetected.Thiskindofmouseisalsocalledaball
mouse.

1

Fig.1:Aballmouse

Adifferentkindofcomputermouseistheopticalmouse(fig.2).Theopticalmousehasa
sensorinsidethatisactuallyaverysmallcamera.Itdetectschangesinpicturesofthesurface
underneathit.ThisisthenconvertedtoanamountofmovementintotheXorYdirection.AredLED
illuminatesthesurfacetoincreasethecontrastofthepictures.


fig.2:Myopticalmouse(beforethisproject)

Ichosetogofurtherwiththeopticalmousesensorforthefollowingreasons:
ithasnomechanicalpartsthatcanbreakorwearoff;
itcanbeusedonmoresurfacesthantheballmouse;
aballmousecollectsdebrisinsidethecasing
itsquitesmall(smallerthantheballmouse);
itscheap;
Iseealotoflearningopportunitieshere.

ThefollowingchapterswilldescribehowtocreateanopticalmousesensorfortheNXTfromacheap
opticalmouse.
3.Hackingtheopticalmouse

2,3
IhaveusedsometutorialsIfoundoninternet .Myapproachwasquitesimilar,buteventuallyIneed
toreadthedatawiththeLEGONXT.Myfirststepistohackthemousereadthedatafromthesensor
4
withArduino .

Tutorialforhackingtheopticalmouse
Thistutorialwillexplainwhatstepsyouneedtotaketohacktheopticalmouseandreadthedata
withArduino.Firsttakeanoldopticalmouseorbuyacheapone.Fig.2showshowmymouselooked
like.

1.Openupthemouse
Underneaththemousetheresascrewyouneedtounbolt.Thiswillopenupthemouse.Hereyou
findthecircuitboardwiththesensor(thechipinthemiddle),aLED,buttonsandthescrollwheel(fig.
3).Underneaththecircuitboardisthelensclip(fig.4),thatisusedtoamplifythelightandfunctions
asalensforthesensor.


fig.3:Mouseopenedup


fig.4:lensclip

2.Lookupthedatasheet
Thesensorislocatedinthemiddleofthecircuitboard.Searchforthedatasheetofthesensoronthe
internetandlookupthepinconnections.Youneedthefollowingpins:
+5V(inputvoltage)
GND(ground)
SCK(serialportclock)
SDIO(serialportdata)
InthecaseofmysensorIneedpin3,4,6and7(fig.5)

5

fig.5mechanicaldrawing+pinouttableofthedatasheet

MakesurethatyoursensordoesnthavetheUSBcontrollerembeddedinsidethechip.Thefirst
mouseItriedtoopenhadthiskindofchip(seefig.6),whichmeansthatyoucantcapturethedata
withoutaUSBprotocol.Usinga2wireserialportcommunicationismuchmorepreferable.


fig.6:amousewiththeUSBcontrollerembeddedinthechipofthesensor

3.Solderwires
Solderwirestothe4neededpins.Itsusefultochoosedifferentcolors,soyoureabletodistinguish
theclockanddatalineseasily(fig.7).


fig.7Wiressolderedtopins3,4,6and7

4.RemovetheUSBcontroller
Thisstepisnotnecessary,butifyourenotgoingtousethemouseanymoreitmightbebetterto
removetheUSBcontrollertopreventthecontrolleroftakingoverthedataconnection.Removing
thecontrollercanbedonebyremovingthesolderedtinandscrapethecontrollerofthecircuitboard.

5.ConnectittoArduino
ChoosetwodigitalpinsontheArduinofortheclocklineandthedatalineandconnectthe+5Vand
GNDofthemousetothe5VandGndofArduino.Uploadthesketches(programsforArduino)
providedbyMartijnThbasedonsketchesofBenotRousseau.MakesurethattherightregistersIf
youopentheSerialmonitorintheArduinosoftwareyoullseethexandyvalues.Inthenext
paragraphIllexplainhowthisworks.
GetthedatafromtheopticalmousetoArduino

Inthedatasheetofthesensorthereisawholesectionaboutthesynchronousserialport.Thisportis
usedtosetandreadparametersfromthesensor.Theportusestwowiresandisahalfduplexport,
whichmeansthatitcantalkbothways,butnotatthesametime.Themicrocontroller,inmycasethe
Arduino,alwaysinitiatescommunicationandiscalledthemaster(thesensoristheslave).Thetwo
wiresaretheserialportclock(SCK)andthedataline(SDIO).Theserialportclockisgeneratedbythe
microcontroller.

ThemicrocontrollercantrytoreadparametersliketheXandYmovement,butcanalsowritetothe
sensortochangeparameterslikeforexampletheconfigurationofthesensor.Forthisweneedaread
andawriteoperation.Theclocklineisgeneratedbythemicrocontrollerandispreferablypulsingata
lengthof250nsperpulse.Thefunctionoftheclockistosynchronizedatatransfers.

Writeoperation
Awriteoperationisinitiatedbythemicrocontrollerandconsistsoftwobytes.Thefirstbytecontains
a7bitaddressandhasa1asitsmostsignificantbit(MSB).This1meansthatthemicrocontroller
wantstowritedataandtheaddressisaregisterwhereyouwanttowriteto.Thesecondbyte
containsthedatayouwanttowrite.Theclocksynchronizesthedatatransfer,becausethe
microcontrollerchangesSDIOonfallingedgesoftheclockandthesensorreadsSDIOonrisingedges
oftheclock.


fig.8:Writeoperation

ReadOperation
Awriteoperationworkskindofthesame;italsoconsistsoftwobytes.Thefirstbytehasa0asMSB
(toindicateareadoperation)andasevenbitaddresstorequestthedatafromacertainregister.The
secondbyteisthedatasendfromthesensortothemicrocontroller.Afterthelastbitoftheaddress
thereisadelayof100usforthesensortopreparethedata.


fig.9:ReadOperation

Registers
Inthedatasheetthereisalistofaddresses(fig.10).Fromaddress0x42and0x43youcangetthe
Delta_YandDelta_X(XandYmovement).Letmegiveanexample:IfIwanttoreadtheYmovement
Ihavetoperformareadoperation.Thefirstbytethemicrocontrollersendsis0x42,butwitha0as
MSBbit.Inbitformthislookslikethis:01000010.Thenthereisadelayof100microsecondsand
afterthatthesensorwillgiveabytecontainingthemovementofthesensorintheYdirection.

fig.10:AddressesoftheregistersoftheADNS2083

InthenextchapterIllexplainhowtogetthisdatatotheLEGONXT.
4.HackingLEGONXT

TheNXTisactuallyamicrocontrollerlikeArduino.ForthisassignmentweneedtoprogramtheNXT
withleJOS,aJAVAbasedlanguagefortheNXT.Icouldntfigureouthowtoreadthesensordirectly
withtheNXTusingleJOS.ThereforeIchosetotoreaddatafromthesensorwithArduinoandletthe
NXTcommunicatewiththeArduino.

LEGONXTcables:
ThecablesoftheNXTareactuallyslightlymodifiedJR12cables(telephonecables).Themodification
liesintheconnector.Insidethecableare6wiresthatconnecttoaninputportoftheNXT(fig.12).

6

fig.12:InputportoftheNXT

CommunicationbetweenLegoNXTandArduinothroughI2C:
I2CisaprotocoldevelopedbyPhilips.Theprotocolofreadingtheopticalmousesensorisquitesimilar
tothis,butnotentirely.Inthecaseoftheopticalmousesensor,thereisnodeviceaddress.Youneed
thistostartupthewholecommunication.ForthisreasonIcouldntreadthesensordirectlyfromthe
NXT.

Inmyproject,theArduinoistheslaveandtheNXTisthemaster.InArduinotheresalibrarycalled
2
Wire.hthatcanbeusedforI C.Withthislibraryyoucansetthedeviceaddress,Iusedthenumber
127.WhenevertheNXTrequestsdata,Iusethislibrarytosendanarrayoftwobytes;thexandy
value.

InleJOSIrequestdatabyusingtheI2CSensorclass.Icreatedaclassfortheopticalmousesensorthat
extendstheI2CSensorclass.NowIcanmakeseveralobjectsofthisclass,whichcanbeusefulwhenI
wanttousemorethanoneofthesesensorsatthesametime.
AftertheNXThasinitializedcommunicationwithdeviceaddress127,itcanrequestdata.Afterthis
requestIgetaresponseoftwobytes,whichIstoreinanarraywithtwoindexes.Thefirstindexof
thearrayisthexvalueandthesecondistheyvalue.
5.Implementation

Thischapterdescribestheimplementationintoaworkingprototype.

Circuitblockdiagram
Thecircuitblockdiagram(fig.13)islargelybasedonthecircuitblockdiagramoftheADNS2083.By
removingtheunnecessarypartsliketheUSBcontrollerandthebuttonsyoullhaveablockdiagramof
thesensorwiththeLED.Connectthefourpins(+5V,GND,SCKandSDIO)ofthesensortotheright
2
pinsontheArduinoboard.ConnecttheI CpinsoftheArduinoboard(SCK=ANALOG5,SDIO=
ANALOG4)totheNXTcable.Theotherendofthecablecanbeinsertedintoanysensorportofthe
2
NXT,exceptsensorport4(isnotcapableofI C).Iusedsensorport1.


Fig.13:CircuitblockdiagramforreadingthesensorinNXT(rotateyourpage)
MechanicalDrawing
IwantedtomakeasensorhasalmostthesameformastheexistingNXTsensors.Thefirstdrawings
(fig.14)giveagoodimpressionofthedimensionsoftheouterbody.


fig.14:Firstimpressionofthedimensions

AfterImadethisIstartedtocreatea3Dmodelfor3Dprinting.Thethicknessofthematerialis2mm.
Insidethecasingadifferentconstructionwasneeded.Thecircuitboardhadtobeexactly7.45mm
abovethesurface(fig.15).ThemouseIhackedusedadifferentlensclipthanthedatasheet
recommended.InmydesignIneededtofixthelensclipbymakingaraisedrectangleandcutouta
passageforthelighttogothroughandapassageforthelenstomakepictures(justlikeinsidethe
casingofthemouse,fig.16).Theimplementationisshowninfig.17andtheeventualmodelinfig.18.


fig.15:Assemblydrawingofcircuitboardinsideview

fig.16:Lensclipholderinthemousecasing


fig.17:Twoscreenshotsofthe3DCADmodelmadeinSolidWorks


fig.18:Theprototype3Dprintedwiththetechnologyinside
Nextsteps
Ivetriedoutthesensorondifferentsurfacesanditworksverywellonwhiteorbrightsurfaces.On
darkorblacksurfacesithasmoretroubletoswitchon.Thereisasettingofthemousesensorthat
switchestheLEDtolessbrightwhenitisntmovedformorethanasecond.Whenthesensordetects
movement,theLEDwillgetbrighteragain.Thisisasettingtosaveenergy.Theproblemondark
surfacesisthatthesensordoesntdetectthefirstmovementverywell,sotheLEDwillnotemitatfull
power.Incaseofemittingwithfullpowerthesensorworksfineagain.Iwouldrecommendwhenthe
sensorisusedondarksurfacestochangetheconfigurationofthesensorandlettheLEDalwayson
withfullpower.

Therearealsosomeminorimprovementsthatcanbemadetothedesignofthecasing.Thelensclip
onlyfitswhenitisputinwithalotofforce.Tryingalitbiggermarginwouldbebetter.

Asmallercircuitboardcanbedesignedwiththetechnicaldrawingprovidedinthisreport.Thismeans
thatthesensorwoulddecreaseinsizeandmightevendecreaseitsbroadthtwice.
6.PossibleApplications

SomeexamplesofapplicationsusingtheNXTopticalmousesensorare:

Measuremovement
Arobotcanmeasurehowfarithasdrivenyet.

Measurevelocity
Forrobotsthathavetogowithacertainspeedyoucandividethemovementwiththetimeandyou
getvelocity

Feedbackloopfordrivingstraight
BecausethesensormeasuresXandYvaluesitcanalsogivefeedbackaboutmovementsideways.This
isunwantedforexamplebecauseonmotorisstrongerthantheother.Becauseyoudetectthe
movementsidewaysyoucanfeedthisinformationbacktothemotors(justlikethepotentiometerin
aservomotor).

Drivingtocoordinates
Arobotcandrivetoanexactpointintheenvironment,whenitkeepstrackofthemovementsofar
andthebendsithasmade.
7.References

1. Picturefrom:
http://upload.wikimedia.org/wikipedia/commons/2/2c/Ball_Mouse_semiinside_view.jpg
2. http://www.gurulib.com/_project/optical_mouse_hack_files/optical_mouse_hack.htm
3. http://www.martijnthe.nl/optimouse/
4. http://www.arduino.cc/
5. DatasheetADNS2083(SDNS2083)fromAvagotechnologies
6. LegoMindstormsNXTHardwareDelopmentKit

8.Appendix

A.DatasheetADNS2083

B.LegoMindstormsNXTHardwareDelopmentKit

C.ArduinoSketch

D.leJOSclasses

E.Videooftheworkingprototype

BecauseAppendixC&Dcontaintoomanypagestoprintandaremoreusefulto
havedigital,Ihavemadethemavailableonline.AppendixEisavideoandcanbe
downloaded.

C.http://www.kansloosdesign.nl/portfolio/wp
content/uploads/2008/12/coordinates_in_mm_i2c.zip

D.http://www.kansloosdesign.nl/portfolio/wp
content/uploads/2008/12/lejosopticalmousesensor.zip

E.http://www.kansloosdesign.nl/portfolio/wp
content/uploads/2008/12/filmpje.m4v