Untitled

mail@pastecode.io avatar
unknown
python
a month ago
1.4 kB
1
Indexable
Never
# Import the RPi.GPIO library for GPIO control
import RPi.GPIO as GPIO 

# Import the sleep function for introducing delays
from time import sleep

# Set the pin numbering mode to use physical pin numbers on the Raspberry Pi
GPIO.setmode(GPIO.BCM)

# Set up GPIO pins 4, 17, and 18 as output pins for controlling the motor
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)

#servo
GPIO.setup(13, GPIO.OUT)

btn1 = 26
btn2 = 16
GPIO.setup(btn1, GPIO.OUT)
GPIO.setup(btn2, GPIO.OUT)



# Create a PWM (Pulse Width Modulation) object on pin 18 with a frequency of 100 Hz
p = GPIO.PWM(18, 100)


# Start the PWM with an initial duty cycle of 0%
p.start(0)

# Activate motor in one direction: Set GPIO pin 4 to HIGH, pin 17 to LOW, and set the duty cycle of pin 18 to 25%
GPIO.output(4, True)
GPIO.output(17, False)
p.start(25)


# Turn on the motor by activating GPIO pin 18
GPIO.output(18, True)

# Pause the program for 5 seconds
sleep(5)

# Deactivate the motor and switch to another direction: Turn off GPIO pin 18, GPIO pin 4, and turn on GPIO pin 17
GPIO.output(18, False)
GPIO.output(4, False)
GPIO.output(17, True)

# Set the duty cycle of pin 18 to 50% for increased motor speed
p.start(50)

# Turn on the motor by activating GPIO pin 18
GPIO.output(18, True)

# Pause the program for 5 seconds
sleep(5)

# Turn off the motor, stop the PWM, and clean up the GPIO settings
GPIO.output(18, False)
p.stop()
GPIO.cleanup()
Leave a Comment