File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 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 ("\n Servo motor control stopped." )
33+ pwm .stop ()
34+ GPIO .cleanup ()
Original file line number Diff line number Diff line change 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!
You can’t perform that action at this time.
0 commit comments