Ardpy : Arduino controlled by python
ardpy, the pyhon3 package, contains Arduino llibrary ‘Ardpy’ that makes it very easy to communicate between Arduino and Raspberry pi using python via i2c protocol. Before using it, the following configure procedures are required.
1. configuring i2c for Raspberry pi
You must configure Raspberry pi to enable i2c. Refer to the follwing page:
Configure i2c : Raspberry pi
2. connect wires for i2c between Arduino and Raspberry pi
The GPIO pins on Raspberry pi use 3.3V while most Arduino boards are activated on 5V. Thus, a voltage level converter is required. The level-converting circuit is very simple and one can easily buy a low-priced hardware module. (for example, see fig. 1)
[fig. 1] voltage level converter
Using this voltage level converter, wiring between Raspberry pi and Arduino nano are illustrated in fig. 2.
[fig, 2] wiring for i2c communication between RPi and Arduino nano
3. installing Ardpy arduino library
You can use PC or Raspberry pi to program Arduino board. If you use PC, download Ardpy.zip at the following github release page:
https://github.com/salesiopark/Ardpy/releases
After downloading, in the Arduino IDE, navigate to Sketch > Include Library. At the top of the drop down list, select the option to "Add .ZIP Library''. You will be prompted to select the library you would like to add. Navigate to the Ardpy.zip file's location and open it.
[fig.3] add arduino zipped library in Mac PC
If you use Raspberry pi to develop Arduino board, install Arduino IDE first using the following command.
$ sudo apt-get install arduino |
Then, after executing IDE, the following directory created in the user home directory.
In this directory, Arduino library files are to be located. Go to that directory and download Ardpy library files (Ardpy.h, Ardpy.cpp, keywords,txt) using the following git command.
sudo git clone https://github.com/salesiopark/Ardpy.git |
Then, the Ardpy directory is automatically created and the following files with some other source files are copied into it.
Ardpy.h Ardpy.cpp keyword.txt |
3.3 test Ardpy library are properly installed
If the Ardpy library is properly installed, the following Arduino program must be successfully compiled without errors, Note that <Wire.h> must be included before <Ardpy.h> header file.
#include <Wire.h> // <== must be included before Ardpy.h #include <Ardpy.h>
void setup() { Ardpy.begin(0x10); //0x10 is an i2c address }
void loop(){ } |
The Ardpy.begin() method initiates i2c communication and the first argument to the function is i2c address (0x03~0x77). If this is download into Arduino board and if it is correctly wired to the Raspberry pi’s i2c pins (refer fig. 2), the i2cdetect command must show the 0x10 address as follows.
$ i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 10 a b c d e f00: -- -- -- -- -- -- -- -- -- -- -- -- -- --10: 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --70: -- -- -- -- -- -- -- |
4. installing python package
The ardpy package is programmed using python3 (not python2) and, in case of raspbian, python3 is pre-installed. And ardpy requires smbus package. Thus, it must be installed before installing ardpy. Note that python3-smbus is to be installed. (not for python2)
$ sudo apt-get update $ sudo apt-get install python3-smbus |
Then, ardpy can be easily installed using the following command.
$ sudo python3 -m pip install ardpy |
After that, in the python3 shell, it must be imported without error.
This is it. All the pre-configures are completed. Some example codes are here.