3

I am not sure if this is a silly question or not, but you might be able to provide some advice.

I have an Arduino project (C++) that uses a set of temperature/humidity sensors and a couple of LCDs. The sensors come with their own C/C__ libraries.

I'd like to port this project to my Raspberry Pi 2.

Will it work if I simply move the C++ project (including the sensor libraries) on my RPi?

Any advice or points to resources are appreciated.

Update On RPi, I am interested in using C++, not Python (research project constraints).

I use the following temperature/humidity sensors

Thanks.

  • 2
    You don't mention which sensor you are using, but it is likely that there is already a library and sample code to use it with a Pi (Google is your friend here), which would be even faster than porting your arduino code. – Steve Robillard Feb 25 '17 at 10:11
  • Thanks, @Steve. Actually, I want to reuse if possible the Arduino code or make minimum changes. I am mostly interested to find out whether it is possible to port (with some modifications) the code to a RPI. See my update regarding the sensors I use. – STiGMa Feb 25 '17 at 14:42
  • The DHT22 has a stock "iio" (industrial input/output) linux kernel driver; you can just read values from a sysfs node. Configuration is done via a device tree blob (look at /boot/overlays/README.txt). – goldilocks Feb 25 '17 at 15:22
2

Will it work if I simply move the C++ project (including the sensor libraries) on my RPi?

Not that simply. The Arduino C++ API is unique and as far as I am aware it is only ported to other similar microcontroller devices. The Pi has a normal CPU on which a normal, multi-tasking OS kernel is usually run.

If most of your C++ programming experience is on the Arduino, I believe the wiringPi C API is functionally similar (in many ways the restricted Arduino C++ library is more like C than it is contemporary standard, STL powered C++) and may feel familiar (this is a hunch though, since I haven't used it).

However, if you understand the library code you are using, it may be a good reference point for using the same sensors on the pi. While you would have to re-write them, likely many significant chunks can be cut n' pasted, etc.

  • Thank you, @goldilocks. I am aware of wiringPi and have in mind to use this. I also thought that big chunks of the Arduino libraries could be used with RPi (since the libs are primarily C/C++) but I wanted to check if anyone from the community tried this before (and whether he/she succeeded or not)... – STiGMa Feb 25 '17 at 14:46
  • If the libraries you use don't need strict timing, then it shouldn't be too hard. I would check if the libraries use direct port manipulation (setting bits of PORTA/B/C etc.) or use assembly. Those might need to be rewritten. – lights0123 Feb 25 '17 at 15:08
2

I'm afraid it depends on the code. If it uses Arduino quirks or Arduino libraries it will be harder to port than if it is a stand alone implementation.

The other determining factor will be the type of sensor. If it's analog it won't be so simple as the Pi has no analog GPIO. If it is digital it may be easier.

Personally I would first google for the sensor name and Raspberry Pi. Only if you can't find a hit which includes software would I consider doing a port.


The DHT22 is a digital temperature and humidity sensor. It uses a proprietary so called one wire system to communicate with a computer. The system is incompatible with the (Dallas) 1-wire bus.

An Arduino solution will be of little use on the Pi because of the tight timing required.

See http://abyz.me.uk/rpi/pigpio/examples.html#pdif2_DHTXXD for a C implementation (likely to be the most reliable on the Pi).

The TMP102 and HTU21D are digital sensors. They communicate using the standard I2C bus. The Arduino implementation is quirky and is probably not a good model to use on the Pi. Googling for Linux and the sensors should reveal standard C solutions.

  • thanks. I updated my post with the sensors I am using. All these sensors have C/C++ Arduino libraries, which I suppose could be ported to RPi with some modifications, right? Also, due constraints in my research project I have to stick to C/C++. – STiGMa Feb 25 '17 at 14:48

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.