Giter Club home page Giter Club logo

sensirion / arduino-liquid-flow-snippets Goto Github PK

View Code? Open in Web Editor NEW
22.0 13.0 15.0 38 KB

Arduino Code Snippets for Raw Sensor Communication with Sensirion Liquid Flow Sensors

Home Page: https://web.archive.org/web/20220816230009/https://developer.sensirion.com/archive/tutorials/arduino-interface-for-liquid-flow-sensors/

License: BSD 3-Clause "New" or "Revised" License

C++ 100.00%
arduino liquid-flow-sensor sensirion flow-meter ld20 ls32 lpg10

arduino-liquid-flow-snippets's Introduction

Sample Code for Arduino

Summary

The Arduino Platform allows for easy prototyping with endless possibilities. In order to enable our customers to use this platform, we provide sample code for use with Sensiron AG's liquid flow sensors. This application note describes the examples, as well as the main code components used in the examples. It is meant as a starting point for setting up communication with the liquid flow sensors through the I2C interface.

Compatibility

The code snippets are generally compatible with Sensirion's digital flow sensor portfolio. The current portfolio is based on two different sensor chips, the SF04 and the SF06. More specifically:

SF04

SLG, SLI, SLS, SLQ-QTxxx, LG16-xxxxD, LS32 and LPG10 series sensors.

SF06

LD20 and SLF3x series sensors.

Introduction

This sample code library is aimed at customers who have successfully set up their Arduino and connected their Sensirion liquid flow sensor. Please see the Arduino Quick Start Guide for help in setting up your Arduino and liquid flow sensor. Sensirion provides code examples showcasing the most important use cases of I2C communication with our liquid flow sensors.

Code Examples

SF04

Folder Number & Name Description
SF04 01 Simple Measurement Reads simple measurement data from the
sensor.
SF04 02 Set Resolution Reads measurement data from the sensor and
changes the resolution.
SF04 03 Read Scale Factor and Unit Reads the scale factor and measurement unit
information from the sensor's EEPROM.
SF04 04 Read Product Details Reads the product details (serial number and product name).
SF04 05 CRC Checksum Reads flow measurement data from the sensor
and calculates the checksum of the
communication for error detection.
SF04 10 Calibration Field Describes the necessary steps to change the
calibration field settings.
SF04 11 Read Temperature Voltage Demonstrates how to read temperature and
voltage data.
SF04 12 Two's Complement Demonstrates how to use the signed datatype
interpretation directly and how to calculate
the two's complement manually.
SF04 13 DIY Flow Meter Expands on example 3 to build a stand-alone
flow meter using a graphic LCD.

SF06

Folder Number & Name Description
SF06 14 Simple measurement (LD20-2600B) Reads simple measurement data from the
LD20-2600B.
SF06 15 Simple measurement (SLF3S-1300F) Reads simple measurement data from the
SLF3S-1300F and highlights where
changes have to be made for other SF06
sensors.
SF06 16 CANBUS receive Demonstrates how to forward measurement data over CANBUS.
This is useful for some test setups.
SF06 17 CANBUS send Demonstrates how to forward measurement data over CANBUS.
This is useful for some test setups.
SF06 18 DIY closed loop volume controller Demonstrates how an SLF3x can be used
together with a pump to dispense a controlled volume.
SF06 19 DIY flow meter Stand-alone flow meter using a praphic LCD (SF06 sensors).
SF06 20 simple measurement multiple SLF3x Demonstrates how to use an I2C multiplexer
(TCA9548A) to communicate with up to 8
SLF3x sensors on the same bus.

Prerequisites

In order to initiate the serial communication, Serial.begin(9600) should be used to set the baud rate to 9600. The code examples require the use of the "Wire"-library. This Arduino library contains all the relevant functions for the communication between your Arduino and the I2C device. The provided code examples are all based on this library and typically use the following functions:

Functions Description
Wire.begin() Join I2C bus (address optional for
master).
Wire.beginTransmission(ADDRESS) Initiate a transmission transaction
with the device at ADDRESS
over I2C.
Wire.write(value) Queues bytes for transmission from
master to slave.
Wire.endTransmission() Commit the transmission transaction:
start, send data, stop.
Wire.requestFrom(ADDRESS, LEN) Master requests data (LEN bytes)
from slave at ADDRESS and
store to buffer.
Wire.available() Retrieve the length of the I2C buffer.
Wire.read() Read data from the I2C buffer.

For further details, please refer to Arduino's official Wire Documentation.

SF04 Sensor Commands

The following table shows the commands that are typically used for I2C communication. The commands need to be transmitted through the Wire.write(Value) function that is part of Arduino's Wire library for I2C communication.

Command Value Description See Example(s)
USER_REG_W 0xE2 Write user register 10, 11
USER_REG_R 0xE3 Read user register 10, 11
ADV_USER_REG_W 0xE4 Write advanced user register 2
ADV_USER_REG_R 0xE5 Read advanced user register 2
READ_ONLY_REG2_R 0xE9 Read-only register 2 4
TRIGGER_FLOW_MEASUREMENT 0xF1 Trigger a flow measurement 1-13
TRIGGER_TEMP_MEASUREMENT 0xF3 Trigger a temperature measurement 11
TRIGGER_VDD_MEASUREMENT 0xF5 Trigger a supply voltage measurement 11
EEPROM_R 0xFA Read EEPROM 3, 4, 13
SOFT_RESET 0xFE Soft reset 1-13

SF06 Sensor Commands

SF06 based sensors have a simplified I2C interface. The measurement commands are calibration specific and therefore depend on the respective sensor. The available commands are listed in the sensors' datasheets.

Two's Complement

