This documentation provides an example of how to use the ExternalGPIOController
library to manage GPIO pins.
Install CP210x USB to UART Bridge for Serial Communication
pip install git+https://github.com/rigbetellabs/ExternalGPIOContoroller_release.git
Below is an example of how to use the ExternalGPIOController library in your code.
from ExternalGPIOController import ExternalGPIOController
Parmeters :
( port
= '/dev/ttyUSB0', baudrate
= 115200, timeout
= 1) (Default Values)
gpio_manager = ExternalGPIOController(port='COM6', baudrate=115200, timeout=1)
gpio_manager.start_daemon()
Find Port Number on Windows
-
Open Device Manager, and expand the Ports (COM & LPT) list.
-
Like we can see the
COM
ports isCOM7
- Open terminal and type: ls /dev/*
ls /dev/tty*
Input GPIOS are INPUT_PIN_1, INPUT_PIN_2, INPUT_PIN_3, INPUT_PIN_4 and INPUT_PIN_5 Only
For Example We Can read the state Input using get_gpio_state(pin)
of INPUT_PIN_1
gpio_manager.get_gpio_state(gpio_manager.INPUT_PIN_1)
Outputs Pins for Output are as follows OUTPUT_PIN_1
,OUTPUT_PIN_2
,OUTPUT_PIN_3
,OUTPUT_PIN_4
andOUTPUT_PIN_5
only
For Example We Can set the of OUTPUT_PIN_1
to HIGH
gpio_manager.set_gpio(gpio_manager.OUTPUT_PIN_1,gpio_manager.PIN_HIGH))
Pin state are indicated by PIN_HIGH
or PIN_HIGH
thats for setting Pin to HIGH or LOW
respectively.
For Example We Can set the of OUTPUT_PIN_1
to HIGH
gpio_manager.set_gpio(gpio_manager.OUTPUT_PIN_1,gpio_manager.PIN_HIGH))
Input :
int
PinNumber ,bool
stateOutput :
int
Returns0
/LOW
or1
/HIGH
specifying the state of GPIO- Sets the state of a specified GPIO pin.
Input :
int
PinNumberOutput :
int
Returns0
/LOW
or1
/HIGH
specifying the state of GPIO- Returns the current state of a specified GPIO pin.
For Example We Can set the of
OUTPUT_PIN_1
toHIGH
usingget_gpio_state(pin)
gpio_manager.set_gpio(gpio_manager.OUTPUT_PIN_1,gpio_manager.PIN_HIGH))
Input :
None
Output :
int []
Returns array with0
/LOW
or1
/HIGH
specifying the state of all GPIO- Returns the current states of all GPIO pins as a dictionary.
# Print the current states of all GPIO pins
print("Current GPIO states:", gpio_manager.get_gpio_state_all())
Input :
None
Output :
None
- Starts the GPIO control daemon.
Input :
None
Output :
None
- Stops the GPIO control daemon.
from ExternalGPIOController import ExternalGPIOController
import time
# Initialize the GPIO manager
gpio_manager = ExternalGPIOController() #takes the default port
gpio_manager.start_daemon()
# try:
while True:
# Toggle the state of OUTPUT_PIN_1
gpio_manager.set_gpio(gpio_manager.OUTPUT_PIN_1, not bool(gpio_manager.get_gpio_state(gpio_manager.OUTPUT_PIN_1)))
# Print the current states of all GPIO pins
print("Current GPIO states:", gpio_manager.get_gpio_state_all())
# Sleep for a short duration to avoid running the loop too fast
time.sleep(0.1)
from ExternalGPIOController import ExternalGPIOController
# Initialize the GPIO manager
gpio_manager = ExternalGPIOController(port='/dev/ttyUSB1') #specifing a port
gpio_manager.start_daemon()
while True:
print(gpio_manager.get_gpio_state_all())
if gpio_manager.get_gpio_state(gpio_manager.INPUT_PIN_3) == 1:
gpio_manager.set_gpio(gpio_manager.OUTPUT_PIN_3, gpio_manager.PIN_HIGH)
print(" 3 is high")
else:
gpio_manager.set_gpio(gpio_manager.OUTPUT_PIN_3, gpio_manager.PIN_LOW)
print(" 3 is low")