{"id":9,"date":"2007-03-06T21:09:56","date_gmt":"2007-03-07T02:09:56","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/?p=9"},"modified":"2007-08-07T09:21:59","modified_gmt":"2007-08-07T14:21:59","slug":"devantech-cmps03-digital-compass-reader","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/9\/","title":{"rendered":"Devantech CMPS03 digital compass reader"},"content":{"rendered":"<p>This code reads a Devantech CMPS03 digital compass on Wiring or Arduino using the Wire library for I2C.<br \/>\n<!--more--><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.tigoe.com\/pcomp\/code2\/wp-content\/uploads\/2007\/08\/cmps03_arduino_schem.png\" alt=\"CMPS03 and Arduino Schematic\" \/><\/p>\n<pre>\r\n\/*\r\n  CMPS03 compass reader\r\n language: Wiring\/Arduino\r\n\r\n Reads data from a Devantech CMP03 compass sensor.\r\n\r\n Sensor connections:\r\n SDA - Analog pin 4\r\n SCL - Analog pin 5\r\n\r\n created 5 Mar. 2007\r\n by Tom Igoe\r\n\r\n *\/\r\n\r\n\/\/ include Wire library to read and write I2C commands:\r\n#include <wire.h>\r\n\r\n\/\/ the commands needed for the SRF sensors:\r\n#define sensorAddress 0x60\r\n\/\/ this is the memory register in the sensor that contains the result:\r\n#define resultRegister 0x02\r\n\r\nvoid setup()\r\n{\r\n  \/\/ start the I2C bus\r\n  Wire.begin();\r\n  \/\/ open the serial port:\r\n  Serial.begin(9600);\r\n}\r\n\r\nvoid loop()\r\n{\r\n\r\n  \/\/ send the command to read the result in inches:\r\n  setRegister(sensorAddress, resultRegister);\r\n    \/\/ read the result:\r\n  int bearing = readData(sensorAddress, 2);\r\n  \/\/ print it:\r\n  Serial.print(\"bearing: \");\r\n  Serial.print(bearing\/10);\r\n  Serial.println(\" degrees\");\r\n  \/\/ wait before next reading:\r\n  delay(70);\r\n}\r\n\r\n\/*\r\n  setRegister() tells the SRF sensor to change the address pointer position\r\n *\/\r\nvoid setRegister(int address, int thisRegister) {\r\n  \/\/ start I2C transmission:\r\n  Wire.beginTransmission(address);\r\n  \/\/ send address to read from:\r\n  Wire.send(thisRegister);\r\n  \/\/ end I2C transmission:\r\n  Wire.endTransmission();\r\n}\r\n\/*\r\nreadData() returns a result from the SRF sensor\r\n *\/\r\n\r\nint readData(int address, int numBytes) {\r\n  int result = 0;        \/\/ the result is two bytes long\r\n\r\n  \/\/ send I2C request for data:\r\n  Wire.requestFrom(address, numBytes);\r\n  \/\/ wait for two bytes to return:\r\n  while (Wire.available() &lt; 2 )   {\r\n    \/\/ wait for result\r\n  }\r\n  \/\/ read the two bytes, and combine them into one int:\r\n  result = Wire.receive() * 256;\r\n  result = result + Wire.receive();\r\n  \/\/ return the result:\r\n  return result;\r\n}\r\n<\/wire.h><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code reads a Devantech CMPS03 digital compass on Wiring or Arduino using the Wire library for I2C.  \/* CMPS03 compass reader language: Wiring\/Arduino Reads data from a Devantech CMP03 compass sensor.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}