SlideShare a Scribd company logo
#####Python code for servo control using Raspberry Pi#####
# Import libraries
import RPi.GPIO as GPIO
import time
# Set GPIO numbering mode
GPIO.setmode(GPIO.BOARD)
# Set pin 11 as an output, and set servo1 as pin 11 as PWM
GPIO.setup(11,GPIO.OUT)
servo1 = GPIO.PWM(11,50) # Note 11 is pin, 50 = 50Hz pulse
#start PWM running, but with value of 0 (pulse off)
servo1.start(0)
print ("Waiting for 2 seconds")
time.sleep(2)
#Let's move the servo!
print ("Rotating 180 degrees in 10 steps")
# Define variable duty
duty = 2
# Loop for duty values from 2 to 12 (0 to 180 degrees)
while duty <= 12:
servo1.ChangeDutyCycle(duty)
time.sleep(1)
duty = duty + 1
# Wait a couple of seconds
time.sleep(2)
# Turn back to 90 degrees
print ("Turning back to 90 degrees for 2 seconds")
servo1.ChangeDutyCycle(7)
time.sleep(2)
#turn back to 0 degrees
print ("Turning back to 0 degrees")
servo1.ChangeDutyCycle(2)
time.sleep(0.5)
servo1.ChangeDutyCycle(0)
#Clean things up at the end
servo1.stop()
GPIO.cleanup()
print ("Goodbye")

More Related Content

What's hot (13)

PDF
Nxll12 zone based firewall
Netwax Lab
 
PDF
Keep your GIT history clean
tomasbro
 
PDF
Sighup
Masato Bito
 
TXT
Sysinfo
Den Dom
 
PDF
Debugging TV Frame 0x09
Dmitry Vostokov
 
PDF
Plan 9カーネルにおけるTCP/IP実装(未完)
Ryousei Takano
 
PDF
Nxll28 ospf iii
Netwax Lab
 
PDF
Eincop Netwax Lab: Redistribution
Netwax Lab
 
PDF
IL: 失われたプロトコル
Ryousei Takano
 
PDF
Nxll16 basic asa v8.2
Netwax Lab
 
PPTX
04 18-2018--nmap port-80_blue_after_netcat
Alexander Bitar
 
PDF
A deep dive into libuv
Saúl Ibarra Corretgé
 
PDF
Route Redistribution
Netwax Lab
 
Nxll12 zone based firewall
Netwax Lab
 
Keep your GIT history clean
tomasbro
 
Sighup
Masato Bito
 
Sysinfo
Den Dom
 
Debugging TV Frame 0x09
Dmitry Vostokov
 
Plan 9カーネルにおけるTCP/IP実装(未完)
Ryousei Takano
 
Nxll28 ospf iii
Netwax Lab
 
Eincop Netwax Lab: Redistribution
Netwax Lab
 
IL: 失われたプロトコル
Ryousei Takano
 
Nxll16 basic asa v8.2
Netwax Lab
 
04 18-2018--nmap port-80_blue_after_netcat
Alexander Bitar
 
A deep dive into libuv
Saúl Ibarra Corretgé
 
Route Redistribution
Netwax Lab
 

Similar to Python code for servo control using Raspberry Pi (20)

PPTX
Python in raspberry pi
Sudar Muthu
 
TXT
Script
fauzasmg
 
PPTX
Raspberry pi Part 17
Techvilla
 
PDF
4. GPIO Access
Mayank Joneja
 
PPTX
Python and the Raspberry Pi
Rachel Wang
 
PDF
manual_2020_Cyber Physical System.pdf
ssuserc5ee4c
 
PPTX
[5]投影片 futurewad樹莓派研習會 141218
CAVEDU Education
 
PPT
Sensors, actuators and the Raspberry PI using Python
Derek Kiong
 
PPTX
Vermont Code Camp 2014 Simple Rover with RaspberryPi
John Cicilio
 
PPTX
Python-in-Embedded-systems.pptx
TuynLCh
 
PDF
import RPi-GPIO as GPIO import time #from AlphaBot import AlphaBot imp.pdf
Stewart29UReesa
 
PDF
AWS ロボを作ろう JAWSUG Kobe
崇之 清水
 
PDF
Raspberry Pi 4.pdf
Engineering Funda
 
PPT
Hexapod ppt
shiv kumar rai
 
DOCX
RoboticCarKit_MANUAL
Elijah Barner
 
TXT
Metal detecting robot sketch
Akhil Unnikrishnan
 
PDF
Adafruit raspberry-pi-lesson-9-controlling-a-dc-motor
Stefan Oprea
 
PDF
Raspberry Pi - best friend for all your GPIO needs
Dobrica Pavlinušić
 
PPTX
Raspberry Pi Using Python
Seggy Segaran
 
PPTX
Motordrive
Mahdi Babaei
 
Python in raspberry pi
Sudar Muthu
 
Script
fauzasmg
 
Raspberry pi Part 17
Techvilla
 
4. GPIO Access
Mayank Joneja
 
Python and the Raspberry Pi
Rachel Wang
 
manual_2020_Cyber Physical System.pdf
ssuserc5ee4c
 
[5]投影片 futurewad樹莓派研習會 141218
CAVEDU Education
 
Sensors, actuators and the Raspberry PI using Python
Derek Kiong
 
Vermont Code Camp 2014 Simple Rover with RaspberryPi
John Cicilio
 
Python-in-Embedded-systems.pptx
TuynLCh
 
import RPi-GPIO as GPIO import time #from AlphaBot import AlphaBot imp.pdf
Stewart29UReesa
 
