Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

Magnetometer interfacing with Beaglebone black (Part 8/15)

By Ashish Vara

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.

Image of HMC5883L Magenetometer

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.

Table Listing In-Built Registers of HMC5883L Magenetometer

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_I2C
import math
 
from time import 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


Filed Under: Electronic Projects

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Introduction to Brain Waves & its Types (Part 1/13)
  • Understanding NeuroSky EEG Chip in Detail (Part 2/13)
  • Performing Experiments with Brainwaves (Part 3/13)
  • Amplification of EEG Signal and Interfacing with Arduino (Part 4/13)
  • Controlling Led brightness using Meditation and attention level (Part 5/13)
  • Control Motor’s Speed using Meditation and Attention Level of Brain (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • What are the battery-selection criteria for low-power design?
  • Key factors to optimize power consumption in an embedded device
  • EdgeLock A5000 Secure Authenticator
  • How to interface a DS18B20 temperature sensor with MicroPython’s Onewire driver
  • Introduction to Brain Waves & its Types (Part 1/13)

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Poly Spacing Effect
  • Thyristor Gate Drive
  • Passive Harmonics Filter
  • Variable Phase shift control circuit for PWM circuit
  • Avalanche Pulser

RSS Electro-Tech-Online.com Discussions

  • HV Diodes
  • Question about ultrasonic mist maker
  • Someone please explain how this BMS board is supposed to work?
  • DIY bluetooth speaker
  • Disabled son needs advice please
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering