Programming for the Raspberry Pi with Arduino IDE

Hi,
I am familiar with the Arduino IDE and would like to start programming for the Raspberry Pi 3 or 4 with it.

I can imagine it's quite different, isn't it?
I suppose on can't port programs easily from an Arduino to a Raspberry Pi since the context is radically different.

Can one use Arduino libraries for i2C devices connected to GPIO as well?

Can someone suggest some useful resources to start with?
Thank you

When you say "Raspberry Pi", do you mean the traditional single board computers made by that company (e.g., Raspberry Pi 4, Raspberry Pi Zero W), or the new RP2040 microcontroller (e.g., Raspberry Pi Pico)?

The Raspberry Pi is a full featured Linux board, complete with desktop applications. It will run the Arduino IDE, too.

Thank you for the good question, i meant the traditional Pi 3 or 4 under Linux.

I meant compiling FOR the Raspberry, not ON the Raspberry.

No, you cannot use the Arduino IDE to program the Raspberry Pi.

Use the Pi like you would any other linux box. It is not a real time operating system, so lots of things you can do with an Arduino, like fast direct port access, become harder if not impossible with the Pi.

OK. I know of this 3rd party Arduino boards platform:

It adds a "RaspberryPI B+/2" board to the Arduino IDE's Tools > Board menu and you can then compile and upload Arduino sketches to the Raspberry Pi SBC like it was a regular Arduino board.

The Raspberry Pi 1 B+ is pretty long in the tooth. This might mean that you can pick some up for cheap though. I see that support for the Zero W was added, but they didn't add a new boards menu entry:

Maybe the "RaspberryPI B+/2" board selection actually works for all the supported RPi boards and the name was just never updated to reflect the general nature it gained since the platform's creation.

I see there is also a proposal to add support for the 3B+

but that one has not been merged so you'd need to merge those changes in yourself.

if you describe the project that you want to use the Raspberry Pi with and the reasons why you think a Raspberry Pi is better suited than a "real" microcontroller suggestions can be made what to use

a.) to keep the Arduino-IDE as the development tool
or
b.) What programming-tool to use for the Raspberry Pi that is similar to use like the Arduino-IDE
best regards Stefan

I just made quite a bench of Arduino and ESP projects, that interface with a Raspberry Pi for further processing like recording in a database and similar.
For some of them I could imagine it would just be easier to do everything in the Raspberry Pi.

I am very familiar with the Arduino IDE and C++, but really find the standard Python to become fully clumsy when it comes to interface hardware.

I never programmed C++ in a Linux environment an can imagine it is quite different.
I wanted to evaluate the real estate of available hardware related C++libraries written for the Rasberry Pi , that I imagine to be very thin.
I just wondered whether those libraries written for the Arduino would fit as well.

AFAIK the toolchain for Wheezy and ff. does work on ALL RaspberryPi boards.

Aha. We haven't talked about what exactly is clumsy for you with python accessing hardware and me personally I do not have any experience in this.

I did a quoogling (my short for quick googling as I like to create new words) ;-))
for interfacing hardware with raspberry pi and python and came up with this links

which - from a 2 minute very quick cross-read looked quite comfortable. Of course a 2 minute cross-reading is not the same as having realised a real hardware-project with a Raspi

What I did do is using ESP8266s and ESP32s to send UDP-messages to a Raspi where the Raspi stores the received messages in textfiles. The python-code for doing this is pretty short

# very simple and short upd-receiver found here
# https://www.studytonight.com/network-programming-in-python/working-with-udp-sockets#

import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)      # For UDP

udp_host = socket.gethostname()		        # Host IP
udp_port = 4210			                # specified port to connect

sock.bind((udp_host,udp_port))
print ("Waiting for client...")

HeaderDelimitChar = "$"

while True:
  data,addr = sock.recvfrom(1024)	        #receive data from client
  print("data #",data,"#")
  Msg = data.decode('utf-8')
  print ("Received Message: #",Msg,"# from",addr)
  EndOfHeader = Msg.find(HeaderDelimitChar)
  HeaderBytes = Msg[0:EndOfHeader]
  FileName = HeaderBytes + ".txt"
  print("Filename #",FileName,"#")
  myFile = open(FileName, "a+")
  EndOfStr = data.find(0)
  MsgToWrite = Msg[EndOfHeader + 1 :1024] + '\r'
  myFile.write(MsgToWrite);
  myFile.close()
  print ("Data #",MsgToWrite,"#")

