Newbie Questions: RPi and Sensor Programming

Hi all,

I’m struggling to actually move forward with what I thought would be a relatively easy little project. I have purchased the following items:

I started with the DPS310 temp. / pressure sensor and connected it to the breadboard and then to the RPi. I then wrote a Python script (importing the “adafruit_dps310” library) which wrote the temperature and pressure to a file every half an hour. All this worked no problem.

The DPS310 sensor is also meant to provide the Altitude. Upon reading various posts as well as looking at the GitHub repository, it appeared that the Altitude function was a recent inclusion into the C / C++ library only (and not the Python library), so I then began a C++ script in Geany, but gave up because I couldn’t compile the code as it just kept wanting to “#include” Arduino library after library after library.

I then installed the Arduino IDE, followed this tutorial to ‘build Arduino Sketches for Pi’ to link the RPi to the Arduino IDE. Attempting to compile the program, I then began getting “‘BitOrder’ has not been declared” errors. From the GitHub repo., I took their example code directly, put it into a new Sketch without alteration, and attempted to compile it, and received exactly the same ‘BitOrder’ error.

After getting my Python script working - reading the Temp and Pressure (only) - I then connected the CO2 sensor to the breadboard and jumpered the cables across from the DPS310. So far, I’ve not been able to ‘communicate’ with this CO2 sensor at all, even though the DPS310 still works fine through my Python script.

I have not yet even attempted to do anything with the SHT-10 soil sensor, but ideally, once I have the first 2 sorted, then that would be next.

