Over the years, I have been working on several projects with raspberry pis mostly related to some form of environmental sensing. One thing that always bothered me is that there are a lot of sensors that come with off-the-shelf libraries: the obvious advantage is, that everything is relatively easy to implement, however, often I found myself frustrated, as the documentation of these libraries is often scarce or incomprehensible for a beginner, and i felt like not learning anything substantial from simply connecting a sensor and running some functions from a library. Additionally, a lot of parts/sensors do not have libraries and would require a custom made implementation.

I was therefore wondering, whether tutorials or teaching aids exist that are a good introduction to the subject of connecting sensors from scratch?

Addendum: I want to clarify that I'm looking for tutorials or guides on general principles or approaches on how to connect a component using a standardized protocoll or port to the raspberry pi.

An example could be a BME280 humidity, atmospheric pressure and temperature sensor: for me, the general approach would be to read any tutorial (e.g. https://www.raspberrypi-spy.co.uk/2016/07/using-bme280-i2c-temperature-pressure-sensor-in-python/) on how to connect the sensor to the raspberry pi. In the tutorial a script is available to download, which reads out the data. In the script we have several functions that e.g. define registers, read out bytes etc. And that is where my question starts:

  • There must be a generalized approach on how to connect a device or component using a given bus (e.g. I2C/UART, etc.)?
  • Somehow you must know that when you connect device d via I2C that you'll have to check steps n to x to be able to read out the data it sends?

Thank you everybody for answering my question, I realise now, that it was way to broad to give a simple answer to it. I hope i could clarify it a little bit.

  • 4
    Hello. That's quite a broad question! Might be worth narrowing it down a little by naming a sensor you're interested in and indicating which languages you're using. As a general suggestion I'd start with getting the Datasheet or Application Note for the sensor and, if the available "library" is open-source, looking through the source code. – Roger Jones Mar 20 '20 at 8:41

I was therefore wondering, whether tutorials or teaching aids exist that are a good introduction to the subject of connecting sensors from scratch.

You are unlikely to find anything, although there are a few books which explain how to do common tasks in different languages.

One I can recommend (although somewhat dated for newer Raspbian) is

   Author = {Derek Molloy},
   Title = {Exploring Raspberry Pi: Interfacing to the Real World with Embedded Linux},
   Publisher = {Wiley},
   Year = {2016},
   ISBN = {978-1-119-1868-1},
   URL = {http://www.exploringrpi.com/}

The traditional approach is to get a data sheet for the sensor, which usually have connection details.

  • Thank you, that is a great book! – Octopus Mar 22 '20 at 15:08
  • I marked your question as the final answer, I looked through the book and I think it's closest to what I was looking for. – Octopus Mar 27 '20 at 6:59

I doubt it. That would be pretty much the definition of reinventing the wheel.

I suggest your best learning experience is to look through the sensor datasheet at the same time as studying the library code.


I'm going to disagree with joan a bit about "re-inventing the wheel". If everyone stuck to this adage in an unflinching way, there would only be one operating system, one web browser, etc., and joan's own pigpio library would not exist because it reinvents the wheel of WiringPi and/or libbcm2835 (not sure and didn't check what came first, but I am pretty sure it was not pigpio).

All of those are written in C and serve the ultimate purpose of controlling (the exact same) very specific hardware. Are two of the authors crazy people? Why waste your time doing that? Or is the idea that having competing interfaces and implementations is a good thing in evolutionary terms sound?

There are (at least) a few very valid reasons for creating a library that serves much the same ultimate purpose as another library in the same language, even when the platform is also identical:

  • Performance, if you think it could be improved but trying to do so by forking and modifying the existing library (presuming open source!) is not the way to get there.
  • Because the API sucks, which is sort of what I'm hearing from you; poor documentation and a crappy API often go hand in hand, as a good API should lean strongly toward self-documenting. Notice that the three Pi libraries mentioned in the first paragraph provide three very different APIs, again all focused on more or less the same set of tasks (to be fair: pigpio also adds a lot on top that I think the other two do not).

A sensor based lib is potentially a layer on top of existing the GPIO lib, unless you want to write custom code for the nitty-gritty too. That I think is a probably a bad idea unless you are doing it as a nearly bare metal exercise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.