Skip to content

Arduino API on Pi boards, the best of both worlds !

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING
Notifications You must be signed in to change notification settings

epsilonrt/piduino

Repository files navigation

PiDuino

Arduino on Pi boards, the best of both worlds !

Abstract

PiDuino is a C ++ library for Pi boards that allows the use of I/O like GPIO, I2C, SPI, UART ... with an API as close as possible to the Arduino language.
The description of Pi cards uses a stored "Object" model in a database that allows to add new models of boards easily.

At this time, the SoC models supported are AllWinner H-Series and Broadcom BCM2708 through 2711 which allows it to be used on Raspberry Pi and most Nano Pi, Orange Pi and Banana Pi.

The updated list of all the boards in the database is available in the Wiki.

To learn more about PiDuino, you can follow the Wiki, but if you're in a hurry, let's go to the quick start version...

Quickstart guide

The fastest and safest way to install piduino on Armbian is to use the APT repository from piduino.org, so you should do the following :

wget -O- http://www.piduino.org/piduino-key.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/piduino-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/piduino-archive-keyring.gpg] http://apt.piduino.org buster piduino" | sudo tee /etc/apt/sources.list.d/piduino.list
sudo apt update
sudo apt install libpiduino-dev piduino-utils

This repository provides Piduino packages for armhf and arm64 architectures. In the above commands, the repository is a Debian Buster distribution, but you can also choose Buster, Ubuntu Xenial or Bionic by replacing buster with bullseye, xenial, trusty, bionic, focal, jammy.
For Raspbian you have to do a little different :

wget -O- http://www.piduino.org/piduino-key.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/piduino-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/piduino-archive-keyring.gpg] http://raspbian.piduino.org buster piduino" | sudo tee /etc/apt/sources.list.d/piduino.list
sudo apt update
sudo apt install libpiduino-dev piduino-utils

The Raspbian repository provides Piduino packages for armhf architecture for Stretch and Buster only.

If you want to build from sources, you can follow the Wiki.

Utilities

Once installed, you should run the following on the command line :

$ pinfo
Name            : NanoPi Core2 Mini Shield
Family          : NanoPi
Database Id     : 40
Manufacturer    : Friendly ARM
Board Tag       : nanopineocore2shield
SoC             : H5 (Allwinner)
Memory          : 1024MB
GPIO Id         : 9
I2C Buses       : /dev/i2c-0
SPI Buses       : /dev/spidev1.0
Serial Ports    : /dev/ttyS1

As we can imagine, in the example, we are on a NanoPi Neo Core2 connected to a Mini Shield.

To read the pin status of connector 1, run the following on the command line :