Questions:

  1. How do you ordinarily suggest to ‘communicate’ with sensors? I.e., should I use pre-built libraries, or can you somehow communicate direct to the sensor using say I2C and get each of the values that you’re after (i.e., altitude, temperature and pressure, then CO2 etc. on the next sensor etc.)?
  2. If Arduino have a given library (which I believe they do for both the DPS310 and SEN-15112, then should I persist in trying to use their IDE, or should I scrap it totally and program it directly some other way?
  3. When I run the sudo i2cdetect -y 1 Linux command, I see a number “77” even though - in theory - I have 2 sensors connected. Is this what I should expect to see when 2 or more sensors are connected?
  4. As a newbie with RPi and electronics, am I going about this the wrong way? Is there something easier or am I missing something perhaps?

Thanks so much in advance. I would totally appreciate any guidance as I’m really struggling with contemplating the next logical step here.

Thanks again,
Kirk.

Hi Kirk,

How interesting!

May I ask which repo you tried to pull the C++ script from that is supposed to determine altitude from relative pressure? It doesn’t appear to be linked here.

As for the BitOrder error, this is a problem with the setup of the SPI bus between the devices which should be fixed by the same (if not then a similar fix) to that which I’ve found from another discussion at the link below.

https://www.gitmemory.com/issue/MCUdude/MegaCoreX/81/662321071

As for the CO2 sensor, this GitHub library should cover all the setup and scripts that you need in order to run it. You should be able to communicate with it via I2C or Serial, although this library only details the us of an I2C connection by the look of it.

To answer your listed questions:

  1. Pre-built libraries are nearly always the way to go unless the sensor is quite simple. This is due to the fact that most sensors will have different (usually similar) methods of outputting data, but if you’re hardcoding all of it, the scripts can be overly cumbersome as the length and complexity increases.

  2. The IDE is probably the simplest way to work with these scripts. It has a few built-in functions for uploading to the board, interacting with it via serial, plotting outputs, etc.

  3. What may be happening here is a collision (if both devices are assigned to the same address) although this is extremely unlikely as most I2C devices we supply usually all have different addresses. What’s more likely is that one of the devices isn’t connected. Can you try running i2c detect with each configuration to see whether they’re both showing up separately?

  4. As a newbie, I’d recommend going through the tutorials on the site, they’re completely free and public, and if you have any questions about them you can ask directly. There are a few on SPI and I2C protocols too if you haven’t used them in your projects before.

Best of luck with the project!

Hi @Bryce,

Thanks so much for your detailed reply. In terms of the repo, it is this library that I downloaded into the Arduino IDE. I then grabbed the code directly from this example and attempted to compile it.

Much of the reason for me purchasing that DPS310 sensor was because your site said it supported Altitude:

How high are you right now? If you had a precision altitude sensor, you would know for sure! The DPS310 sensor from Infineon a high precision barometric sensor, perfect for measuring altitude changes with a up to ±0.002 hPa (or ±0.02 m) precision high precision mode and ± 1 hPa absolute accuracy.

Thanks so much for the link to the BitOrder error fix. I just implemented that suggestion and recompiled and I now get these errors:

In file included from sketch\DPS310_example.ino.cpp:1:
C:\Program Files (x86)\Arduino\hardware\RaspberryPi\piduino\cores\piduino/Arduino.h:64:18: error: invalid conversion from ‘int’ to ‘BitOrder’ {aka ‘_BitOrder’} [-fpermissive]
#define MSBFIRST 1
^
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:31:31: note: in expansion of macro ‘MSBFIRST’
#define SPI_BITORDER_MSBFIRST MSBFIRST
^~~~~~~~
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:62:43: note: in expansion of macro ‘SPI_BITORDER_MSBFIRST’
BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
^~~~~~~~~~~~~~~~~~~~~
C:\Program Files (x86)\Arduino\hardware\RaspberryPi\piduino\cores\piduino/Arduino.h:64:18: error: invalid conversion from ‘int’ to ‘BitOrder’ {aka ‘_BitOrder’} [-fpermissive]
#define MSBFIRST 1
^
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:31:31: note: in expansion of macro ‘MSBFIRST’
#define SPI_BITORDER_MSBFIRST MSBFIRST
^~~~~~~~
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:67:43: note: in expansion of macro ‘SPI_BITORDER_MSBFIRST’
BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
^~~~~~~~~~~~~~~~~~~~~
exit status 1
Error compiling for board RaspberryPI B+/2.

The tutorials on your site are great. Thank you. I’ve already completed the whole beginners workshop:

https://core-electronics.com.au/tutorials/raspberry-pi/raspberry-pi-workshop-for-beginners.html

It provided some great fundamentals, but nothing specific really about programming for sensors using pre-built libraries etc.

I agree that using the pre-built libraries is the way to go. That is precisely what I anticipated; I guess it was just disappointing to find that the Python library then did not provide the functions for Altitude - which was really the only reason I switched to the C++ libraries instead.

Thanks again Bryce.

Hi Kirk,

No worries, you’re exactly right, the DPS310 doesn’t measure altitude directly, instead, it measures the ambient pressure, and then dependant on the difference between the current pressure and the pressure at sea level you can determine your altitude fairly precisely. This is identical to how it’s done on aircraft (well now also with other backup systems to assist, but that’s a whole topic in itself), the library should cover this conversion from pressure (usually measured in hPa) to metres above sea level. Let us know if you have any problems with setting that up and we’ll see what we can do to fix it.

As for that SPI error, that fix may have been specific to that type of board they were using it with. I’d scrap that solution, and instead, just try and set up the DPS310 for the original application as normal without any other sensors/peripherals attached only running it as an altimeter. That should help give us a better idea of what’s going on (I expect the same error messages as before, but I’m curious to see if there are any changes)

Thanks again @Bryce,

I have removed that section of code and recompiled and basically get the same error as I initially got:

In file included from C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_BusIO_Register.h:2,
from C:\Users\Documents\Arduino\libraries\Adafruit_DPS310/Adafruit_DPS310.h:17,
from C:\Users\Documents\Arduino\DPS310_example\DPS310_example.ino:3:
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:62:22: error: ‘BitOrder’ has not been declared
BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
^~~~~~~~
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:67:22: error: ‘BitOrder’ has not been declared
BitOrder dataOrder = SPI_BITORDER_MSBFIRST,
^~~~~~~~
C:\Users\Documents\Arduino\libraries\Adafruit_BusIO/Adafruit_SPIDevice.h:88:3: error: ‘BitOrder’ does not name a type
BitOrder _dataOrder;
^~~~~~~~
exit status 1
Error compiling for board RaspberryPI B+/2.

Yes, in theory, the DPS310 ‘should’ measure / calculate altitude, but it does not appear to be doing that when using the Python library, and I’m obviously having difficulty compiling the C++ code from the Arduino IDE.

If the libraries were fully functioning in Python, then I guess I’d just stick with programming in Python. Is this something that can be fixed perhaps?

I then potentially run into the issue when I get the SEN-15112 sensor working, that if it doesn’t have a Python library, then I’m back to using C / C++, so I just wonder if I should just persist with C++ and see if we can figure out the BitOrder error upon compiling?

Thanks again,
Kirk.

Hi Bryce, Kirk.
Excuse me for butting in here but I was reading this post with interest.
Apparently this device can measure atmospheric pressure the calculate the altitude using sea level pressure. OK so far.
My question, how does this device know the sea level pressure for any given site and any time. As we know this varies greatly. Aircraft pilots do have an adjustment on the aneroid altimeter which they normally adjust before departing. This info is given to them by radio or other means and is updated en route. But the altimeter has to be given this information. It doesn’t just happen.
As an aside. If this set up is going to be static why the altitude. If it’s not going anywhere it should not change. Can get that from most GPS navigators.
Cheers Bob

Hi @Robert93820,

So happy for you to ‘but in’. No problem at all.

Part of my research was that in order to work out (an approximate) altitude, I needed to provide the ‘closest’ sea level pressure to my script. Reading the C++ library I came across the following function comments:

/**************************************************************************/
/*!

  • @brief Calculates the approximate altitude using barometric pressure and the
  • supplied sea level hPa as a reference.
  • @param seaLevelhPa
  •    The current hPa at sea level.
    
  • @return The approximate altitude above sea level in meters.
    */
    /**************************************************************************/

In my original Python code, I had created a seaLevel variable, and that’s about as far as I got when I realised that they had not provided an Altitude function in the Python library. My intent was going to be to read the ‘local’ sea level pressure from the BOM website, say about once a day (or less - maybe once a week) just to ensure that the Altitude previously determined is ‘about right’. This little project I’m trying to get working effectively won’t move from one spot once I have it set up, so it’s not that I need to determine altitude say every minute or so.

I just didn’t think all this would be so difficult. I thought I could by a RPi, buy some compatible sensors that ‘appeared’ to have libraries pre-built, and to then simply put it together and cut the code. Now I’m finding that the simplest example code provided in the repo doesn’t compile for me, and that I can’t use the Python library as it doesn’t contain the Altitude function. Hmmmmm. :confounded:

Cheers.

Hi Bob,

No worries at all mate! :smile: Thank you for getting involved! This isn’t a Core Electronics support exclusive forum and is designed for group discussion with the whole team and community.

As for the project, that shouldn’t cause too much discrepancy in the altitude measured as long as the pressure is updated frequently. I’m not sure why it’s not compiling, but trying another IDE (or even another OS) wouldn’t be a bad idea to confirm that there are no issues there if possible. If anyone else has suggestions please feel free to share them :slight_smile:

All the best with it Kirk, please let me know if you have any other questions I can help with!

Thanks again @Bryce. I was using a brand new install of the Arduino IDE on my Win 10 laptop, so I’ll go with your suggestion and attempt to compile the code directly in the Arduino IDE on my RPi. The only reason I’ve not done that directly at this point was that I could not really find anywhere that showed how to configure / choose the Raspberry Pi board in the Arduino IDE’s Board Manager. I found this tutorial that showed how to set it up for Windows, but I’ve not seen anything comparable for Linux / RPi.

So I guess this prompts the inevitable question/s: for those whom are using a Raspberry Pi and connecting sensors with Arduino libraries, what language are you predominately programming in, and for those using the Arduino IDE, how are you compiling your code?

Thanks again,
Kirk.

2 Likes

Hi Kirk, Bryce
I really know very little about Pi and Python. Just interested that’s all. I thought that would have to be the case. Aircraft pilots need a constant update as they move from one area to the next as they could find themselves bumping into what used to be known as “Cumulous Granulous”. Clouds with Rocks.
Cheers Bob

1 Like

Just received this feedback from Adafruit Support:

You can’t use Arduino to program the Raspberry Pi to use the DPS310 sensor. You would need to use a separate microcontroller, like an UNO. In that case, you’d be running the Arduino IDE on the Pi to program the UNO and the DPS310 would be wired to the UNO, not the Pi.

If you want to use the DPS310 directly on the Pi, then you’ll want to program in Python and use Blinka to allow using CircuitPython libraries:
https://learn.adafruit.com/circuitpytho … spberry-pi
Arduino would not be used at all.

It looks like I’ll be heading back to Python then! I really didn’t think hooking up an Adafruit sensor to the Raspberry Pi would have been so difficult.

Quick update: it appears that in the past few days, the Python library for the DPS310 sensor was updated to now include the Altitude calculation. I have reverted back to Python and have updated the libraries and the Altitude is now correctly calculating.

Hi Kirk,

Excellent to hear, glad things are on track.

Thanks so much @Mitchell. Just need to get the SHT10 soil sensor working and I think I’m almost there. Steep learning curve!

1 Like

Hey Kirk,

Yes know the feeling, these things can be a super steep learning curve. Please let us know if you need anything on the SHT10 and the final result of your project - always awesome to see the finished product

1 Like