Interface do magnetômetro com Beaglebone preto (Parte 8/15)

Magnetometer interface with black Beaglebone (Part 8/15)

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.

Imagem do Magenetômetro HMC5883L

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.

Tabela listando registros integrados do magenetômetro HMC5883L

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
import math
of time matter sleep i2c = 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) second print("x axis = " + str(x)) first = i2c.readS8(0x05) second = i2c.readS8(0x06) z = (first << 8) second print("z axis = " + str(z)) first = i2c.readS8(0x07) second = i2c.readS8(0x08) y = (first << 8) second print("y axis = " + str(y)) degree = 90-math.atan(x/y)*180/math.pi print("angle = " + str(degree)) print('n')

###

Circuit diagrams

Circuit Diagram-Beaglebone-Black-Interfacing-HMC5883L-Magenetometer

Project video

Conteúdo Relacionado

A network of sensors is embedded in every vehicle,...
The motor controller is one of the most important...
ESP32-CAM is a compact camera module that combines the...
A evolução dos padrões USB foi fundamental para moldar...
A SCHURTER anuncia um aprimoramento para sua conhecida série...
A Sealevel Systems anuncia o lançamento da Interface Serial...
A STMicroelectronics introduziu Diodos retificadores Schottky de trincheira de...
Determinar uma localização precisa é necessário em várias indústrias...
O novo VIPerGaN50 da STMicroelectronics simplifica a construção de...
A Samsung Electronics, fornecedora de tecnologia de memória avançada,...
O mercado embarcado tem uma necessidade de soluções de...
You have probably come across the term ' drag...
You probably have a support insulator if you've noticed...
You've probably seen stand an insulator sit on power...
You've probably seen shackle insulators enthroned on electricity poles,...
You have probably experienced situations where controlling a circuit...
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.