Skip to content

Instantly share code, notes, and snippets.

@adamfowleruk
Created July 14, 2014 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamfowleruk/370144fc7343217d7c82 to your computer and use it in GitHub Desktop.
Save adamfowleruk/370144fc7343217d7c82 to your computer and use it in GitHub Desktop.
Sending GPS and Compass information to hardware serial on Arduino Pro Mini with UBlox GPS and HMC5883L compass chip (Airbot UBlox 2.0 module)
#include <nmea.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial mySerial(2, 3); // RX, TX
NMEA nmeaDecoder(ALL);
void setup() {
// Open serial communications and wait for port to open:
Wire.begin();
// instruct chip we want to read it continuously (TODO: REPLACE WITH BETTER POWER SAVING MODE)
Wire.beginTransmission(0x1e); // transmit to device 0x1e
Wire.write(0x02); // sends one byte (select register to write to - MODE)
Wire.write(0x00); // sends one byte (configure mode - continuous)
Wire.endTransmission();
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Tracker has initialised");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() { // run over and over
if (mySerial.available()) {
if (nmeaDecoder.decode(mySerial.read())) {
Serial.print("Lon: ");
Serial.print(nmeaDecoder.gprmc_longitude());
Serial.print(", Lat: ");
Serial.print(nmeaDecoder.gprmc_latitude());
Serial.print(", Speed: ");
Serial.print(nmeaDecoder.gprmc_speed(KMPH));
Serial.print(", Course: ");
Serial.println(nmeaDecoder.gprmc_course());
// Now take a compass reading to supplement GPS information
int x,y,z; //triple axis data
//Tell the HMC5883L where to begin reading data
Wire.beginTransmission(0x1e);
Wire.write(0x03); //select register 3, X MSB register - to start read from
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(0x1e, 6);
if (6 <= Wire.available()) {
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
//Print out values of each axis
Serial.print("COMPASS: x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
}
}
//delay(1000); // DO NOT USE THIS - messes up software serial library
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment