Home

Awesome

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

FolderNumber & NameDescription
SF0401 Simple MeasurementReads simple measurement data from the<br>sensor.
SF0402 Set ResolutionReads measurement data from the sensor and<br>changes the resolution.
SF0403 Read Scale Factor and UnitReads the scale factor and measurement unit<br>information from the sensor's EEPROM.
SF0404 Read Product DetailsReads the product details (serial number and product name).
SF0405 CRC ChecksumReads flow measurement data from the sensor<br>and calculates the checksum of the<br>communication for error detection.
SF0410 Calibration FieldDescribes the necessary steps to change the<br>calibration field settings.
SF0411 Read Temperature VoltageDemonstrates how to read temperature and<br>voltage data.
SF0412 Two's ComplementDemonstrates how to use the signed datatype<br>interpretation directly and how to calculate<br>the two's complement manually.
SF0413 DIY Flow MeterExpands on example 3 to build a stand-alone<br>flow meter using a graphic LCD.

SF06

FolderNumber & NameDescription
SF0614 Simple measurement (LD20-2600B)Reads simple measurement data from the<br>LD20-2600B.
SF0615 Simple measurement (SLF3S-1300F)Reads simple measurement data from the<br>SLF3S-1300F and highlights where<br>changes have to be made for other SF06<br>sensors.
SF0616 CANBUS receiveDemonstrates how to forward measurement data over CANBUS.<br>This is useful for some test setups.
SF0617 CANBUS sendDemonstrates how to forward measurement data over CANBUS.<br>This is useful for some test setups.
SF0618 DIY closed loop volume controllerDemonstrates how an SLF3x can be used<br>together with a pump to dispense a controlled volume.
SF0619 DIY flow meterStand-alone flow meter using a praphic LCD (SF06 sensors).
SF0620 simple measurement multiple SLF3xDemonstrates how to use an I2C multiplexer<br>(TCA9548A) to communicate with up to 8<br>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:

FunctionsDescription
Wire.begin()Join I2C bus (address optional for<br>master).
Wire.beginTransmission(ADDRESS)Initiate a transmission transaction <br>with the device at ADDRESS<br>over I2C.
Wire.write(value)Queues bytes for transmission from<br>master to slave.
Wire.endTransmission()Commit the transmission transaction:<br>start, send data, stop.
Wire.requestFrom(ADDRESS, LEN)Master requests data (LEN bytes)<br>from slave at ADDRESS and<br>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.

CommandValueDescriptionSee Example(s)
USER_REG_W0xE2Write user register10, 11
USER_REG_R0xE3Read user register10, 11
ADV_USER_REG_W0xE4Write advanced user register2
ADV_USER_REG_R0xE5Read advanced user register2
READ_ONLY_REG2_R0xE9Read-only register 24
TRIGGER_FLOW_MEASUREMENT0xF1Trigger a flow measurement1-13
TRIGGER_TEMP_MEASUREMENT0xF3Trigger a temperature measurement11
TRIGGER_VDD_MEASUREMENT0xF5Trigger a supply voltage measurement11
EEPROM_R0xFARead EEPROM3, 4, 13
SOFT_RESET0xFESoft reset1-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