Saturday, July 5, 2014

Basic: HMC5883L (Digital Compass)

Hi, everyone. I am actually waiting for the time for breakfast. I'm writing this post to take my mind off the meal. So, this time, it's about a digital compass; HMC5883L which I bought from GI Electronic.


When I first played around with it, it was quite complicated, but soon enough, I manage to understand most of the important part of it. I wrote a library to make it even simpler to use, which can be found here or here.

Introduction

This module is actually a digital compass, which probably means that you've understand what it should do. It is a compass, which measures the magnetic strength around it in 3 axis; X, Y and Z. This module is using I2C communication. It is a famous type of communication and I really like it as it basically uses only 2 wires, SDA and SCL, and multiple devices could be connected on the same pin at the same time.

Connection

In the following diagram, notice that the pins listed aren't really accurate as the sketch is using a product by Adafruit, not the one sold by GI Electronic. The pins are equivalent as:

VIN = VCC
GND = GND
3vo = -
SDA = SDA
SCL = SCL
RDY = DRDY




Code

The following code simply prints the output or the degree based on X and Y axis.

#include <HMC5883L.h>
#include <Wire.h>

HMC5883L compass;

void setup(){
  Serial.begin(19200);
  compass.init();
}

void loop(){
  if(compass.available()) {
    Serial.println(compass.getHeading());
  }

  delay(100);
}

Important Note

By observing the serial monitor, turn the digital compass until the value move closer to 0 or 360. When you manage to get that value, you are actually finding the magnetic north. However, relying on the magnetic north alone is unwise especially when it comes to navigational use. At long distance, 1 degree difference could end up quite far. Be sure to deduct or add the declination angle according to where you are situated at to get the true north.

Look for your declination angle here: http://magnetic-declination.com/

About the library


Constructor (HMC5883L)
  • Prototype:
    • HMC5883L::HMC5883L(int address = 30);
  • Description:
    • Create the HMC5883L object and set the address of the module.
  • Arguments:
    • address: The address of the module with default value of 30 or 0x1E.
  • Returns:
    • None
  • Usage:
    • Outside any function. (Global)


init
  • Prototype:
    • void HMC5883L::init();
  • Description:
    • Begin the data transmission and selecting the continuous measurement mode.
  • Arguments:
    • None
  • Returns:
    • Void
  • Usage:
    • Inside setup() function.

available

  • Prototype:
    • bool HMC5883L::available();
  • Description:
    • Check if the data is available.
  • Arguments:
    • None
  • Returns:
    • true: if data is available.
    • false: if data is not available.
  • Usage:
    • Inside if block.

getHeading

  • Prototype:
    • double HMC5883L::getHeading();
  • Description:
    • Get the angle from North calculated between X and Y axis.
  • Arguments:
    • None
  • Returns:
    • Angle calculated between X and Y axis.
  • Usage:
    • Inside if block after ensuring data availability.

getAngleXY

  • Prototype:
    • double HMC5883L::getAngleXY();
  • Description:
    • Get the angle from North calculated between X and Y axis.
  • Arguments:
    • None
  • Returns:
    • Angle calculated between X and Y axis.
  • Usage:
    • Inside if block after ensuring data availability.

getAngleYZ

  • Prototype:
    • double HMC5883L::getAngleYZ();
  • Description:
    • Get the angle from North calculated between Y and Z axis.
  • Arguments:
    • None
  • Returns:
    • Angle calculated between Y and Z axis.
  • Usage:
    • Inside if block after ensuring data availability.

getAngleXZ

  • Prototype:
    • double HMC5883L::getAngleXZ();
  • Description:
    • Get the angle from North calculated between X and Z axis.
  • Arguments:
    • None
  • Returns:
    • Angle calculated between X and Z axis.
  • Usage:
    • Inside if block after ensuring data availability.


Intermediate: QC12864B (128x64 LCD using u8glib.h)

Hi, everyone. Today, I have some time to kill. So, I decided to play around with my 128x64 pixels LCD. This is also a product distributed by GI Electronic which can be found here.


There are some sample code written in C attached on the product page, but for me, it's not very helpful as I'm more familiar with Arduino. So, this tutorial will be about interfacing Arduino and the LCD.

Introduction

This LCD comes with build-in ST7920 controller chip which will handle the data sent to it. The attached user manual somewhat tells you how it should work and what command you should send, but it is still blurry, isn't it? It's just look too complicated. But, there's an easy solution for that. An external library makes our life so much easier. The library is called U8glib and it supports wide range of LCD.  However, this tutorial will focus on this LCD only. You can see the list of supported LCD here.

Usually, you need to send 8 bits of data to make the LCD display what you want it to display. So, basically, there'll be 8 wires going from Arduino to the LCD. Imagine how complicated that would look like. However, here's the sweetest part of all. The library allows you to program the LCD using 3 methods.

1. Using 8 bits of data as mentioned above. (Parallel Bus Mode)
2. Using hardware/built-in SPI port on Arduino. (Serial Bus Mode)
3. Using software SPI port.

Personally, I'd choose the second method in case where the SPI ports are available. It uses less port, making the connection far simpler and also leave the other ports available for other applications. For your information, SPI ports are on pin 10 (Slave Select), 11 (MOSI), 12 (MISO), 13 (SCK) on Arduino Nano and Arduino Uno. Using the software SPI port may slow down the refresh rate.

Connection

This product comes with 20x1 header, which really helpful to shove the LCD into a breadboard. I'll show you how to connect using the first and second methods as mentioned above. The third one is almost the same as the second one, but it is actually depends on what port you want to use.

Note that the pin in the following photo is equivalent as follows:
D/I = RS
CS1 = PSB
CS2 = NC

First method:

Second method: (Special thanks to this website for showing this method)
IMPORTANT! For this LCD to work using this method, you need to de-solder R9 and R11 at the back panel of the LCD.
The reason is, VDD is actually connected to PSB through the 0Ω resistors (R9 and R11), making PSB always HIGH, putting the LCD always in parallel bus mode. Connecting GND to this could end badly to your LCD and Arduino as you are basically connecting 5V to GND directly. So, just de-solder it. Don't worry, you can still connect it in parallel bus mode by connecting PSB to 5V.


Testing the LCD

To test the LCD, simply copy the U8glib library/folder into Documents > Arduino > libraries.
Start Arduino IDE. Goto File > Examples > U8glib > GraphicsTest.


The next step depends on the method that you choose.

If you choose the first method, un-comment the line:
U8GLIB_ST7920_128X64_4X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16

If you choose the second method, add the following line:
U8GLIB_ST7920_128X64_4X u8g(10);

If you choose the third method, modify the following line as you needed to:
U8GLIB_ST7920_128X64_4X u8g(18, 16, 17); // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17

If you choose the first method but using different pin numbers, make sure you also change the pin numbers in the declaration above.

If the LCD is showing various shapes and text, that means your LCD is working correctly. Now, you may go on and program your own graphic or anything that you want on the LCD. You may refer to more functions and explanation about the U8glib library on their official page and user reference page. Check out this page as well. There are links to the explanation of the examples on that page.

I've shown you the basics, so you should be able to explore more by yourselves. Just comment down here if you do not understand any step. Until next time, good bye. Have fun!