This tutorial explains how to work with magnetometer and Beaglebone black. Magnetometer senses the low magnetic field and works as a digital compass. It is used in tracking or navigation application. In this tutorial, HMC5883L magnetometer has been used and interfaced with Beaglebone black through I2C protocol. Program is written in python script with adafruit I2C library.
Required Tools:
- Beaglebone Black
- Magnetometer ( HMC5883L module)
- Breadboard
- Female to Female connectors
Setup of Software environment
Install the latest python version in BBB as explained in tutorial How to make first python program with Beaglebone Black. Install the adafruit python-GPIO library.
Working
HMC5883L is 3-axis low magnetic sensing and digital compass device. It is supported by I2C interface. It generates sensitive magnetic value during simple measurement. Value and degree of angle are displayed on SSH prompt every one second. Magnetometer senses the magnetic field and generates the digital value accordingly. Magnetometer HMC5883L senses the analog value but it has on chip ADC which provides the digital value. You can get more information from datasheet of HMC5883L.
Fig. 1: Image of HMC5883L Magenetometer
I2C is a two wire serial communication protocol which transfers the data serially between two devices. It requires only two pins for data transfer:
1) SCL – Serial Clock pulse
2) SDA – Serial data address
Advantage of I2C reads and writes operation performed by only single pin unlike SPI. You can get more information about it from I2C Interface or TWI (Two Wire Interface).
Description
Let’s first prepare the circuit connection. Take a breadboard and provide VCC and ground from BBB to breadboard line. Connect Supply 3.3 V from pin number 3rd of header P9 and ground from pin number 2nd of header P8.
You don’t need to worry about adding extra circuitry with sensor because it is already added on chip.
You just need to establish few connections with Beaglebone black.
Open the command terminal and access Beaglebone black through SSH as explained in getting started with Beaglebone black. Create a new file using touch command with .py extension (i.e. magnetometer.py). Open the file with any text editor (i.e. nano, vim etc.) and write a code in python language.
Configuration and Working Prototype:
You need to import I2C library in program to make use of I2C device. You can create your own library but Adafruit library can be used to save time as it provides all kinds of python library of BBB.
from Adafruit_I2C import Adafruit_I2C
It imports I2C library in your script.
Next, set the address for magnetometer for communication. Address of device is 7-bit and addressed to master by following function *(didn’t get the line):
i2c = Adafruit_I2C (0x1e)
Note: 0x1e is device address of HMC5883L.
Configure the register of magnetometer for operation. Here is a list of 8 bit registers:
Magnetometer has four pins:
- SCL
- SDA
- GND
- VCC
Connect the SDA and SCL pin of magnetometer sensor to SDA pin (20th pin of header P9) and SCL pin (19th pin of header P9) of BBB respectively. Supply the VCC and ground to magnetometer.
Fig. 2: Table Listing In-Built Registers of HMC5883L Magenetometer
You can get more details about configuration and bit of register from the magnetometer datasheet.
Download the datasheet from following link:
https://www.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf
Write the configuration value in register A by following function and pass appropriate parameter:
i2c.write8 (0x01, 0x71)
First argument addresses location of register and second argument is the configuration value which is written in register.
Configuration of register A selects sample per measurement, data output rate and mode of measurement operation.
Similarly, write the configuration value in register B and mode register by following function:
i2c.write8 (0x02, 0xA0)
Configuration of register B selects gain configuration.
i2c.write8 (0x03, 0x00)
The mode register is an 8-bit register from which data can be read or to which data can be written. It selects the operating mode.
Project Source Code
###
from Adafruit_I2C import Adafruit_I2Cimport mathfrom time import sleepi2c = Adafruit_I2C(0x1e)i2c.write8(0x00, 0x71)i2c.write8(0x01, 0xA0)i2c.write8(0x02, 0x00)while True:first = i2c.readS8(0x03)second = i2c.readS8(0x04)x = (first << 8) | secondprint("x-axis = " + str(x))first = i2c.readS8(0x05)second = i2c.readS8(0x06)z = (first << 8) | secondprint("z-axis = " + str(z))first = i2c.readS8(0x07)second = i2c.readS8(0x08)y = (first << 8) | secondprint("y-axis = " + str(y))degree = 90-math.atan(x/y)*180/math.piprint("angle = " + str(degree))print('n')###
Circuit Diagrams
Project Video
Filed Under: BeagleBone, Electronic Projects
Filed Under: BeagleBone, Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.