Writing A Sensor Library/Driver

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
RedDirt
Posts: 8
Joined: Fri Nov 27, 2020 1:43 am

Writing A Sensor Library/Driver

Post by RedDirt » Tue Dec 15, 2020 6:19 am

If one were so inclined to take a stab at writing the code for a library to work with a particular sensor, where would you start? I'd like to especially learn how to port a library that is available for Arduino but not yet for Micropython.

Where would one start on this journed? What information would I need to be looking for?

cgglzpy
Posts: 37
Joined: Thu Jul 18, 2019 4:20 pm

Re: Writing A Sensor Library/Driver

Post by cgglzpy » Tue Dec 15, 2020 6:37 pm

There is a nice talk about that: An Introduction to Hardware Drivers in Micro Python

To sum up:
  • Understand the datasheet
  • Know how to write/read to/from the sensor registers (it varies a bit depending on the protocol used I2C/SPI/UART)
  • Also understanding bitwise operations, binary/hexadecimal numbering system and how this representations works in python will help a lot

    Ex:

    Code: Select all

    >>> bin(42)
    '0b101010'
    >>> hex(42)
    '0x2a'
    >>> 0b101010
    42
    >>> 0x2a
    42
    >>> 

RedDirt
Posts: 8
Joined: Fri Nov 27, 2020 1:43 am

Re: Writing A Sensor Library/Driver

Post by RedDirt » Wed Dec 16, 2020 8:34 am

Outstanding! Thank you cqqlzpy.
cgglzpy wrote:
Tue Dec 15, 2020 6:37 pm
There is a nice talk about that: An Introduction to Hardware Drivers in Micro Python

To sum up:
  • Understand the datasheet
  • Know how to write/read to/from the sensor registers (it varies a bit depending on the protocol used I2C/SPI/UART)
  • Also understanding bitwise operations, binary/hexadecimal numbering system and how this representations works in python will help a lot

    Ex:

    Code: Select all

    >>> bin(42)
    '0b101010'
    >>> hex(42)
    '0x2a'
    >>> 0b101010
    42
    >>> 0x2a
    42
    >>> 

RedDirt
Posts: 8
Joined: Fri Nov 27, 2020 1:43 am

Re: Writing A Sensor Library/Driver

Post by RedDirt » Tue Dec 22, 2020 12:23 pm

Since making this post I have been trying to find some simple device that uses I2C that I could attempt to write a library for, for the experience and the experience of it. However, I am coming up short. Does anyone have some ideas? I need to start with something relatively simple, not something like the BME680 or something insane like that. Something I can buy the sensor for and get the datasheet for and start working through the process of how to actually write the library and test it out.

shaoziyang
Posts: 347
Joined: Sun Apr 17, 2016 1:55 pm

Re: Writing A Sensor Library/Driver

Post by shaoziyang » Tue Dec 22, 2020 1:26 pm

You can refer to my micropython library:

https://github.com/micropython-Chinese- ... ty/mpy-lib

RedDirt
Posts: 8
Joined: Fri Nov 27, 2020 1:43 am

Re: Writing A Sensor Library/Driver

Post by RedDirt » Tue Dec 22, 2020 2:04 pm

You're my hero. How/Where did you learn to do that?
shaoziyang wrote:
Tue Dec 22, 2020 1:26 pm
You can refer to my micropython library:

https://github.com/micropython-Chinese- ... ty/mpy-lib

shaoziyang
Posts: 347
Joined: Sun Apr 17, 2016 1:55 pm

Re: Writing A Sensor Library/Driver

Post by shaoziyang » Wed Dec 23, 2020 9:11 am

It's not difficult, just try more, and more discussion.

RedDirt
Posts: 8
Joined: Fri Nov 27, 2020 1:43 am

Re: Writing A Sensor Library/Driver

Post by RedDirt » Wed Dec 23, 2020 3:27 pm

This is rather long, and I have not read through the whole thing as of yet, but it appears to be a good document on porting/writing libraries for sensors.

https://www.digikey.fi/fi/maker/project ... 1725f28b1b

Post Reply