GPIO control
Project description
Documentation for GpioDevice Class
GpioDevice
The GpioDevice class provides a generic interface to work with Raspberry Pi GPIO pins. It supports configuring the GPIO as an input (for buttons, sensors), an output (for relays, LEDs), or for PWM (Pulse Width Modulation) to control devices like motors or dimmable LEDs..
Importing the Package
To use the GpioDevice class, import it from your package/module:
import RPi.GPIO as GPIO
from GpioDevice import GpioDevice
Constructor
init(self, pin_no, mode='input', pwm_freq=None)
Initializes a GPIO pin with the specified mode
- Parameters:
- pin_no (int): The GPIO pin number to use.
- mode (str): The mode in which the GPIO pin will operate. Supported modes:
- 'input': Configure the pin as an input (for buttons, sensors, etc.).
- 'output': Configure the pin as an output (for relays, LEDs, etc.).
- 'pwm': Configure the pin for PWM (for controlling dimmable LEDs, motors, etc.). Requires pwm_freq.
- pwm_freq (int, optional): Frequency for PWM mode. Only required if mode is 'pwm'.
- Raises:
ValueError: If an invalid mode is specified or PWM frequency is missing when required.
Methods
read_input(self)
Reads the input value from a GPIO pin configured as an input.
-
Returns: bool: True if the input is HIGH, False otherwise.
-
Raises: Exception: If the GPIO is not configured as input.
Example:
button = GpioDevice(pin_no=17, mode='input')
if button.read_input():
print("Button is pressed!")
set_output(self, state)
Sets the output state of a GPIO pin configured as an output.
-
Parameters: state (bool): True for HIGH (turn on), False for LOW (turn off).
-
Raises: Exception: If the GPIO is not configured as output.
-
Example:
relay = GpioDevice(pin_no=18, mode='output')
relay.set_output(True) # Turn on the relay
relay.set_output(False) # Turn off the relay
start_pwm(self, duty_cycle)
Starts PWM on the GPIO pin configured for PWM mode.
-
Parameters: duty_cycle (float): Duty cycle for PWM (range: 0.0 to 100.0).
-
Raises: Exception: If the GPIO is not configured for PWM or PWM is not initialized.
-
Example:
pwm_led = GpioDevice(pin_no=19, mode='pwm', pwm_freq=1000)
pwm_led.start_pwm(50) # Start PWM with 50% duty cycle
stop_pwm(self)
Stops PWM on the GPIO pin.
-
Raises: Exception: If the GPIO is not configured for PWM or PWM is not initialized.
-
Example:
pwm_led.stop_pwm() # Stop the PWM signal
cleanup(self)
Cleans up the GPIO pin, resetting any pin states that were set during the operation. It is good practice to call this method when your script terminates to free up the GPIO resources.
- Example:
button.cleanup() # Clean up after use
Example Usage
Here’s an example of how you can use the GpioDevice class to handle multiple GPIO modes in a single project.
Example 1: Using GPIO as a Button
button = GpioDevice(pin_no=17, mode='input')
if button.read_input():
print("Button pressed!")
button.cleanup() # Cleanup when done
Example 2: Using GPIO as a Relay
relay = GpioDevice(pin_no=18, mode='output')
relay.set_output(True) # Turn on relay
relay.set_output(False) # Turn off relay
relay.cleanup() # Cleanup when done
Example 3: Using GPIO for PWM (e.g., LED Dimming)
pwm_led = GpioDevice(pin_no=19, mode='pwm', pwm_freq=1000)
pwm_led.start_pwm(50) # Set PWM to 50% duty cycle
# After some time, stop the PWM
pwm_led.stop_pwm()
pwm_led.cleanup() # Cleanup when done
Notes Modes: When configuring a pin, ensure that the mode parameter correctly reflects your desired operation:
'input' for buttons or sensors. 'output' for relays or LEDs. 'pwm' for devices requiring pulse width modulation (PWM).
-
Error Handling: The class raises exceptions when methods are called on a pin that is not configured for the requested operation (e.g., trying to read input from a pin configured as an output).
-
GPIO Cleanup: Always call the cleanup() method when your script terminates to reset the pin states. This avoids leaving pins in an undefined state.
GPIODevice Package
This package provides a generic interface to interact with Raspberry Pi GPIO pins. You can configure the pins as inputs (for buttons or sensors), outputs (for relays or LEDs), or for PWM (for dimming LEDs, controlling motors, etc.).
Installation
pip install gpio_device
Usage
Initialize a GPIO Pin
from gpio_device import GpioDevice
- You can use GPIO pins in three modes: input, output, or pwm. Depending on the mode, the behavior changes.
Input Mode Example:
button = GpioDevice(pin_no=17, mode='input')
if button.read_input():
print("Button pressed!")
button.cleanup()
Output Mode Example:
relay = GpioDevice(pin_no=18, mode='output')
relay.set_output(True)
relay.cleanup()
PWM Mode Example:
pwm_led = GpioDevice(pin_no=19, mode='pwm', pwm_freq=1000)
pwm_led.start_pwm(50)
pwm_led.cleanup()
Cleanup
Always call the cleanup() method when done to reset the GPIO pins.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pi_gpio_device-0.1.tar.gz
.
File metadata
- Download URL: pi_gpio_device-0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09df9a7235c3032b53ad5289136f943491b5982f031dc17b5717af65ae552e38 |
|
MD5 | c1d4d72dc3999d6712d3f41862881bd1 |
|
BLAKE2b-256 | 8a49d859620a231a49211ee1e410029930aa9ba333e030aeb9713efd605e0b09 |
File details
Details for the file pi_gpio_device-0.1-py3-none-any.whl
.
File metadata
- Download URL: pi_gpio_device-0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a7709b28acc39bbef17da7f0b1aae860dfa4ba5889f8ab1e9099ed4b6c472c5 |
|
MD5 | d90f0e764e3d4eec42c2117efef0240b |
|
BLAKE2b-256 | ee947e6c09ac14201abda075d6c107552d20b5bd28c4a22e63d372175349f8f7 |