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. Development
  3. Async sensor Libraries (SI7021, APDS-9306)

Async sensor Libraries (SI7021, APDS-9306)

Scheduled Pinned Locked Moved Development
3 Posts 3 Posters 1.2k Views 3 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.
  • A Offline
    A Offline
    Artix
    wrote on last edited by
    #1

    Hello,

    Following the presentation of my project gdomotics : https://forum.mysensors.org/topic/9009/gdomotics

    I write this post to share my work on 2 new libraries :

    AsyncSI7021 : https://github.com/gmarti/AsyncSI7021
    AsyncAPDS9306 : https://github.com/gmarti/AsyncAPDS9306

    During my test of the existing libraries, I was really frustrated the way they were written, especially a lot of unnecessary waiting when measuring with multiple sensors.

    With those new libraries, you can start a measurement, continue any other task you want and get back the result when it is ready.

    Old way of measuring sensors:

    def syncMeasurement() : int{
      Wire.write(requestMeasurementRegister)
      delay(25)
      return Wire.read(resultRegister)
    }
    
    def syncMeasurement() : int{
      Wire.write(requestMeasurementRegister)
      While(Wire.read(mesurementStatusRegister) != terminated){ }
      return Wire.read(resultRegister)
    }
    

    Async measuring of sensors:

    def startMeasurement() : void{
      Wire.write(requestMeasurementRegister)
    }
    
    def isMeasurementReady() : bool{
      return  Wire.read(mesurementStatusRegister) == terminated
    }
    
    def getMeasurement() : int{
      return Wire.read(resultRegister)
    }
    

    With this you can write this:

    AsyncSI7021 si7021;
    AsyncAPDS9306 apds9306;
    
    
    while(!apds9306.begin()){} //Wait for sensor to be ready
    apds9306.startLuminosityMeasurement();
    
    while(!si7021.begin()){} //Wait for sensor to be ready (SI7021 has 18-80ms of powerup time)
    si7021.startHumidityMeasurement();
    
    bool isSi7021Measuring = true;
    bool isApds9306Measuring = true;
    
    do{
      if(isSi7021Measuring && si7021.isMeasurementReady()) {
        send(si7021.getTemperature());
        send(si7021.getHumidityFromTemperatureMeasurement());
        isSi7021Measuring = false;  
      }
      if(isApds9306Measuring && apds9306.isMeasurementReady()) {
        send(apds9306.getLuminosity());
        isApds9306Measuring = false;      
      }
    
       //Another task here !!
    
    while(isSi7021Measuring  || isApds9306Measuring);
    
    

    With this code, you don't know if si7021 or apds9306 will finish measuring first.

    And you don't care! Because the total measurement time will more or less be max(SI7021time, apds9306time, otherSensorTime, otherTaskTime)

    SI7021 max measurement time (12bit RH 14bit Temp) : 12 + 10.8 = 22.8ms
    APDS9306 max measurement time : from 25ms to 400ms

    With async code, you will spare more or less 22.8ms in your sketch with this 2 sensors

    With more sensors, you will spare a lot more! (async battery measurement, async everything !)

    Spare time == less battery use == happy me

    I really hope it will be useful for the community.

    Thanks

    Artix

    PS : This is the first release of those libraries, they have been tested only by me for the moment. Things can go wrong and some functionality (mostly interrupts)
    does not exist.

    1 Reply Last reply
    5
    • scalzS Offline
      scalzS Offline
      scalz
      Hardware Contributor
      wrote on last edited by scalz
      #2

      hi,

      async/SM is a good habit . no delays, code freely running, always makes me happy too :) thx for sharing :+1:
      I did the same for si7021 too, and a few others like opt3001 (async + C for mem footprint)

      Keep the good work!

      alexsh1A 1 Reply Last reply
      0
      • scalzS scalz

        hi,

        async/SM is a good habit . no delays, code freely running, always makes me happy too :) thx for sharing :+1:
        I did the same for si7021 too, and a few others like opt3001 (async + C for mem footprint)

        Keep the good work!

        alexsh1A Offline
        alexsh1A Offline
        alexsh1
        wrote on last edited by
        #3

        @scalz You could share your code too :relaxed:

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


        28

        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