This project provides a simple GPIO driver for MicroPython, allowing you to easily control digital, analog, PWM pins, and various peripherals on your microcontroller.
boot.py
: Entry point for the MicroPython script.gpio.py
: Contains theGPIO
,LED
,Servo
,Stepper
,StepperULN
,UltraSonic
,Joystick
,RotaryEncoder
,SevenSegment
, andRGB
classes.main.py
: Example usage of the classes.
The GPIO
class allows you to initialize and control GPIO pins. It supports digital, analog, and PWM pins.
from gpio import GPIO
# Digital pin as output
led = GPIO(14, GPIO.DIG, GPIO.OUT)
# Analog pin
adc = GPIO(35, GPIO.ADC)
# PWM pin
pwmLed = GPIO(25, GPIO.PWM)
GPIO.DIG
: Used to specify the pin type as digital.GPIO.ADC
: Used to specify the pin type as analog input.GPIO.PWM
: Used to specify the pin type as PWM.GPIO.OUT
: Used to specify the pin mode as output.GPIO.IN
: Used to specify the pin mode as input.GPIO.IN_PULLUP
: Used to specify the pin mode as input with pull-up resistor.GPIO.IN_PULLDOWN
: Used to specify the pin mode as input with pull-down resistor.GPIO.RISING
: Used to specify a rising edge interrupt trigger.GPIO.FALLING
: Used to specify a falling edge interrupt trigger.
init(mode=None)
: Reinitialize the GPIO pin with a new mode.setFreq(freq=1000)
: Set the frequency for a PWM pin.flash(t=1)
: Flash the digital output pin fort
seconds.read()
: Read the value from the pin.write(value=None)
: Write a value to the pin.toggle()
: Toggle the digital output pin.attachInterrupt(trigger, callback)
: Attach an interrupt to the GPIO pin.
The LED
class allows you to control an LED with digital or PWM pins.
from gpio import LED
# Digital LED
led = LED(14, LED.DIG)
# PWM LED
pwmLed = LED(25, LED.PWM)
LED.DIG
: Used to specify the LED type as digital.LED.PWM
: Used to specify the LED type as PWM.
on()
: Turn the LED on.off()
: Turn the LED off.flash(t=1)
: Flash the LED fort
seconds.blink(n, t=0.15)
: Blink the LEDn
times witht
seconds between blinks.fadeIn()
: Gradually increase the brightness of the LED.fadeOut()
: Gradually decrease the brightness of the LED.write(value=None)
: Write a PWM duty cycle to the LED.morseCode(msg="SOS")
: Flash the LED in Morse code for the given message.setMorseSpeed(speed)
: Set the speed of the Morse code in words per minute.
The RGB
class allows you to control an RGB LED with digital or PWM pins.
from gpio import RGB
# RGB LED with PWM pins
rgb = RGB(r=14, g=15, b=16, pinType=RGB.PWM, mode=RGB.COMM_CATHODE)
RGB.DIG
: Used to specify the LED type as digital.RGB.PWM
: Used to specify the LED type as PWM.RGB.COMM_ANODE
: Used to specify the RGB LED as common anode.RGB.COMM_CATHODE
: Used to specify the RGB LED as common cathode.
RGB.RED.on() / RGB.GREEN.on() / RGB.BLUE.on()
: Turn on the specified color.RGB.RED.off() / RGB.GREEN.off() / RGB.BLUE.off()
: Turn off the specified color.flash(led, t=1)
: Flash a specific LED (RGB.RED
,RGB.GREEN
, orRGB.BLUE
) fort
seconds.writeDig(r_val, g_val, b_val)
: Write digital values to the RGB LED.write(r_val, g_val, b_val)
: Write PWM values to the RGB LED.blink(r_val, g_val, b_val, t=1, n=1)
: Blink the RGB LED with specified values forn
times.
The Servo
class allows you to control a servo motor with PWM pins.
from gpio import Servo
# Servo motor
servo = Servo(16)
Servo.ABS
: Used to specify absolute movement mode.Servo.REL
: Used to specify relative movement mode.
move(targetAngle)
: Move the servo to the target angle.setMode(mode)
: Set the movement mode of the servo (Servo.ABS
orServo.REL
).reset()
: Reset the servo to the 0-degree position.mid()
: Move the servo to the middle position.max()
: Move the servo to the max degree position.
The Stepper
class allows you to control a stepper motor.
from gpio import Stepper
stepper = Stepper(stepPin=17, dirPin=18, sleepPin=19)
powerOn()
: Enable the stepper motor.powerOff()
: Disable the stepper motor.steps(stepCount)
: Move the motor by a specified number of steps.relAngle(angle)
: Rotate the motor by a relative angle.absAngle(angle)
: Rotate the motor to an absolute angle.revolution(revCount)
: Rotate the motor by a specified number of revolutions.setStepTime(us)
: Set the time between steps in microseconds.
The StepperULN
class allows you to control a stepper motor using a ULN2003 driver.
from gpio import StepperULN
stepperULN = StepperULN(pin1=25, pin2=26, pin3=27, pin4=28, delay=5, mode=StepperULN.HALFSTEP)
StepperULN.FULLSTEP
: Used to specify full-step mode.StepperULN.HALFSTEP
: Used to specify half-step mode.
step(count, direction=1)
: Move the motor by a specified number of steps.angle(r, direction=1)
: Rotate the motor by a specified angle.reset()
: Reset the motor pins to a low state.
The UltraSonic
class allows you to measure distances using an ultrasonic sensor.
from gpio import UltraSonic
ultraSonic = UltraSonic(triggerPin=20, echoPin=21)
getDistanceMm()
: Get the distance in millimeters.getDistanceCm()
: Get the distance in centimeters.
The Joystick
class allows you to read values from a joystick.
from gpio import Joystick
joystick = Joystick(x=22, y=23, btn=24)
read()
: Read the joystick's X, Y, and button states. Returns a tuple(x, y, btn)
.getDirection()
: Get the direction of the joystick. ReturnsUP/DOWN/LEFT/RIGHT/CENTER
.
The RotaryEncoder
class allows you to handle rotary encoder inputs.
from gpio import RotaryEncoder
rotary_encoder = RotaryEncoder(clkPin=29, dtPin=30, swPin=31)
onRight(handler)
: Register a callback function for right rotation.onLeft(handler)
: Register a callback function for left rotation.onButtonPress(handler)
: Register a callback function for button press.
The SevenSegment
class allows you to control a 7-segment display.
from gpio import SevenSegment
seven_seg = SevenSegment(segment_pins=[2, 3, 4, 5, 6, 7, 8], common_anode=True)
display(digit)
: Display a digit (0-9) on the 7-segment display.clear()
: Clear the 7-segment display.random()
: Display a random digit on the 7-segment display.countup(count=10, t=1)
: Count up on the 7-segment display.countdown(count=10, t=1)
: Count down on the 7-segment display.