$ pido readall 1
                                          CON1 (#1)
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+
| sOc | iNo |   Name   | Mode | Pull | V | Ph || Ph | V | Pull | Mode |   Name   | iNo | sOc |
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+
|     |     |     3.3V |      |      |   |  1 || 2  |   |      |      | 5V       |     |     |
|  12 |   8 |  I2C0SDA | ALT2 |  OFF |   |  3 || 4  |   |      |      | 5V       |     |     |
|  11 |   9 |  I2C0SCK | ALT2 |  OFF |   |  5 || 6  |   |      |      | GND      |     |     |
|  91 |   7 |  GPIOG11 |  OFF |  OFF |   |  7 || 8  |   | OFF  | ALT2 | UART1TX  | 15  | 86  |
|     |     |      GND |      |      |   |  9 || 10 |   | OFF  | ALT2 | UART1RX  | 16  | 87  |
|   0 |   0 |   GPIOA0 |  OFF |  OFF |   | 11 || 12 |   | OFF  | OFF  | GPIOA6   | 1   | 6   |
|   2 |   2 |   GPIOA2 |  OFF |  OFF |   | 13 || 14 |   |      |      | GND      |     |     |
|   3 |   3 |   GPIOA3 |  OFF |  OFF |   | 15 || 16 |   | OFF  | ALT2 | UART1RTS | 4   | 88  |
|     |     |     3.3V |      |      |   | 17 || 18 |   | OFF  | ALT2 | UART1CTS | 5   | 89  |
|  15 |  28 | SPI1MOSI | ALT2 |  OFF |   | 19 || 20 |   |      |      | GND      |     |     |
|  16 |  24 | SPI1MISO | ALT2 |  OFF |   | 21 || 22 |   | OFF  | OFF  | GPIOA1   | 6   | 1   |
|  14 |  29 |  SPI1CLK | ALT2 |  OFF |   | 23 || 24 |   | OFF  | ALT2 | SPI1CS   | 27  | 13  |
|     |     |      GND |      |      |   | 25 || 26 |   | OFF  | OFF  | GPIOA17  | 11  | 17  |
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+
| sOc | iNo |   Name   | Mode | Pull | V | Ph || Ph | V | Pull | Mode |   Name   | iNo | sOc |
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+

pido and pinfo come with manpages...

Blink Example

Arduino programming on Pi board ? We are going there !

#include <Piduino.h> // all the magic is here ;-)

const int ledPin = 0; // Header Pin 11: GPIO17 for RPi, GPIOA0 for NanoPi

void setup() {
  // initialize digital pin ledPin as an output.
  pinMode (ledPin, OUTPUT);
}

void loop () {
  // Press Ctrl+C to abort ...
  digitalWrite (ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay (1000);                 // wait for a second
  digitalWrite (ledPin, LOW);   // turn the LED off by making the voltage LOW
  delay (1000);                 // wait for a second
}

Obviously, you need to know the pin number where you connected the LED !

$ pido readall 1
                                          CON1 (#1)
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+
| sOc | iNo |   Name   | Mode | Pull | V | Ph || Ph | V | Pull | Mode |   Name   | iNo | sOc |
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+
|     |     |     3.3V |      |      |   |  1 || 2  |   |      |      | 5V       |     |     |
|  12 |   8 |  I2C0SDA | ALT2 |  OFF |   |  3 || 4  |   |      |      | 5V       |     |     |
|  11 |   9 |  I2C0SCK | ALT2 |  OFF |   |  5 || 6  |   |      |      | GND      |     |     |
|  91 |   7 |  GPIOG11 |  OFF |  OFF |   |  7 || 8  |   | OFF  | ALT2 | UART1TX  | 15  | 86  |
|     |     |      GND |      |      |   |  9 || 10 |   | OFF  | ALT2 | UART1RX  | 16  | 87  |
|   0 |   0 |   GPIOA0 |  OFF |  OFF |   | 11 || 12 |   | OFF  | OFF  | GPIOA6   | 1   | 6   |
|   2 |   2 |   GPIOA2 |  OFF |  OFF |   | 13 || 14 |   |      |      | GND      |     |     |
|   3 |   3 |   GPIOA3 |  OFF |  OFF |   | 15 || 16 |   | OFF  | OFF  | GPIOG8   | 4   | 88  |
|     |     |     3.3V |      |      |   | 17 || 18 |   | OFF  | OFF  | GPIOG9   | 5   | 89  |
|  22 |  12 |   GPIOC0 |  OFF |  OFF |   | 19 || 20 |   |      |      | GND      |     |     |
|  23 |  13 |   GPIOC1 |  OFF |  OFF |   | 21 || 22 |   | OFF  | OFF  | GPIOA1   | 6   | 1   |
|  24 |  14 |   GPIOC2 |  OFF |  OFF |   | 23 || 24 |   | UP   | OFF  | GPIOC3   | 10  | 25  |
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+
| sOc | iNo |   Name   | Mode | Pull | V | Ph || Ph | V | Pull | Mode |   Name   | iNo | sOc |
+-----+-----+----------+------+------+---+----++----+---+------+------+----------+-----+-----+

The iNo column corresponds to the 'Arduino' number, the number 0 pin corresponds therefore at pin 11 of the GPIO connector (GPIOA0).

To build, you must type the command:

$ g++ -o blink blink.cpp $(pkg-config --cflags --libs piduino)

You can then execute the program :

$ sudo ./blink

sudo is necessary for an access to the memory mapping of the GPIO). You can enable the setuid bit to avoid sudo in the future :

$ sudo chmod u+s blink
$ ./blink

With Codelite it's easier and funny, right ?

Debugging with Codelite

You should read the wiki on the examples to learn more...