This tutorial explains how to work with magnetometer and black Beaglebone. The magnetometer detects the low magnetic field and works like a digital compass. It is used in tracking or navigation applications. In this tutorial, HMC5883L magnetometer was used and connected to Beaglebone black via I2C protocol. The program is written in python script with adafruit I2C library.
Required tools :
- Black Beaglebone
- Magnetometer (HMC5883L module)
- Test board
- Female to female connectors
Software environment configuration
Install the latest version of python on BBB as explained in the tutorial How to make the first python program with Beaglebone Black. Install the adafruit python-GPIO library.
Working
HMC5883L is a low magnetic detection device and 3-axis digital compass. It is supported by I2C interface. It generates sensitive magnetic value during simple measurements. The angle value and degree are displayed in the SSH prompt every second. The magnetometer detects the magnetic field and generates the digital value accordingly. The HMC5883L magnetometer detects the analog value, but has an on-chip ADC that provides the digital value. You can get more information in the HMC5883L datasheet.
Fig. 1: Image of the HMC5883L Magenetometer
I2C is a two-wire serial communication protocol that transfers data serially between two devices. Only two pins are needed for data to transfer:
1) SCL – serial clock pulse
2) SDA – Serial data address
Advantage of I2C read and write operation performed by just a single pin, unlike SPI. You can get more information about this at I2C Interface or TWI (Two Wire Interface).
Description
Let's first prepare the circuit connection. Take a breadboard and supply VCC and ground from BBB to the breadboard line. Connect 3.3V power from pin number 3 third of header P9 and ground from pin number 2 and header P8.
You don't have to worry about adding extra circuits to the sensor because they are already added to the chip.
You just need to establish some connections with the 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 tap command with .py extension (i.e. magnetometer.py). Open the file with any text editor (i.e. nano, vim etc.) and write code in python language.
Configuration and working prototype:
You need to import the I2C library into the program to use the I2C device. You can create your own library, but Adafruit library can be used to save time as it provides all types of BBB python library.
from Adafruit_I2C import Adafruit_I2C
It imports the I2C library into your script.
Next, set the magnetometer address for communication. The device address is 7 bits and addressed to the master by the following function *(I didn't understand the line):
i2c = Adafruit_I2C (0x1e)
Note: 0x1e is the HMC5883L device address.
Configure the magnetometer register for operation. Here is a list of 8-bit registers:
The magnetometer has four pins:
- SCL
- SDA
- GND
- CCV
Connect the SDA and SCL pin of the magnetometer sensor to the SDA pin (20th pin of the P9 header) and SCL pin ( 19th pin of the P9 header) of the BBB respectively. Provide VCC and ground to the magnetometer.
Fig. 2: Table listing integrated records of the HMC5883L magenetometer
You can get more details about setup and registration in the magnetometer datasheet.
Download the technical sheet at the following link:
Write the configuration value to the A register following the function and pass the appropriate parameter:
i2c.write8 (0x01, 0x71)
The first argument addresses the location of the registry and the second argument is the configuration value that is written to the registry.
The A register setting selects sample per measurement, data output rate, and measurement operating mode.
Similarly, write the configuration value to the B register and the mode register following the function:
i2c.write8 (0x02, 0xA0)
The B register setting selects the gain setting.
i2c.write8 (0x03, 0x00)
The mode register is an 8-bit register from which data can be read or written. It selects the operating mode.
Project source code
###
from Adafruit_I2C import Adafruit_I2C
###
Circuit diagrams
Circuit Diagram-Beaglebone-Black-Interfacing-HMC5883L-Magenetometer |