TWI interface with flow sensor works incorrectly

Hello!

I have been trying to read a flow sensor with the nRF52840 DK, but the output is unexpected.

It has been proven that the flow sensor is functional, by connecting it with another MCU.

When I used a logic analyzer, the output I received was completely normal, meaning that the flow sensor is actually sending some values, and the DK receives them correctly. The thing is that the values that the flow sensor sends are incorrect. So, the flow sensor gives off incorrect values, but there is no problem with the DK.

After that, I tried to find what was wrong, and for the life of me, I could not see anything wrong with my code. As a result, I started all over from the TWI sensor example. Nothing changed. Even at the most basic level of TWI code, the registers I am reading are giving me the wrong info. What is curious is that when I use the same sensor with a BBB board, where I do not know and cannot change the code that is being executed, the flow sensor gives correct values. So it is not a sensor problem.

I am attaching you the twi code and the sensor's datasheet, where I have highlighted some lines on page 6, so you can understand a bit about the register I am trying to read. Chapter 2.4 is also quite useful. The sensor, by default, jumps into reading the flow register, and so I do not change the register read in my code. In the code, I am just shooting the sensor's flow registers values in the Terminal, every time a twi event occurs. I do not try to translate the data into real values, since I see that the raw register values are incorrect. The output of the code is shown in the figure. The red square contains the 7F-FF registers that denote the full-scale positive flow, which is totally incorrect, since I cannot produce such a flow (500LPM).

Do you have any idea what I am doing wrong?

I am truly pissed, because I know that it is a very simple application, but I cannot get it to work.

Thank you in advance,

Kostas

7723.twi_sensor_mine.zip4571.ESF Series I2C Interface_v2.pdf

  • Hey nordicator!

    You were totally right! It worked with the logic level converter! Thank you very much!

    One last question though, about the voltages.

    I am using a logic level converter which works between 5V and 3.3V. I have connected its high VDD pin to the 5V supply pin of the DK and its low VDD to the VDD pin of the DK, which is 3V. The low VDD of the logic level converter is recommended to be connected to a 3.3V supply though. Do you think, that there will be any problems in the data transfer, due to the supply not being 3.3V, but around 3V?

    Thank you again,

    Kostas

  • Awesome! Glad it's working.

    And yes, I think whatever voltage is actually at VDD is the actual reference voltage you would want to use for IO. So even if it isn't exactly 3.3V it should match the IO voltage reference exactly. I'm not 100% sure on that but thinking of it that way has always given me good results at least.

  • Hey Nordicator, I have one more question concerning this project.

    I am trying to read the Temperature and the Flow registers of the sensor every 10ms, but I am stepping into erroneous behavior.

    More accurately, I am changing the register to read to the Temperature one and I try to get a reading.

    After that, I am changing the register to read to the Flow one and try to get a reading.

    These steps happen in the main while loop

    Unfortunately, I get an NRF_ERROR_BUSY error.

    What is weird is that, when I am running the code in the Debug, everything runs smoothly. That leads me to believe that there is some issue with the timing of the code.

    I am attaching you the updated code (twi_sensor_mine(2).zip) that produces the following output in the terminal:

    Do you have any idea, on what is wrong?

    Thank you,

    Kostas

    twi_sensor_mine (2).zip

  • Hi,

    You need to wait for the m_xfer_done flag to be set in the callback when the transfer completes, before starting a new transfer. Add this after line 104, line 141 and line 152 in your main.c file:

    while(m_xfer_done == false)
    {
        // Do nothing or sleep
    }

    The TWI driver can only handle one transfer at a time, and the APIs are asynchronous when a event handler is provided. When you build the application in Debug configuration, optimization level is set lower, which will typically make the application run a bit slower, which is likely why the transfer manages to complete before a new transfer is started, compared to the Release build.

    Best regards,
    Jørgen

  • Hello Jorgen! Thanks for the prompt reply!

    I added the lines you suggested after the APP_ERROR_CHECK lines you suggested, but nothing changed. The TWI performs the register change successfully, enters the twi_event handler and accurately executes it. But in the first call of the read_sensor_data, the same error is presented on line 104.

    Do you have any idea what I should try after that?

    FYI, line 104 is the APP_ERROR_CHECK line.

    Thank you,

    Kostas

Related