12th Anniversary Sale 12% Off in July & August with the voucher code "SUMMER12" *excludes trade customers
Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

Python Library and Demos

Software Library and Demo Code for the Python Programming Language

Our Python libraries are designed to work seamlessly with our expansion boards available from AB Electronics UK, providing a robust and efficient way to interface with your Raspberry Pi. These libraries offer a comprehensive suite of functions, making it easier than ever to develop and prototype your projects.

Our Python library is available on our Python Library GitHub Repository

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.

If you require support or have any questions regarding our Python libraries and demo code, please do not hesitate to contact us. We have a support forum where you can post your questions and engage with other users. Additionally, you can use the contact form on our website to reach out directly. Our support team is dedicated to helping you get the most out of your Raspberry Pi expansion boards. We welcome your feedback and are committed to continually enhancing our products and services to better meet your needs.


Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit

Related Expansion Boards

Order a ADC Differential Pi Photo of ADC Differential Pi
ADC Differential Pi

8 Channel 18-bit Differential Analogue to Digital converter for the Raspberry Pi

£17.99 Ex VAT: £14.99
Order a ADC Pi Photo of ADC Pi
ADC Pi

8 Channel 17-bit Single-Ended Analogue to Digital converter for the Raspberry Pi

£19.19 Ex VAT: £15.99
Order a ADC-DAC Pi Zero Photo of ADC-DAC Pi Zero
ADC-DAC Pi Zero

2 Channel ADC and 2 Channel DAC for the Raspberry Pi

£14.99 Ex VAT: £12.49
Order a Expander Pi Photo of Expander Pi
Expander Pi

Analogue, Digital and RTC Expansion Interface for the Raspberry Pi

£27.59 Ex VAT: £22.99
Order a I2C Switch Photo of I2C Switch
I2C Switch

4 channel I2C Switch for the Raspberry Pi

£10.79 Ex VAT: £8.99
Order a IO Pi Plus Photo of IO Pi Plus
IO Pi Plus

32 channel MCP23017 digital I/O expander for the Raspberry Pi

£14.39 Ex VAT: £11.99
Order a IO Zero 32 Photo of IO Zero 32
IO Zero 32

32 channel digital I/O expander for the Raspberry Pi

£13.19 Ex VAT: £10.99
Order a RTC Pi Photo of RTC Pi
RTC Pi

Real-time clock for the Raspberry Pi

£11.39 Ex VAT: £9.49
Order a Servo PWM Pi Photo of Servo PWM Pi
Servo PWM Pi

16-channel, 12-bit PWM controller for driving LEDs and RC servos

£11.39 Ex VAT: £9.49

Related Articles

Python Library and Demos
Software Library and Demo Code for the Python Programming Language
C Library and Demos
Software Library and Demo Code for the C Programming Language
C++ Library and Demos
Software Library and Demo Code for the C++ Programming Language
MicroPython Library and Demos
MicroPython Library and Demo Samples
Arduino Library and Demos
Demo code for the Arduino Platform
.Net Core Libraries
Use .Net Core with your Raspberry Pi Project
Windows 10 IOT Library & Demos
Windows 10 IOT Library & Demo Code
Node JS Library and Demos
Node JS Library and Demo Samples