Skip to content

Commit 4c61931

Browse files
Motor_Control script added
1 parent 00d7660 commit 4c61931

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Motor_Control/Motor_Control.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import RPi.GPIO as GPIO
2+
import time
3+
4+
# Set the GPIO mode to BCM
5+
GPIO.setmode(GPIO.BCM)
6+
7+
# Define the GPIO pin for the servo motor
8+
servo_pin = 18
9+
10+
# Set up the GPIO pin for PWM
11+
GPIO.setup(servo_pin, GPIO.OUT)
12+
pwm = GPIO.PWM(servo_pin, 50) # 50 Hz PWM frequency
13+
14+
def set_servo_position(angle):
15+
duty_cycle = angle / 18 + 2 # Map angle to duty cycle
16+
pwm.ChangeDutyCycle(duty_cycle)
17+
time.sleep(0.5) # Give time for the servo to move
18+
19+
if __name__ == "__main__":
20+
try:
21+
pwm.start(0) # Start with 0% duty cycle (0 degrees)
22+
print("Servo motor control started. Press Ctrl+C to stop.")
23+
24+
while True:
25+
angle = float(input("Enter angle (0 to 180 degrees): "))
26+
if 0 <= angle <= 180:
27+
set_servo_position(angle)
28+
else:
29+
print("Invalid angle. Angle must be between 0 and 180 degrees.")
30+
31+
except KeyboardInterrupt:
32+
print("\nServo motor control stopped.")
33+
pwm.stop()
34+
GPIO.cleanup()

Motor_Control/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Motor_Control
2+
3+
Short description of package/script
4+
5+
- This Script Was simple to setup
6+
- Need import RPi.GPIO,time
7+
8+
9+
## Setup instructions
10+
11+
12+
Just Need to run this command "pip install RPi.GPIO " then run Motor_Control.py file and for running python3 is must be installed!
13+
14+
## Detailed explanation of script, if needed
15+
16+
This Script Is Only for Motor_Control use only!

0 commit comments

Comments
 (0)