Compass: Difference between revisions
Jump to navigation
Jump to search
(Added code sample) |
|||
Line 1: | Line 1: | ||
== Components == | == Components == | ||
[[File:DC-Boarduino.jpg|thumb]] | [[File:DC-Boarduino.jpg|thumb]] | ||
Line 7: | Line 6: | ||
* Compass Module CMPS03 http://www.robot-electronics.co.uk/htm/cmps3tech.htm | * Compass Module CMPS03 http://www.robot-electronics.co.uk/htm/cmps3tech.htm | ||
* DC Boarduino http://learn.adafruit.com/boarduino-kits/dc-parts-list | * DC Boarduino http://learn.adafruit.com/boarduino-kits/dc-parts-list | ||
* FTDI Friend http://www.adafruit.com/products/284 | |||
Other potential components | |||
* SCF5740 Siemens (OSRAM) 4 digit display http://catalog.osram-os.com/media/_en/Graphics/00034126_0.pdf | |||
* 0.96" OLED Display https://www.adafruit.com/products/684 | * 0.96" OLED Display https://www.adafruit.com/products/684 | ||
* FTDI/USB http://www.parallax.com/catalog/integrated-circuits/ftdi | * FTDI/USB http://www.parallax.com/catalog/integrated-circuits/ftdi | ||
== Code == | |||
The following will give a compass heading via serial monitor | |||
<pre> | |||
/* | |||
CMPS03 with arduino I2C example | |||
This will display a value of 0 - 359 for a full rotation of the compass. | |||
The SDA line is on analog pin 4 of the arduino and is connected to pin 3 of the CMPS03. | |||
The SCL line is on analog pin 5 of the arduino and is conected to pin 2 of the CMPS03. | |||
Both SDA and SCL are also connected to the +5v via a couple of 1k8 resistors. | |||
A switch to callibrate the CMPS03 can be connected between pin 6 of the CMPS03 and the ground. | |||
*/ | |||
#include <Wire.h> | |||
#define ADDRESS 0x60 //defines address of compass | |||
void setup(){ | |||
Wire.begin(); //conects I2C | |||
Serial.begin(9600); | |||
} | |||
void loop(){ | |||
byte highByte; | |||
byte lowByte; | |||
Wire.beginTransmission(ADDRESS); //starts communication with cmps03 | |||
Wire.write(2); //Sends the register we wish to read | |||
Wire.endTransmission(); | |||
Wire.requestFrom(ADDRESS, 2); //requests high byte | |||
while(Wire.available() < 2); //while there is a byte to receive | |||
highByte = Wire.read(); //reads the byte as an integer | |||
lowByte = Wire.read(); | |||
int bearing = ((highByte<<8)+lowByte)/10; | |||
Serial.println(bearing); | |||
delay(100); | |||
} | |||
</pre> |
Revision as of 23:28, 16 December 2013
Components
- Compass Module CMPS03 http://www.robot-electronics.co.uk/htm/cmps3tech.htm
- DC Boarduino http://learn.adafruit.com/boarduino-kits/dc-parts-list
- FTDI Friend http://www.adafruit.com/products/284
Other potential components
- SCF5740 Siemens (OSRAM) 4 digit display http://catalog.osram-os.com/media/_en/Graphics/00034126_0.pdf
- 0.96" OLED Display https://www.adafruit.com/products/684
- FTDI/USB http://www.parallax.com/catalog/integrated-circuits/ftdi
Code
The following will give a compass heading via serial monitor
/* CMPS03 with arduino I2C example This will display a value of 0 - 359 for a full rotation of the compass. The SDA line is on analog pin 4 of the arduino and is connected to pin 3 of the CMPS03. The SCL line is on analog pin 5 of the arduino and is conected to pin 2 of the CMPS03. Both SDA and SCL are also connected to the +5v via a couple of 1k8 resistors. A switch to callibrate the CMPS03 can be connected between pin 6 of the CMPS03 and the ground. */ #include <Wire.h> #define ADDRESS 0x60 //defines address of compass void setup(){ Wire.begin(); //conects I2C Serial.begin(9600); } void loop(){ byte highByte; byte lowByte; Wire.beginTransmission(ADDRESS); //starts communication with cmps03 Wire.write(2); //Sends the register we wish to read Wire.endTransmission(); Wire.requestFrom(ADDRESS, 2); //requests high byte while(Wire.available() < 2); //while there is a byte to receive highByte = Wire.read(); //reads the byte as an integer lowByte = Wire.read(); int bearing = ((highByte<<8)+lowByte)/10; Serial.println(bearing); delay(100); }