This tutorial will guide you to work with our XENSIV Digital Pressure Sensors (DPS3xx) sensing barometric pressure and temperature.
Let’s get started!
1 Features and ComparisonInfineons DPS310 and DPS368 come in a Shield2Go version which can mount easily on an XMC2Go or stacked on top of a My-IoT-Adapter as well as a Kit2Go version which comes with a XMC1100 controller onboard so you can directly plug it into your PC with a micro-USB and start programming.
Both the DPS310 and DPS368 are pretty similar in terms of:
- Operation range: 300 to 1200 hPa
- Temperature: -40 to 85 °C
- Precision: +/- 0.002 hPa (so basically +/- 2cm)
- Takes about 28 ms for one measurement (in standard mode)
- Interface: I²C and SPI
- Operating Voltage: 1.7 to 3.6 V
The main difference lies in the package of the sensor. The DPS368 has a more environmentally resistant package. It can withstand 50m underwater for up to one hour.
IMPORTANT: THE COVER IMAGE IS FOR FUN-PROMO PURPOSES ONLY, NEITHER THE SHIELDS2GO NOR KITS2GO ARE WATERPROOF!!! ONLY the DPS368 Chip itself can be used in waterproof applications (IPx8 certified).
2 PinoutAll DPS3xx Shields and Kits2Go come with the same pinout:
Originally the boards come in the I²C interface mode (Resistors soldered on the inner soldering spots). By simply resoldering the 0 Ohm resistors to the outer soldering spots the interface mode changes to SPI mode.
All variants have a breakable head, so you can use the DPS individually on a small breakout board. Then you'd have to use two 10kOhm I²C pull-up resistors for the use of I2C: One between SCL-3V3
and one between SDA-3V3
.
IMPORTANT: DO NOT TRY TO CONNECT THE SHIELD2GO OR KIT2GO DIRECTLY TO A BOARD WITH A 5V OPERATING VOLTAGE. For that, you can either use the MyIot Adapter (Hackster article here) or apply proper level shifting (5V => 3V3).
3 Setup and Code with ArduinoBefore the action starts, if you're not familiar with XMCforArduino, check out this Hackster post to help you set up your environment.
After setting up your environment you can start by downloading the latest DPS3xx library from grithub or adding the library via the library manger in the Arduino IDE:
NOTE: You may be familiar with (or actually used) the DPS310 Library or DPS368 Library before. To make things easier, this one unites both, so you can use it for DPS368 or DPS310 whether Kit2Go or Shield2Go.
The Kit2Go version is plug-and-play, so you don't need any additional hardware (except for a micro-USB cable) to program it. If you're using the Shield2Go version you can use every supported microcontroller board within the IDE.
Now let's take an overview of the important methods of the DPS Class, that you're probably going to use.
First and foremost include the library itself with:
#include <Dps3xx.h>
Now define a DPS3xx Object and call its constructor:
Dps3xx Dps3xxPressureSensor = Dps3xx();
Scroll down to the setup() function and initialize your DPS in SPI mode or I²C mode, depending on which interface version you have:
Serial.begin(9600);
...
Dps3xxPressureSensor.begin(Wire);
or
Dps3xxPressureSensor.begin(SPI, pin_cs);
NOTE: The CS/Chip Select pin (sometimes SS/Slave select pin) varies from board to board. Have a look at the respective pinout diagram to know which pin is the right Pin of the board you're using. (Pinout diagrams are usually in the Wiki Section of Infineon's XMC-for-Arduino repository)
Now, there are two modes for the sensor:
- Background Mode
- Command Mode
In Background Mode, pressure and/or temperature measurements are performed continuously according to the selected measurement precision and rate (temperature measurement is performed immediately after the pressure measurement).
NOTE: A FIFO is available to store 32 measurement results and minimize the number of times the sensor must be accessed to read out the results.
FIFO means "First In, First Out." It's a method where the first measurement added is the first one to be removed. It follows a queue-like behavior, processing items in the order they were added.
To enable background mode type the following in the setup()-function:
Dps3xxPressureSensor.startMeasureTempCont(temp_mr, temp_osr);
or
Dps3xxPressureSensor.startMeasurePressureCont(prs_mr, prs_osr);
or
Dps3xxPressureSensor.startMeasureBothCont(temp_mr, temp_osr, prs_mr, prs_osr);
As you can see the function takes in two parameters which allow you to alter the precision of the DPS by setting the:
- oversampling rate (OSR): value from 1 to 128 (1 being the lowest precision and 128 the highest)
and
- measuring rate (MR): how many measurements/second; also from 1 to 128 (ONLY IN BACKGROUND MODE).
NOTE: When measuring both continuously, you have to enter 4 parameters starting with temperature first and then pressure.
In terms of code, parameters OSR and MR are a number between 0 to 7. The actual value is always a power of two, so the parameters correspond to the exponent. E.g:
uint8_t temp_mr = 2; //actual value: 2^(temp_mr)
uint8_t temp_osr= 2; // actual value: 2^(temp_osr)
ATTENTION: If the measuring rate is n and oversampling rate is m, the DPS3xx performs 2^(n+m) internal measurements per second. The DPS cannot operate with high precision and high speed at the same time. Refer to the respective datasheet (DPS310, DPS368) for more information.
Now to actually read the measurements, head over to the loop() section and type in the following:
uint8_t pressureCount = 20; //for example
float pressure[pressureCount];
uint8_t temperatureCount = 20;// for example
float temperature[temperatureCount];
...
Dps3xxPressureSensor.getContResults(temperature, temperatureCount, pressure, pressureCount);
temperature/pressureCount:
is the number of temperature or pressure measurements (remember to not exceed 32)temperature/pressure[]:
is an array of type float to save the temperature or pressure measurements in.
NOTE: E.g. If you don't want the temperature values and only want the pressure values, replace the parameter that you don't want with NULL
.
This only saves them in their arrays, to actually read them out from the serial monitor, you're going to need some loops:
Serial.println();
Serial.print(temperatureCount);
Serial.println(" temperature values found: ");
for (int16_t i = 0; i < temperatureCount; i++)
{
Serial.print(temperature[i]);
Serial.println(" degrees of Celsius");
}
...
Serial.println();
Serial.print(pressureCount);
Serial.println(" pressure values found: ");
for (int16_t i = 0; i < pressureCount; i++)
{
Serial.print(pressure[i]);
Serial.println(" Pascal");
}
And there you go! Now you can read out the temperature and pressure values from the DPS:
In Command Mode, one temperature or pressure measurement is performed according to the selected precision. The sensor will return to Standby Mode (will not perform any measurements) when the measurement is finished.
To start the measurements type in the following in the loop() section:
float temperature;
float pressure;
uint8_t oversampling = 7;
int16_t ret;
...
Dps3xxPressureSensor.measureTempOnce(temperature, oversampling);
...
Dps3xxPressureSensor.measurePressureOnce(pressure, oversampling);
Remember, this only stores them in their respective variables (temperature or pressure). To read them out from the serial monitor, use the Serial.println();
function of Arduino:
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degrees of Celsius");
...
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" Pascal");
Because it's in the loop() you might want to add some delay either at the end or at the beginning with the delay();
function.
And There you have it, you can now read out temperature and pressure!
4 Farewell and potential appsSo, this is the end of this article. Plug-and-play: As easy as it can get.
Make sure to check out the Example codes in the Attachment section.
Also check out these guys, who made a digital ping-pong game with the DPS310 Shield2Go as the racket:
As you can see there are various applications for the DPS. It's safe to say that the DPS can indeed work under pressure :-D
Be creative, stay safe, and Goodbye!
Comments