Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    HMC5883L(GY-271) driver for 3-Axis Digital Compass IC

    Scheduled Pinned Locked Moved Discussion
    drivercompasshmc5883lgy-271
    11 Posts 5 Posters 4.0k Views 6 Watching
    Loading More Posts
    • 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.
    • L Offline
      livius
      last edited by livius

      Hi,

      if someone is interested i have created driver for HMC5883L 3-Axis Digital Compass.
      https://github.com/pycom/pycom-libraries/pull/8/files

      without compensation, self test and calibration but good point to start.
      Strongly optimized form memory allocation point of view in every m.readAxes()

      UPDATE
      HMC5883L is in GY-271 module.
      Use case

      import HMC5883L
      import gc
      import time
      m = HMC5883L.HMC5883L()
      while True:
      	m.readAxes()
      	m.heading()
      	print(m)
      	time.sleep_ms(1000)
      	gc.collect()
      

      sample output from above

      X: 391.0, Y: -341.32, Z: 73.6 - Heading: 318.8809, Declination: (0, 0)
      X: 390.08, Y: -339.48, Z: 77.28 - Heading: 318.9675, Declination: (0, 0)
      

      but better (from memory point of view) is replace print(m)
      by

      print(m.x,m.y,m.z,m.h,m.headingDeg)
      

      and if you do not need m.heading() - you can simply do not call it

      think about people first, then computers

      RobTuDelftR 1 Reply Last reply Reply Quote 2
      • RobTuDelftR Offline
        RobTuDelft @livius
        last edited by

        @livius said in HMC5883L(GY-271) driver for 3-Axis Digital Compass IC:

        HMC5883L is in GY-271 module.

        Where can I find this module please? Thank you.

        Without data you're just another person with an opinion.
        Hackster | About me

        L 1 Reply Last reply Reply Quote 0
        • L Offline
          livius @RobTuDelft
          last edited by

          @RobTuDelft
          https://github.com/pycom/pycom-libraries/pull/8/files

          think about people first, then computers

          RobTuDelftR 1 Reply Last reply Reply Quote 1
          • RobTuDelftR Offline
            RobTuDelft @livius
            last edited by RobTuDelft

            @livius Thanks a lot Livius.

            Spotted a slight error in the example:
            m = HMC5883L2.HMC5883L() should be m = HMC5883L.HMC5883L()

            Without data you're just another person with an opinion.
            Hackster | About me

            L 1 Reply Last reply Reply Quote 1
            • L Offline
              livius @RobTuDelft
              last edited by

              @RobTuDelft
              i must look into it - what i have on board
              i remember that file with 2 was from testing time
              Thanks for pointing this

              think about people first, then computers

              1 Reply Last reply Reply Quote 0
              • R Offline
                roadfox
                last edited by roadfox

                Works here, on LoPy and pycom expansion board, can read data.
                I'm using this module: http://hobby-elektronik.ch/Module-Sensoren/Bewegung-Distanz/3-Achsen-Kompass-GY-271-HMC5883L-Magnetometer-Sensor-I2C::127.html

                Now to figure out whoch way to mount the module to get usabel heading values...

                thanks allot

                1 Reply Last reply Reply Quote 0
                • S Offline
                  simonjcarr
                  last edited by

                  @RobTuDelft said in HMC5883L(GY-271) driver for 3-Axis Digital Compass IC:

                  HMC5883L2

                  I have tried the code with this device and I get the following error

                  OSError: I2C bus error

                  Do I need to connect the DRDY pin to the LoPy and can anyone confirm which pin numbers on the breakout board the SCL and SDA pins are on, there is nothing in documentation to explain this.

                  Thanks

                  L 1 Reply Last reply Reply Quote 0
                  • L Offline
                    livius @simonjcarr
                    last edited by livius

                    @simonjcarr
                    How do you init it? Do you use default m = HMC5883L.HMC5883L()
                    or you pass some parameters?
                    Without parameters `HMC5883L()'
                    it default use this i2c init

                    I2C(0, I2C.MASTER, baudrate=100000)
                    

                    you can change this by e.g.

                    bus = I2C(0, I2C.MASTER, baudrate=100000, pins=(SDA, SCL))
                    

                    e.g. if you connect i2c to pins SDA=P9, and SCL=P10
                    simply do

                    bus = I2C(0, I2C.MASTER, baudrate=100000, pins=('P9', 'P10'))
                    m = HMC5883L.HMC5883L(bus)
                    

                    and check address of you sensor
                    default is sensor_address=30

                    if you have different address e.g. 50 change it in the init

                    bus = I2C(0, I2C.MASTER, baudrate=100000, pins=('P9', 'P10'))
                    m = HMC5883L.HMC5883L(busI2C=bus,sensor_address=50)
                    

                    think about people first, then computers

                    1 Reply Last reply Reply Quote 1
                    • L Offline
                      Leo_Sol
                      last edited by

                      The libary works fine (except for missing check in heading() for x and y to not be None) but the sensor is not useable.

                      You can not trust the results - is is more like gambling and extremly depending on your surrounding. Dont buy it, if you need reliable data!

                      R L 2 Replies Last reply Reply Quote 0
                      • R Offline
                        roadfox @Leo_Sol
                        last edited by

                        @leo_sol I think this Module only works properly with calibration, and the library lacks some calibration functionality so i agree with you in the current situation the module is useless.

                        1 Reply Last reply Reply Quote 0
                        • L Offline
                          livius @Leo_Sol
                          last edited by

                          @leo_sol
                          It depend on environment. For me it is quite precise.
                          But as @roadfox say and i in first post introduction about optimized library
                          that library is without calibration part and good as first steep.

                          It is optimized for memory allocation but do not support full sensor features yet

                          think about people first, then computers

                          1 Reply Last reply Reply Quote 0

                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                          With your input, this post could be even better 💗

                          Register Login
                          • 1 / 1
                          • First post
                            Last post

                          Pycom on Twitter

                          Pycom