Something like a Teensy 4.0/4.1 has quite a lot of calculation-power 32bit 600 MHz and a lot of RAM (1MB) but of course is not a full blown OS-based-Computer like a Raspi.
So it still depends on what you want to do.

Some time ago I discovered ESP-Dash, which makes developing webbased Interfaces a breeze because all the HTML-stuff is done in the backround. (the pro-version offers free position-configuring)

And Webserial and elegantOTA

best regards Stefan

I don't want to reinvent the wheel.
Linux has tons of excellent high level solutions,
Arduino tons of excellent low level libraries.
I just need both.

Python, due to its not declarative variables just gets awful as soon as you want to handle bit level interfacing to hardware,

I did that too.
I just wondered, if I could avoid one hardware level.

So I did a quoogle (a quick googling) and found this

# getting your message as int
i = int("140900793d002327", 16)
# getting bit at position 28 (counting from 0 from right)
i >> 28 & 1
# getting bits at position 24-27
bin(i >> 24 & 0b111)

best regards Stefan

Not necessarily. There are free IDEs similar to the Arduino IDE that run on the Pi, or any other linux installation. I use Code::Blocks for console programs.

Libraries for low level functions might be hard to find, though. So, for something like a robot, it makes perfect sense to use an RPi for the brains, and a few Arduinos talking to it, to operate the effectors and sensors.

In the long run, you will be FAR better off programming the Pi natively. By limiting yourself to the Arduino world, you surrender a massive amount of capability, and will spend a huge amount of time trying to make things work Arduino-style when they will work much better using the native Linux infrastructure. Just a few examples:

  1. Web server - First, I very much doubt any of the Arduino web server code would work. Even if it did, you'll be limited in capabilities. Simply use Apache, and you have a fully-functional web server, and access to javascript, PHP, Perl, AJAX and countless other industry-standard web tools. Apache takes mere minutes to install and configure.

  2. You mentioned a database - Not sure what "database" is available for Arduino, but on the Pi, you can use full SQL, along with PHP. SQL also takes mere minutes to install and configure.

  3. Multi-threading - Arduino simply does not do it. Debian Linux on the Pi is a full multi-threaded OS, allowing you do anything you like with threads.

If you want to do development on your PC, but build for the Pi, you can do that easily. Install MS Visual Studio, and install the VS Linux build plug-in. You can do ALL of your editing and debugging on the PC, with the actual build being performed remotely on the Pi. You also have full remote debugging capability. It works beautifully, and gives you at least 1000X more functionality than ANY Arduino-based development environment, with access to source control (git, or any other).

...but where are the countless device libraries of the Arduino world?
In the Raspberry fora, the C++ contributions are really thin.
Raspberry is a Python world and Python is NOT my taste AT ALL!

Raspberry Pi is Linux, which supports every programming language known to man. There are DECADES of c/c++ applications, libraries and drivers available for everything under the sun. There are also the OS-level, and Linux driver-level device drivers, which are, in general FAR greater functionality than the average Arduino "device library". Devices drivers for ALL existing RPi hardware devices are already built into the OS, ready to use. There are a near infinite number of references, tutorials, and example programs for doing nearly anything you'd ever want to do. Almost NONE of it is unique to RPi, but is, rather, common to nearly all Linux variants, and most of the interfaces existed long before Linux came along, so they are very stable, and robust.

1 Like

Python is popular on the Pi, but there is no reason to use it if you don't wish to.

What specifically do you want this Pi to do? Bear in mind too, that it can run hundreds of processes, so you need not limit yourself to a few tasks.

"There are DECADES of c/c++ applications, libraries and drivers available for everything under the sun"

Maybe...
Surely tons of files, whereas you never know which one is valid, which one is where and which examples show how the things match. Mainly you end up in a dead path and just can start over with another attempt...

That is what made the success of the Arduino IDE: get for every device libraries in a clear interface that install in one click, providing the corresponding examples to start with.

Every device is only as good as its support base.

And regarding the Pi and C++ that base looks pretty thin.