Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Gas Meter Reading Using a Magnetometer

Gas Meter Reading Using a Magnetometer

Scheduled Pinned Locked Moved My Project
77 Posts 10 Posters 23.3k Views 12 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ThomasDrT Offline
    ThomasDrT Offline
    ThomasDr
    wrote on last edited by
    #4

    Hello,

    here my sketch to form a puls:

    /***************************************************************************
      This is a library example for the HMC5883 magnentometer/compass
    
      Designed specifically to work with the Adafruit HMC5883 Breakout
      http://www.adafruit.com/products/1746
     
      *** You will also need to install the Adafruit_Sensor library! ***
    
      These displays use I2C to communicate, 2 pins are required to interface.
    
      Adafruit invests time and resources providing this open source code,
      please support Adafruit andopen-source hardware by purchasing products
      from Adafruit!
    
      Written by Kevin Townsend for Adafruit Industries with some heading example from
      Love Electronics (loveelectronics.co.uk)
     
     This program is free software: you can redistribute it and/or modify
     it under the terms of the version 3 GNU General Public License as
     published by the Free Software Foundation.
     
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
    
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
     ***************************************************************************/
    
    #include <Wire.h>
    #include <Adafruit_Sensor.h>
    #include <Adafruit_HMC5883_U.h>
    
    /* Assign a unique ID to this sensor at the same time */
    Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
    
    unsigned long t = 0;
    unsigned long counter = 0;
    bool trigger = 0;
    float out = 0.0;
    
    void setup(void) 
    {
      Serial.begin(115200);
      //Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
      
      /* Initialise the sensor */
      if(!mag.begin())
      {
        /* There was a problem detecting the HMC5883 ... check your connections */
        Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
        while(1);
      }
      
      /* Display some basic information on this sensor */
      // displaySensorDetails();
    }
    
    void loop(void) 
    {
      /* Get a new sensor event */ 
      sensors_event_t event; 
      mag.getEvent(&event);
     
      /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
      out = event.magnetic.y;
      if (out > -0.7 && trigger == 0)
      {
      trigger = 1;
      Serial.println(trigger);
      }
      if (out < -110.5 && trigger == 1)
      {
      trigger = 0;
      counter++;
      Serial.println(trigger);
      }
      
      if (millis() > (t+5000))
       {
       Serial.println(trigger);
       t = millis();
       }
     
      
      delay(250);
    }
    

    And here the Output, 1 Puls is 0.001 m3, first at slow flow and then with my max.

    0_1486850133246_gas-002.png

    regards
    ThomasD

    YveauxY dpcrD 2 Replies Last reply
    0
    • ThomasDrT ThomasDr

      Hello,

      here my sketch to form a puls:

      /***************************************************************************
        This is a library example for the HMC5883 magnentometer/compass
      
        Designed specifically to work with the Adafruit HMC5883 Breakout
        http://www.adafruit.com/products/1746
       
        *** You will also need to install the Adafruit_Sensor library! ***
      
        These displays use I2C to communicate, 2 pins are required to interface.
      
        Adafruit invests time and resources providing this open source code,
        please support Adafruit andopen-source hardware by purchasing products
        from Adafruit!
      
        Written by Kevin Townsend for Adafruit Industries with some heading example from
        Love Electronics (loveelectronics.co.uk)
       
       This program is free software: you can redistribute it and/or modify
       it under the terms of the version 3 GNU General Public License as
       published by the Free Software Foundation.
       
       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU General Public License for more details.
      
       You should have received a copy of the GNU General Public License
       along with this program.  If not, see <http://www.gnu.org/licenses/>.
      
       ***************************************************************************/
      
      #include <Wire.h>
      #include <Adafruit_Sensor.h>
      #include <Adafruit_HMC5883_U.h>
      
      /* Assign a unique ID to this sensor at the same time */
      Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
      
      unsigned long t = 0;
      unsigned long counter = 0;
      bool trigger = 0;
      float out = 0.0;
      
      void setup(void) 
      {
        Serial.begin(115200);
        //Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
        
        /* Initialise the sensor */
        if(!mag.begin())
        {
          /* There was a problem detecting the HMC5883 ... check your connections */
          Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
          while(1);
        }
        
        /* Display some basic information on this sensor */
        // displaySensorDetails();
      }
      
      void loop(void) 
      {
        /* Get a new sensor event */ 
        sensors_event_t event; 
        mag.getEvent(&event);
       
        /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
        out = event.magnetic.y;
        if (out > -0.7 && trigger == 0)
        {
        trigger = 1;
        Serial.println(trigger);
        }
        if (out < -110.5 && trigger == 1)
        {
        trigger = 0;
        counter++;
        Serial.println(trigger);
        }
        
        if (millis() > (t+5000))
         {
         Serial.println(trigger);
         t = millis();
         }
       
        
        delay(250);
      }
      

      And here the Output, 1 Puls is 0.001 m3, first at slow flow and then with my max.

      0_1486850133246_gas-002.png

      regards
      ThomasD

      YveauxY Offline
      YveauxY Offline
      Yveaux
      Mod
      wrote on last edited by
      #5

      @ThomasDr do I understand it correctly that you get a full sine swing for each 0.001 m3 of gas usage?

      http://yveaux.blogspot.nl

      dpcrD ThomasDrT 2 Replies Last reply
      0
      • YveauxY Yveaux

        @dpcr Very nice project and creative use of a magnetometer!
        These things are very cheap and able to solve far more problems than just telling in what direction you're heading ;-)

        Could you post a shot of some of the sine data from the serial plotter? Just wondering what it looks like.
        Any idea about the resolution you get with this method? You describe half a sine to be divided in 10 steps, but how much gas usage does one sine represent?

        dpcrD Offline
        dpcrD Offline
        dpcr
        wrote on last edited by
        #6

        @Yveaux ! The picture shows the sine wave produced by the HMC5883L. The orange line is the Y axis, which is the one I am using. The large visible sine wave is when the gas is flowing for the furnace. When the gas stops the line is creeping upwards, this is the pilot light on the water heater.
        0_1486851169495_Untitled1.png

        1 Reply Last reply
        0
        • YveauxY Yveaux

          @ThomasDr do I understand it correctly that you get a full sine swing for each 0.001 m3 of gas usage?

          dpcrD Offline
          dpcrD Offline
          dpcr
          wrote on last edited by
          #7

          @Yveaux Not quite. What I did was look at the actual gas meter and determine how many full "cycles" of the sign wave I received for 2 CuFt. What I got was 16 full cycles per 2 CuFt. I then divided that by 2 to get how many cycles per CuFt (8). Then I divided that by the number of divisions per one full cycle (20), 10 from TOP to BOTTOM and 10 from BOTTOM to TOP. This gave me the the CuFt of gas used per division or pulse. I then converted that to litre. This gave me the 0.17698 per litre. Hope this help and I hope my math is correct.

          1 Reply Last reply
          0
          • YveauxY Yveaux

            @ThomasDr do I understand it correctly that you get a full sine swing for each 0.001 m3 of gas usage?

            ThomasDrT Offline
            ThomasDrT Offline
            ThomasDr
            wrote on last edited by ThomasDr
            #8

            @Yveaux Sorry, it is 1 puls at 0.01 m3
            That depends on the gas meter you use, the BK4 gives a magnetic pulse per 0.01 m3.
            For this puls you can use a reed contact or a magnetometer.
            0_1486857534669_IMG_20170212_005723_649.jpg

            regards
            ThomasD

            YveauxY 1 Reply Last reply
            0
            • ThomasDrT ThomasDr

              @Yveaux Sorry, it is 1 puls at 0.01 m3
              That depends on the gas meter you use, the BK4 gives a magnetic pulse per 0.01 m3.
              For this puls you can use a reed contact or a magnetometer.
              0_1486857534669_IMG_20170212_005723_649.jpg

              regards
              ThomasD

              YveauxY Offline
              YveauxY Offline
              Yveaux
              Mod
              wrote on last edited by
              #9

              @dpcr Thanks for the nice chart!
              It is indeed a clear sine wave. Having the sensor run for a while and checking with the actual meter counts will prove if your math was correct ;-)
              You could also use inverse sine to calculate the angle of the sine pulse, to have linear readout and probably even increase resolution.

              @ThomasDr I'm currently using a pulse counter (reed contact) to count the pulses of my meter (also 100/m3).
              Works well, but the magnetometer has the potential to increase the measurement resolution.

              http://yveaux.blogspot.nl

              1 Reply Last reply
              0
              • gohanG Offline
                gohanG Offline
                gohan
                Mod
                wrote on last edited by
                #10

                Sorry for the question, but why would you need a magnetometer to have better resolution in the measurements when you only need to count pulses? Given of course that the reed switch provides accurate readings, because if it doesn't then I understand why to use the magnetometer

                YveauxY dpcrD ThomasDrT 3 Replies Last reply
                0
                • gohanG gohan

                  Sorry for the question, but why would you need a magnetometer to have better resolution in the measurements when you only need to count pulses? Given of course that the reed switch provides accurate readings, because if it doesn't then I understand why to use the magnetometer

                  YveauxY Offline
                  YveauxY Offline
                  Yveaux
                  Mod
                  wrote on last edited by
                  #11

                  @gohan well, it seems like a magnetometer is able to also give detailed information inbetween the pulses.
                  For a pulse counter you only see a complete revolution as a single pules; it doesn't tell you how far the revolution is. The magnetometer creates a sine signal, which indicates where you are on this single revolution.

                  http://yveaux.blogspot.nl

                  1 Reply Last reply
                  0
                  • gohanG Offline
                    gohanG Offline
                    gohan
                    Mod
                    wrote on last edited by
                    #12

                    I can see that, but looking at the graph if that climbing line is just the consumption of the pilot light, how would you benefit for this added resolution? I am just referring to the fact that this kind of sensor is used to keep track of gas usage during the day and not a live view of the actual flow even for the small amounts: we are talking 0.01 cubic meter for each pulse and a pilot light usually uses 3 cubic meters per hour, so we end up with a pulse every the 10-15 seconds and to me it seems a good resolution, or maybe I just don't see what are other possible benefits.

                    1 Reply Last reply
                    0
                    • gohanG gohan

                      Sorry for the question, but why would you need a magnetometer to have better resolution in the measurements when you only need to count pulses? Given of course that the reed switch provides accurate readings, because if it doesn't then I understand why to use the magnetometer

                      dpcrD Offline
                      dpcrD Offline
                      dpcr
                      wrote on last edited by
                      #13

                      @gohan I had to use a magnetometer because my meter did not have a magnet in it. I tried a reed switch but it never actuated and my meter is outside. So I was stuck using a magnetometer which I mounted in a water proof plastic box. The issue I had was getting the sine wave to create a pulse that was usable for MySensors. I did look at using an inverse sine, or as someone else mentioned, use an exponential moving average of the data but they both failed. When the gas stops flowing, a pilot light as an example as seen in the attached picture, there is little to no gas flowing so the angle doesn't change much. It looks like no gas is flowing when it actually is. I decided to go with dividing the wave or meter movement into separate divisions to create a more accurate way of measuring the gas flow in all flow situations.

                      I haven't had a chance to test the accuracy of my math against the gas meter but I will at some point. The issue I'm currently having is that the bellows inside of the meter that the magnetometer is reading do not move at the same rate at all times. This causes the FLOW output to change when I know the gas is flowing at a steady rate. The VOLUME doesn't seem to be effected too much. So what I did was to take a moving average of the last three FLOW readings. This helps a little.

                      The hardware and ideas were done by me but the coding was done by my 24 year old son. I always enjoy a challenge. Please feel free to comment.

                      1 Reply Last reply
                      1
                      • gohanG Offline
                        gohanG Offline
                        gohan
                        Mod
                        wrote on last edited by
                        #14

                        Ok, now it makes sense. If you plot the data with the pilot using the gas, don't you still see a sine wave but with, of course, a longer period?

                        YveauxY dpcrD 2 Replies Last reply
                        0
                        • gohanG gohan

                          Ok, now it makes sense. If you plot the data with the pilot using the gas, don't you still see a sine wave but with, of course, a longer period?

                          YveauxY Offline
                          YveauxY Offline
                          Yveaux
                          Mod
                          wrote on last edited by
                          #15

                          @gohan I assume you do. By interpreting the sine and thereby increasing the resolution you might be able to see there's gas flowing, even if the rate is very low.

                          http://yveaux.blogspot.nl

                          1 Reply Last reply
                          0
                          • gohanG Offline
                            gohanG Offline
                            gohan
                            Mod
                            wrote on last edited by
                            #16

                            good luck with the math then, I am calling myself out! :sweat_smile:
                            Back in high school I was quite good at math, but now... :D

                            1 Reply Last reply
                            0
                            • gohanG gohan

                              Ok, now it makes sense. If you plot the data with the pilot using the gas, don't you still see a sine wave but with, of course, a longer period?

                              dpcrD Offline
                              dpcrD Offline
                              dpcr
                              wrote on last edited by
                              #17

                              @gohan Yes you do, but what about the times when there is gas flowing. If I remember correctly there was about 20 or 25 minutes for full sine wave when only the pilot was running. That SEND_FREQUENCY (1500000) was too large when the gas was actually flowing to get an accurate measurement. I just wanted to get an accurate reading of the gas flow at all flow rates. Am I missing something?

                              Here are some screenshots from Domoticz for the past week:
                              1_1486917738604_Screenshot from 2017-02-12 11-40-09.png 0_1486917738603_Screenshot from 2017-02-12 11-40-26.png )

                              1 Reply Last reply
                              0
                              • gohanG gohan

                                Sorry for the question, but why would you need a magnetometer to have better resolution in the measurements when you only need to count pulses? Given of course that the reed switch provides accurate readings, because if it doesn't then I understand why to use the magnetometer

                                ThomasDrT Offline
                                ThomasDrT Offline
                                ThomasDr
                                wrote on last edited by
                                #18

                                @gohan Hello,
                                My thought is simpler.
                                The original sensor is too expensive.
                                With a Reedcontact I must find the right position.
                                The magnetometer I could simply attach somewhere in the proximity.

                                regards
                                ThomasD

                                gohanG 1 Reply Last reply
                                0
                                • ThomasDrT ThomasDr

                                  @gohan Hello,
                                  My thought is simpler.
                                  The original sensor is too expensive.
                                  With a Reedcontact I must find the right position.
                                  The magnetometer I could simply attach somewhere in the proximity.

                                  regards
                                  ThomasD

                                  gohanG Offline
                                  gohanG Offline
                                  gohan
                                  Mod
                                  wrote on last edited by
                                  #19

                                  @ThomasDr
                                  I see your point, but you pay the simplicity with a more complex coding ;-)

                                  1 Reply Last reply
                                  0
                                  • ThomasDrT ThomasDr

                                    Hello,

                                    here my sketch to form a puls:

                                    /***************************************************************************
                                      This is a library example for the HMC5883 magnentometer/compass
                                    
                                      Designed specifically to work with the Adafruit HMC5883 Breakout
                                      http://www.adafruit.com/products/1746
                                     
                                      *** You will also need to install the Adafruit_Sensor library! ***
                                    
                                      These displays use I2C to communicate, 2 pins are required to interface.
                                    
                                      Adafruit invests time and resources providing this open source code,
                                      please support Adafruit andopen-source hardware by purchasing products
                                      from Adafruit!
                                    
                                      Written by Kevin Townsend for Adafruit Industries with some heading example from
                                      Love Electronics (loveelectronics.co.uk)
                                     
                                     This program is free software: you can redistribute it and/or modify
                                     it under the terms of the version 3 GNU General Public License as
                                     published by the Free Software Foundation.
                                     
                                     This program is distributed in the hope that it will be useful,
                                     but WITHOUT ANY WARRANTY; without even the implied warranty of
                                     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                                     GNU General Public License for more details.
                                    
                                     You should have received a copy of the GNU General Public License
                                     along with this program.  If not, see <http://www.gnu.org/licenses/>.
                                    
                                     ***************************************************************************/
                                    
                                    #include <Wire.h>
                                    #include <Adafruit_Sensor.h>
                                    #include <Adafruit_HMC5883_U.h>
                                    
                                    /* Assign a unique ID to this sensor at the same time */
                                    Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
                                    
                                    unsigned long t = 0;
                                    unsigned long counter = 0;
                                    bool trigger = 0;
                                    float out = 0.0;
                                    
                                    void setup(void) 
                                    {
                                      Serial.begin(115200);
                                      //Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
                                      
                                      /* Initialise the sensor */
                                      if(!mag.begin())
                                      {
                                        /* There was a problem detecting the HMC5883 ... check your connections */
                                        Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
                                        while(1);
                                      }
                                      
                                      /* Display some basic information on this sensor */
                                      // displaySensorDetails();
                                    }
                                    
                                    void loop(void) 
                                    {
                                      /* Get a new sensor event */ 
                                      sensors_event_t event; 
                                      mag.getEvent(&event);
                                     
                                      /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
                                      out = event.magnetic.y;
                                      if (out > -0.7 && trigger == 0)
                                      {
                                      trigger = 1;
                                      Serial.println(trigger);
                                      }
                                      if (out < -110.5 && trigger == 1)
                                      {
                                      trigger = 0;
                                      counter++;
                                      Serial.println(trigger);
                                      }
                                      
                                      if (millis() > (t+5000))
                                       {
                                       Serial.println(trigger);
                                       t = millis();
                                       }
                                     
                                      
                                      delay(250);
                                    }
                                    

                                    And here the Output, 1 Puls is 0.001 m3, first at slow flow and then with my max.

                                    0_1486850133246_gas-002.png

                                    regards
                                    ThomasD

                                    dpcrD Offline
                                    dpcrD Offline
                                    dpcr
                                    wrote on last edited by
                                    #20

                                    @ThomasDr Sorry not to get back to you when I saw your post, your project looks interesting. Does the HMC5983 magnetometer output something similar to the HMC5883L? I just used the HMC5883L magnetometer because I found it useful, I didn't run across it during my research.

                                    1 Reply Last reply
                                    0
                                    • dpcrD Offline
                                      dpcrD Offline
                                      dpcr
                                      wrote on last edited by
                                      #21

                                      Ran into a problem. The magnetism value changes when the outside air temperature changes which causes many incorrect readings. So my initial sketch that finds the TOP and BOTTOM of the wave is having problems so I'm in the process of trying to find another way to get some useful data out of the magnetometer. Any ideas?

                                      1 Reply Last reply
                                      0
                                      • gohanG Offline
                                        gohanG Offline
                                        gohan
                                        Mod
                                        wrote on last edited by gohan
                                        #22

                                        If you find a correlation between temperature and change of magnetic field, maybe you could add a thermistore and use it to compute a correction coefficient.
                                        Or maybe even a second magnetometer with a fixed magnet to use as reference 🤔

                                        dpcrD 1 Reply Last reply
                                        0
                                        • gohanG gohan

                                          If you find a correlation between temperature and change of magnetic field, maybe you could add a thermistore and use it to compute a correction coefficient.
                                          Or maybe even a second magnetometer with a fixed magnet to use as reference 🤔

                                          dpcrD Offline
                                          dpcrD Offline
                                          dpcr
                                          wrote on last edited by
                                          #23

                                          @gohan Thanks, that was what I thought initially but the changes in the magnetic forces were all over the place but did seem to coincide with the outdoor temperature. They were relatively small in comparison to the total but enough the cause problems. It was almost like the meter itself was doing this?? Since we rely so heavily on the Top and Bottom magnetic readings to not only find the top and the bottom of the "wave" but to also calculate the intervals, we made some updates to the sketch. It now calculates a new top and bottom on its own. It has been running for 12 hours and so far so good. For now we are writing the top and bottom values to EEPROM, but once I get some memory we'll store the values there.

                                          I did have a question that I can't seem to find a answer to - what length I2C bus can be used with out pull up resistors? The current bus being used is about 2 meters without pull up resistors and there doesn't seem to be too much noise compared to when it was only several inches . I would like to make it a little longer if possible.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          19

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular