{"id":724,"date":"2022-01-25T04:46:24","date_gmt":"2022-01-25T04:46:24","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=724"},"modified":"2022-01-25T04:46:26","modified_gmt":"2022-01-25T04:46:26","slug":"working-with-stm32-and-sensors-hmc5883l-magnetic-field-sensor","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=724","title":{"rendered":"Working with STM32 and sensors: HMC5883L Magnetic Field Sensor"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/www.electronicwings.com\/public\/images\/user_images\/images\/Sensor%20%26%20Modules\/Magnetometer\/HMC5883L.PNG\" alt=\"\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this weekly guide, we shall take a look at the HMC5883L magnetic field sensor and measure the magnetic field in the three axis (x, y and z).<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is magnetometer<\/li><li>Interfacing HMC5883L with STM32F4<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 What is magnetometer:<\/h2>\n\n\n\n<p><strong>magnetometer,<\/strong>, instrument for measuring the strength and sometimes the direction of magnetic fields, including those on or near the Earth and in space. Magnetometers are also used to\u00a0calibrateelectromagnets and permanent magnets and to determine the magnetization of materials(<a href=\"https:\/\/www.britannica.com\/technology\/magnetometer\" data-type=\"URL\" data-id=\"https:\/\/www.britannica.com\/technology\/magnetometer\" target=\"_blank\" rel=\"noreferrer noopener\">source<\/a>).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 HMC5883L:<\/h2>\n\n\n\n<p>The Honeywell HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as low-cost compassing and magnetometry. The HMC5883L includes our state-of-the-art, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1\u00b0 to 2\u00b0 compass heading accuracy. The I2C serial bus allows for easy interface. The HMC5883L is a 3.0&#215;3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC). Applications for the HMC5883L include Mobile Phones, Netbooks, Consumer Electronics, Auto Navigation Systems, and Personal Navigation Devices.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"720\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-1024x720.png\" alt=\"\" class=\"wp-image-725\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-1024x720.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-300x211.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-768x540.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-1536x1081.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-2048x1441.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-1150x809.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-750x528.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-400x281.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.15.23-AM-250x176.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"357\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-1024x357.png\" alt=\"\" class=\"wp-image-726\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-1024x357.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-300x105.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-768x268.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-1536x536.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-2048x715.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-1150x401.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-750x262.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-400x140.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.16.30-AM-250x87.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Interfacing HMC5883L with STM32:<\/h2>\n\n\n\n<p>Since the module uses I2C bus to communicate with STM32, we shall use the i2c Read\/Wire multiple bytes for earlier post(<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=576\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=576\" target=\"_blank\">here<\/a><\/strong>) and we shall use Liquid Crystal Display (LCD) in I2C mode (<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=655\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=655\" target=\"_blank\">here<\/a><\/strong>).<\/p>\n\n\n\n<p>Before we start, we need take a look at the register from the datasheet<\/p>\n\n\n\n<p>Here is the list of the registers:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"402\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-1024x402.png\" alt=\"\" class=\"wp-image-727\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-1024x402.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-300x118.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-768x302.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-1536x603.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-2048x804.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-1150x452.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-750x295.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-400x157.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.21.06-AM-250x98.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence we can start by defining the registers in the source file (HMC5883L.c)<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define mode_register (0x02)\n#define config_b      (0x01)<\/pre><\/div>\n\n\n\n<p>and the address of the chip (from i2c scanner post (<strong><a href=\"https:\/\/blog.embeddedexpert.io\/?p=416\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=416\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/strong>))<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define address       (0x1E)<\/pre><\/div>\n\n\n\n<p>Now, in configuration register b, we need to configure the scale as shown:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"262\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-1024x262.png\" alt=\"\" class=\"wp-image-729\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-1024x262.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-300x77.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-768x196.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-1536x392.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-1150x294.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-750x192.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-400x102.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM-250x64.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.25.39-AM.png 1918w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"498\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-1024x498.png\" alt=\"\" class=\"wp-image-730\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-1024x498.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-300x146.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-768x373.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-1536x746.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-1150x559.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-750x364.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-400x194.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1-250x121.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-25-at-7.26.00-AM-1.png 1918w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence we can use enum for that in the header file (HMC5883L.h)<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef enum\n\t{\n\tHMC5883L_RANGE_0_88GA\t=0x00,\n\tHMC5883L_RANGE_1_3GA\t=0x20,\n\tHMC5883L_RANGE_1_9GA\t=0x40,\n\tHMC5883L_RANGE_2_5GA    =0x60,\n\tHMC5883L_RANGE_4GA      =0x80,\n\tHMC5883L_RANGE_4_7GA    =0xA0,\n\tHMC5883L_RANGE_5_7GA    =0xC0,\n\tHMC5883L_RANGE_8_1GA    =0xE0\n\n\t}HMC5883LScale_t;<\/pre><\/div>\n\n\n\n<p>Hence the initializing function: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int8_t HMC5883L_Init(HMC5883LScale_t scale)\n\t{\n\tint8_t state;\n\ti2c_writeByte(address, mode_register, 0x00);\n\n\tif(state==-1){return -1;}\n\n\ti2c_writeByte(address, 0x00, 0xD0);\n\n\tif(state==-1){return -1;}\n\n\tswitch(scale)\n\t    {\n\t\tcase HMC5883L_RANGE_0_88GA:\n\t\t\tm_Scale = 0.073f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_1_3GA:\n\t\t\tm_Scale = 0.92f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_1_9GA:\n\t\t\tm_Scale = 1.2f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_2_5GA:\n\t\t\tm_Scale = 1.52f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_4GA:\n\t\t\tm_Scale = 2.27f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_4_7GA:\n\t\t\tm_Scale = 2.56f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_5_7GA:\n\t\t\tm_Scale = 3.03f;\n\t\t    break;\n\n\t\tcase HMC5883L_RANGE_8_1GA:\n\t\t\tm_Scale = 4.35f;\n\t\t    break;\n\n\t\tdefault:\n\t\t    break;\n\t    }\n\n\tstate = i2c_writeByte(address, config_b, scale);\n\tif(state==-1){return -1;}\n\treturn 0;\n\t}<\/pre><\/div>\n\n\n\n<p>For reading the value, we can use read_multibyte function starting from address 0x03 as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int8_t update_HMC5883L(void)\n\t{\n\n\tint8_t state;\n\tstate=i2c_ReadMulti(address, 0x03, 6, (char *)gaus_data);\n\tif(state==-1){return -1;}\n\treturn 0;\n\t}<\/pre><\/div>\n\n\n\n<p>For getting the scaled value, we can use simple 3 functions for this purpose:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\nfloat getGausX(void)\n\t{\n\tuint16_t data=gaus_data[0]&lt;&lt;8|gaus_data[1];\n\tfloat g=data*m_Scale;\n\treturn g;\n\t}\n\nfloat getGausY(void)\n\t{\n\tuint16_t data=gaus_data[4]&lt;&lt;8|gaus_data[5];\n\tfloat g=data*m_Scale;\n\treturn g;\n\t}\n\nfloat getGausZ(void)\n\t{\n\tuint16_t data=gaus_data[2]&lt;&lt;8|gaus_data[3];\n\tfloat g=data*m_Scale;\n\treturn g;\n\t}\n<\/pre><\/div>\n\n\n\n<p>In the main function<\/p>\n\n\n\n<p>We can include the lLCD with I2C, the HMC5883L and enable FPU (floating point uint) as following<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;LiquidCrystal_PCF8574.h&quot;\n#include &quot;delay.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;HMC5883L.h&quot;\n#include &quot;stm32f4xx.h&quot;\nfloat gx,gy,gz;\n\n\n\/*\n * This function shall enable the FPU unit in cortex-M4\/M7 based MCU\n * Note: Not all Cortex-M4\/M7 has embedded FPU\n *\n * *\/\nvoid enable_fpu()\n\t{\n\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n\n\t}\n\n\n\nint main(void)\n\t{\n\tenable_fpu();\n\tsystick_init_ms(16000000);\n\tlcd_init();\n\tHMC5883L_Init(HMC5883L_RANGE_2_5GA);\n\tsetCursor(0,0);\n\tlcd_send_string(&quot;magnetometer&quot;);\n\n\n\twhile(1)\n\t\t{\n\t\tupdate_HMC5883L();\n\t\tgx=getGausX();\n\t\tgy=getGausY();\n\t\tgz=getGausZ();\n\t\tchar data[20];\n\t\tsprintf(data,&quot;Gx=%0.5f       &quot;,gx);\n\t\tsetCursor(0, 1); lcd_send_string(data);\n\n\t\tsprintf(data,&quot;Gy=%0.5f       &quot;,gy);\n\t\tsetCursor(0, 2); lcd_send_string(data);\n\n\t\tsprintf(data,&quot;Gz=%0.5f     &quot;,gz);\n\t\tsetCursor(0, 3); lcd_send_string(data);\n\t\tdelay(100);\n\n\n\n\t\t}\n\n\n\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>in the while 1 loop<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Update the HMC5883L<\/li><li>Get the scaled data<\/li><li>Display the data on the display<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file aligncenter\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/HMC5883L.zip\">HMC5883L<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/HMC5883L.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/IMG_7777.mp4\"><\/video><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this weekly guide, we shall take a look at the HMC5883L magnetic field sensor and measure the magnetic field in the three axis (x, y and z). In this guide, we shall cover the following: What is magnetometer Interfacing HMC5883L with STM32F4 Code Demo 1.1 What is magnetometer: magnetometer,, instrument for measuring the strength [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,19,11,12],"tags":[],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-lcd","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/724"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=724"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/724\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}