AWS ロボを作ろう JAWSUG Kobe
崇之 清水
 
Raspberry Pi 4.pdf
Engineering Funda
 
Hexapod ppt
shiv kumar rai
 
RoboticCarKit_MANUAL
Elijah Barner
 
Metal detecting robot sketch
Akhil Unnikrishnan
 
Adafruit raspberry-pi-lesson-9-controlling-a-dc-motor
Stefan Oprea
 
Raspberry Pi - best friend for all your GPIO needs
Dobrica Pavlinušić
 
Raspberry Pi Using Python
Seggy Segaran
 
Motordrive
Mahdi Babaei
 
Ad

More from Amarjeetsingh Thakur (20)

PPTX
“Introduction to MATLAB & SIMULINK”
Amarjeetsingh Thakur
 
PDF
Python code for Push button using Raspberry Pi
Amarjeetsingh Thakur
 
PDF
Python code for Buzzer Control using Raspberry Pi
Amarjeetsingh Thakur
 
PDF
Arduino programming part 2
Amarjeetsingh Thakur
 
PDF
Arduino programming part1
Amarjeetsingh Thakur
 
PDF
Python openCV codes
Amarjeetsingh Thakur
 
PDF
Python Numpy Source Codes
Amarjeetsingh Thakur
 
PDF
Steemit html blog
Amarjeetsingh Thakur
 
PDF
Python OpenCV Real Time projects
Amarjeetsingh Thakur
 
PPTX
Adafruit_IoT_Platform
Amarjeetsingh Thakur
 
PDF
Core python programming tutorial
Amarjeetsingh Thakur
 
PDF
Python openpyxl
Amarjeetsingh Thakur
 
PPTX
Introduction to Internet of Things (IoT)
Amarjeetsingh Thakur
 
PPTX
Introduction to Node MCU
Amarjeetsingh Thakur
 
PPTX
Introduction to Things board (An Open Source IoT Cloud Platform)
Amarjeetsingh Thakur
 
PPTX
Introduction to MQ Telemetry Transport (MQTT)
Amarjeetsingh Thakur
 
PPTX
Arduino Interfacing with different sensors and motor
Amarjeetsingh Thakur
 
PPTX
Image processing in MATLAB
Amarjeetsingh Thakur
 
PPTX
Introduction to Arduino
Amarjeetsingh Thakur
 
PPTX
Introduction to Arduino
Amarjeetsingh Thakur
 
“Introduction to MATLAB & SIMULINK”
Amarjeetsingh Thakur
 
Python code for Push button using Raspberry Pi
Amarjeetsingh Thakur
 
Python code for Buzzer Control using Raspberry Pi
Amarjeetsingh Thakur
 
Arduino programming part 2
Amarjeetsingh Thakur
 
Arduino programming part1
Amarjeetsingh Thakur
 
Python openCV codes
Amarjeetsingh Thakur
 
Python Numpy Source Codes
Amarjeetsingh Thakur
 
Steemit html blog
Amarjeetsingh Thakur
 
Python OpenCV Real Time projects
Amarjeetsingh Thakur
 
Adafruit_IoT_Platform
Amarjeetsingh Thakur
 
Core python programming tutorial
Amarjeetsingh Thakur
 
Python openpyxl
Amarjeetsingh Thakur
 
Introduction to Internet of Things (IoT)
Amarjeetsingh Thakur
 
Introduction to Node MCU
Amarjeetsingh Thakur
 
Introduction to Things board (An Open Source IoT Cloud Platform)
Amarjeetsingh Thakur
 
Introduction to MQ Telemetry Transport (MQTT)
Amarjeetsingh Thakur
 
Arduino Interfacing with different sensors and motor
Amarjeetsingh Thakur
 
Image processing in MATLAB
Amarjeetsingh Thakur
 
Introduction to Arduino
Amarjeetsingh Thakur
 
Introduction to Arduino
Amarjeetsingh Thakur
 
Ad

Recently uploaded (20)

PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
Bachelor of information technology syll
SudarsanAssistantPro
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPTX
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Distribution reservoir and service storage pptx
dhanashree78
 
Bachelor of information technology syll
SudarsanAssistantPro
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 

Python code for servo control using Raspberry Pi

  • 1. #####Python code for servo control using Raspberry Pi##### # Import libraries import RPi.GPIO as GPIO import time # Set GPIO numbering mode GPIO.setmode(GPIO.BOARD) # Set pin 11 as an output, and set servo1 as pin 11 as PWM GPIO.setup(11,GPIO.OUT) servo1 = GPIO.PWM(11,50) # Note 11 is pin, 50 = 50Hz pulse #start PWM running, but with value of 0 (pulse off) servo1.start(0) print ("Waiting for 2 seconds") time.sleep(2) #Let's move the servo! print ("Rotating 180 degrees in 10 steps") # Define variable duty duty = 2 # Loop for duty values from 2 to 12 (0 to 180 degrees) while duty <= 12: servo1.ChangeDutyCycle(duty) time.sleep(1) duty = duty + 1 # Wait a couple of seconds time.sleep(2) # Turn back to 90 degrees print ("Turning back to 90 degrees for 2 seconds") servo1.ChangeDutyCycle(7) time.sleep(2) #turn back to 0 degrees print ("Turning back to 0 degrees") servo1.ChangeDutyCycle(2) time.sleep(0.5) servo1.ChangeDutyCycle(0) #Clean things up at the end servo1.stop() GPIO.cleanup() print ("Goodbye")