The two's complement is a binary number representation. This notation is widely used in computer science to represent both positive and negative integer numbers. To get the two's complement notation of a negative integer, first write the absolute value of that integer in binary representation b = bin(abs(x)). Then invert the binary bit sequence b (i.e. change every 1 into a 0 and every 0 into a 1). Finally, increment the number represented by b by one. Note that all negative numbers will thus carry a 1 in their highest bit.

Example: Represent -5 in 8bit

  1. binary representation of absolute value: b = bin(abs(-5)) -> 0000 0101
  2. invert: b = xor(b, 1111 1111) -> 1111 1010
  3. Increment by one: b = b + 1 -> 1111 1011

arduino-liquid-flow-snippets's People

Contributors

abrauchli avatar danielbrownjr avatar winkj avatar zifzaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arduino-liquid-flow-snippets's Issues

Wire.requestFrom() should not be followed by Wire.endTransmission()

The Wire.endTransmission() is only used when writing data. It should not be used after the Wire.requestFrom().

The Wire.requestFrom() is a complete I2C transaction on its own. It does the start, sending the i2c address, reading of the data bytes and the stop.

At this moment a Wire.endTransmission() without Wire.beginTransmission (for the Wire library for AVR chip) sends out an i2c address and reads the acknowledge by the sensor. This results into an unnecessary I2C transaction on the bus. It could result in an error when the Wire library is changed and it could be bad with implementations of the Wire library for other processors.

To check if the Wire.requestFrom() is successful, the return value could be checked or a Wire.available() could be called directly after it. The number of received bytes should be the same as the number of requested bytes.

The problem is in these files:
example_01_simple_measurement.ino (once)
example_02_set_resolution.ino (twice)
example_03_read_scalefactor_and_unit.ino (three times)
example_04_read_product_details.ino (just once, only at the bottom in the loop function)
example_05_crc_checksum.ino (once)
example_10_calibration_field.ino (twice)
example_11_read_temperature_voltage.ino (once)
example_12_twos_complement.ino (once)
example_13_DIY_flow_meter.ino (three times)

SLF3S-1300F Liquid flow sensor I2C via arduino uno provides no useable ic2 signal using snippets

Dear Sensirion

Purpose of the project: choice of sensor for a sub project in liquid handling. To to verify if SLF3S sensor ic2 data can be handled, displayed and written -as pilot project before scaling up with multiple sensors per device that will be i2c multiplexed to provide values used for PID controlled feedback loops.

Purpose of this inquiry: to obtain either a working code for proof that Arduino Uno, any other type, can handle the sensor derived I2C data flow temp and flags or a suggestion to abandon i2c devices. Any help is valued, any costs assosciated with this inquiry may be invoiced.

THANK YOU VERY MUCH

Materials: Arduino uno, 3.3v, no additional pullups, wired as datasheet-instructed, cable length appros 20 cm. SLF3S-1300F sensor evaluation kit.

Description of the problem: No matter how the address of the sensor is modified, the above mentioned and below provided code keeps outputting: "Error while sending soft reset command, retrying..." on the serial monitor.
The sensor could NOT be detected by several different 12c scanners (scanning for 7 bit).
The sensor is working FINEwith the provided RS485-USB cable using the viewer software.
3.3V OP verified
Oscilloscope reading of SCL and SCL during serial monitor updates the serial monitor error : periodically (pulled down from 2v to 0V
grafik

all Sensirion-provided codes were tried out (with modified sensor addresses from 4 to 8) and include:

/*
 * Copyright (c) 2018, Sensirion AG
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of Sensirion AG nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*******************************************************************************
 * Purpose:  Example code for the I2C communication with Sensirion Liquid
 *           Flow Sensors
 *
 *           Read measurements from the sensor
 ******************************************************************************/

#include <Wire.h> // Arduino library for I2C

const int ADDRESS = 0x80; // Standard address for Liquid Flow Sensors

// -----------------------------------------------------------------------------
// Arduino setup routine, just runs once:
// -----------------------------------------------------------------------------
void setup() {
  int ret;

  Serial.begin(9600); // initialize serial communication
  Wire.begin();       // join i2c bus (address optional for master)

  do {
    // Soft reset the sensor
    Wire.beginTransmission(ADDRESS);
    Wire.write(0xFE);
    ret = Wire.endTransmission();
    if (ret != 0) {
      Serial.println("Error while sending soft reset command, retrying...");
    }
  } while (ret != 0);

  delay(50); // wait long enough for chip reset to complete
}

// -----------------------------------------------------------------------------
// The Arduino loop routine runs over and over again forever:
// -----------------------------------------------------------------------------
void loop() {
  int ret;
  uint16_t raw_sensor_value;
  int16_t signed_sensor_value;

  // To perform a measurement, first send 0xF1 to switch to measurement mode,
  // then read 2 bytes + 1 CRC byte from the sensor.
  Wire.beginTransmission(ADDRESS);
  Wire.write(0xF1);
  ret = Wire.endTransmission();
  if (ret != 0) {
    Serial.println("Error during write measurement mode command");

  } else {
    Wire.requestFrom(ADDRESS, 2);       // reading 2 bytes ignores the CRC byte
    if (Wire.available() < 2) {
      Serial.println("Error while reading flow measurement");

    } else {
      raw_sensor_value  = Wire.read() << 8; // read the MSB from the sensor
      raw_sensor_value |= Wire.read();      // read the LSB from the sensor

      Serial.print("raw value from sensor: ");
      Serial.print(raw_sensor_value);

      signed_sensor_value = (int16_t) raw_sensor_value;
      Serial.print(", signed value: ");
      Serial.println(signed_sensor_value);
    }
  }

  delay(1000); // milliseconds delay between reads (for demo purposes)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.