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

    i2c Return Data Conversion

    Scheduled Pinned Locked Moved MicroPython
    micropythonlopyi2c
    4 Posts 2 Posters 1.2k Views 2 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.
    • D Offline
      dchappel
      last edited by

      I have been successful in bringing back data from an i2c analog input device.

      Data comes back as two bytes. To convert the data to engineering units, I need to combine the two bytes to a 16 bit word and then convert to decimal. Example data coming back for the two bytes in the return array from a i2c.readfrom(addr,2) instruction:

      Decimal: 100 214
      Hex: b'd\xd6'

      This is 0110010011010110 in combined 16 bit form which then can be converted to 25814 decimal. This decimal number is proportional to the incoming signal and can be used to convert to engineering units.

      Have spun my wheels for awhile trying to figure out how to do combine [0] and [1] and convert to decimal in code. Throwing myself on the collective braintrust of the forum for assistance.

      Thanks!

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

        @dchappel
        it is quite simple:

        (100 << 8) + 214
        

        same on var:

        data = i2c.readfrom(addr,2)
        value = (data[0] << 8) + data[1]
        

        think about people first, then computers

        D 1 Reply Last reply Reply Quote 3
        • D Offline
          dchappel @livius
          last edited by

          @livius

          That works!

          What is the "<<8" notation called so I can read up more on how to use it?

          Thanks for the rapid reply...

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

            @dchappel
            bit left shift

            <<
            

            same for bit right shift

            >>
            

            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