Knowledge Base
The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.
Python Library and Demos
Using our Python Library with your electronics project
Python libraries are collections of reusable code that can be used to perform various tasks without having to write code from scratch. For users of AB Electronics UK's expansion boards, these libraries provide a simple way to seamlessly interface with the hardware.
By using Python, a user-friendly and widely used programming language, you can control and interact with these boards easily. This makes tasks like reading sensor data or controlling digital devices more straightforward, especially for those new to electronics or programming.
Our Python library is available on GitHub at https://github.com/abelectronicsuk/ABElectronics_Python_Libraries
Setting Up Your Environment:
Connect your Raspberry Pi to a monitor, keyboard, and mouse. Power it up and ensure it's running the latest version of Raspberry Pi OS.
Ensure Python is installed on your Raspberry Pi. Python normally comes pre-installed on Raspberry Pi OS. You can check by typing python --version or python3 --version in the terminal. Current versions of Raspberry Pi OS will only have Python 3 installed, and it is recommended to use Python 3 with any new projects.
If your computer does not have Python 3 installed, you can use the following commands in a terminal to install it.
sudo apt update sudo apt install python3
Most of our expansion boards use the I2C bus on the Raspberry Pi, some use the SPI bus. You will need to enable these data buses before you can use the Python library. Check the documentation for your expansion board to find out which data bus is needed.
For I2C-based expansion boards please follow our tutorial I2C Part 2 - Enabling I2C on the Raspberry Pi to enable the I2C bus.
For SPI-based expansion boards please use the tutorial SPI and Python on Raspberry Pi OS.
Downloading the Library
To install the AB Electronics UK Python library on your Raspberry Pi, start by updating your system and installing the necessary Python build and installer packages by running the following commands in a terminal.
sudo apt sudo apt install python3-build python3-installer git
Next, download the AB Electronics UK Python Libraries to your computer by running:
git clone https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git
There are two common methods for using Python library modules. You can install them on your computer so they can be referenced from any Python program, or you can copy the module into your project directory and reference it directly.
Installing the Python library
After downloading, navigate to the ABElectronics_Python_Libraries folder and run the following commands in a terminal to build the library, and install it.
python3 -m build sudo python3 -m installer dist/*.whl
This will install the library in the system Python library directory where it can be referenced from Python programs.
Using classes without installing the library
To use a specific part of our Python library in your project without installing the entire library, you can simply copy the needed class file into your project's directory. For example, to use the IO Pi Plus, copy the IOPi.py file from the IOPi directory to where your project files are located. After doing this, you can use the class in your program by adding an import statement at the beginning of your Python code. This allows you to directly utilize the class's functionality in your project.
from IOPi import IOPi
Exploring the Library
The folder for the AB Electronics UK Python Library has a structured directory, each corresponding to a specific expansion board.
The main directories include ADCDACPi, ADCPi, ADCDifferentialPi, ExpanderPi, I2CSwitch, IOPi, IOZero32, RTCPi, and ServoPi.
Each of these contains the Python module and a demo directory containing demonstration files relevant to the respective board.
Additionally, each directory has a README.md file that contains the documentation for the Python module including a list of available classes and functions.
Running Your First Script
To run a Python script using Python 3, open your command terminal and navigate to the directory where your script is located. Once there, you can run your script by typing python3 scriptname.py into the CLI, replacing scriptname.py with the actual name of your script.
For example, to run the demo_ioread.py demonstration script for the IO Pi Plus navigate to the ABElectronics_Python_Libraries/IOPi/demos/ directory and run the following command.
python3 demo_ioread.py
To exit the Python script use CTRL-C on your keyboard.