Knowledge Base

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

Knowledge Base

Python Library and Demos

Using our Python Library with your electronics project

Python

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.


Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit
Created 02/10/2015 | Last Updated: 28/12/2023

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
Add to Cart
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
Add to Cart
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
Add to Cart
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
Add to Cart
Order a I2C Switch Photo of I2C Switch
I2C Switch

4 channel I2C Switch for the Raspberry Pi

£10.79 Ex VAT: £8.99
Add to Cart
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
Add to Cart
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
Add to Cart
Order a RTC Pi Photo of RTC Pi
RTC Pi

Real-time clock for the Raspberry Pi

£11.39 Ex VAT: £9.49
Add to Cart
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
Add to Cart

Related Articles

Python Library and Demos
Using our Python Library with your electronics project
C Library and Demos
C Library and Demo Samples
CPP Library and Demos
C++ Library and Demo code
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
Other Code Samples
Other Code Demos and Samples