Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:17:50+00:00 2026-06-10T23:17:50+00:00

I am using a soft I²C implementation to read a set of Sensirion SHT21

  • 0

I am using a soft I²C implementation to read a set of Sensirion SHT21 sensors. I am trying to figure out a way of having the sensors answer to see if they are actually connected to the device. I am using the Arduino which means all my code is C/C++

The libraries I am using are here.

The code used to read the sensors is the following:

#include <Ports.h>
#include <PortsSHT21.h>

//Define soft I²C channels for three sensors
SHT21 hsensor2 (2); // pins A1 and D5 - Sensor 2

//define variables for temp data
float h, t;

void setup() {}

void loop()
{
    // Get data from sensor soft I²C
    hsensor2.measure(SHT21::HUMI);
    hsensor2.measure(SHT21::TEMP);
    hsensor2.calculate(h, t);
    float hum2 = (h);
    float temp2 = (t);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:17:52+00:00Added an answer on June 10, 2026 at 11:17 pm

    The large code block is the code for the measure() function. Notice that it returns 0 at one point without performing a connReset(). This should be a way to detect a valid device such as …

    bool hasHUMI;
    if (hsensor2.measure(SHT21::HUMI))
    {
      hasHUMI=true;
    }
    

    or

    if (hsensor2.measure(SHT21::HUMI) && hsensor2.measure(SHT21::TEMP))
    {
     hsensor2.calculate(h, t); 
     float hum2 = (h); 
     float temp2 = (t);    
    }
    

    or

    Your code should be clearing h and t to 0 before making the read so that you can test for valid values. Like this…

    void loop() 
    { 
        h=0.00f;
        t=0.00f;
        // Get data from sensor soft I²C 
        hsensor2.measure(SHT21::HUMI); 
        hsensor2.measure(SHT21::TEMP); 
        hsensor2.calculate(h, t); 
        float hum2 = (h); 
        float temp2 = (t); 
        if (h>0) {
        }
        if (t>0) {
        }
    } 
    

    If not then you could make (copy) your own version of the measure() function that tests for valid return value in meas[type]. You would need to set meas[type] to a known invalid value before the read (such as 0).

    uint8_t SHT21::measure(uint8_t type, void (*delayFun)()) {
    
        start();
        writeByte(type == TEMP? MEASURE_TEMP : MEASURE_HUMI)
    
        for (uint8_t i = 0; i < 250; ++i) {
            if (!digiRead()) {
                meas[type] = readByte(1) << 8;
                meas[type] |= readByte(1);
                uint8_t flipped = 0;
    
                for (uint8_t j = 0x80; j != 0; j >>= 1) {
                    flipped >>= 1;
                }
    
                if (readByte(0) != flipped)
                    break;
    
                return 0;
            }
    
    
            if (delayFun)
                delayFun();
            else
                delay(1);
        }
    
        connReset();
        return 1;
    }
    

    You probably know that if you add a method to a library cpp then you also need to add a corresponding prototype to the .h otherwise the arduino will fail to compile your code.

    .cpp

    uint8_t SHT21::measureTest(uint8_t type, void (*delayFun)()) {
    
        }
    

    .h

    uint8_t measureTest(uint8_t type, void (*delayFun)() =0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an Android Soft keyboard application using avail code at
I created a cache using Soft References a while ago, but in trying to
Is there any way to determine whether a linux executable is using soft float
Using Android (Eclipse) I am trying to read a .csv file and dynamically populate
I'm trying to deactivate the soft keyboard when using a NumberPicker to enter numerical
For some reason, Vim keeps using soft-tabs (tabs as spaces) everytime I'm working on
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using SQL Server 2008 R2 we are looking for a way to select the
I'm using soft deleting in my database ( IsDeleted field). I'm actively using LoadWith
I am using a Video Soft Vs Elastic control and now when ever my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.