Apurv no.13

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Practical No.

13 : Develop Python program to control Servo-motor


interfaced with Arduino.
I. Practical Significance
Developing a Python program to control a servo motor interfaced with Arduino holds
significant practical significance in various fields such as robotics, automation, and remote
control systems. By integrating Python with Arduino, users can harness the versatility of
Python programming to precisely manipulate servo motors, enabling the creation of intricate
mechanisms and robotic systems. This experiment allows for the development of applications
ranging from robotic arms in manufacturing to camera stabilization systems in cinematography.
Moreover, Python's extensive library support and ease of use streamline the process of
prototyping and implementing complex servo motor control algorithms, fostering innovation
and advancement in automation technologies.
II. Relevant Program Outcomes (POs)
• Basic Knowledge: Apply fundamental principles of electronics and programming to
develop Python programs for controlling servo motors interfaced with Arduino boards.
• Discipline Knowledge: Demonstrate proficiency in understanding the principles of servo
motor operation, Arduino programming, and Python integration for embedded systems
applications.
• Experiments and Practice: Plan and conduct experiments to gain hands-on experience in
interfacing servo motors with Arduino boards and utilizing Python for motor control.
• Engineering Tools: Utilize appropriate engineering tools and technologies, such as Python
libraries for serial communication and Arduino IDE for firmware uploading, to develop and
debug servo motor control systems effectively.
• Life-Long Learning: Engage in continuous learning activities to stay updated with
advancements in embedded systems, robotics, and automation technologies, for lifelong
learning and professional development in the field.
III. Competency and Practical Skills
Develop general purpose programming using Python to solve problem.
The practical is expected to develop the following skills:
• Write a program to control Servo-motor with Arduino.
• Utilize Python libraries for serial communication.
IV. Relevant Course Outcomes(s)
Perform interfacing with Arduino microcontrollers.
V. Practical Outcomes(PrOs)
• Write Python code to control the Servo-motor for specific angle.
VI. Relevant Affective Domain related Outcomes(s)
1. Follow safety practices
2. Demonstrate working as leader/ a team member
3. Follow ethical practices
VII. Minimum Theoretical Background
Arduino: Arduino is an open-source electronics platform based on easy-to-use hardware and
software. It consists of a microcontroller board and development environment, allowing users
to create interactive electronic projects. Arduino boards are programmed using the Arduino
IDE (Integrated Development Environment), which is based on the C and C++ programming
languages. Arduino boards are commonly used in various applications, including robotics,
automation, and IoT (Internet of Things) projects.
Basic Connections of Servo Motor: A servo motor is a rotary actuator that allows for precise
control of angular position. It typically consists of a motor, a control circuit, and a feedback
system. Servo motors have three wires: power (usually red), ground (usually black or brown),
and control signal (usually yellow or white). The control signal wire is connected to a PWM
(Pulse Width Modulation) pin on the Arduino board to control the position of the servo motor
shaft. Servo motors are commonly used in robotics, RC (remote control) vehicles, and other
applications requiring precise control of movement.
PyFirmata: PyFirmata is a Python library that allows communication between Python scripts
and Arduino boards using the Firmata protocol. It enables users to control Arduino pins, read
sensor data, and send commands to Arduino boards from Python scripts running on a computer.
PyFirmata provides a convenient way to interface Python with Arduino without the need for
low-level programming in C or C++.

Fig no. 13.1

Install pyFirmata Library: Install the pyfirmata library using pip, the Python package
manager. Open a terminal or command prompt and run the following command:
>> pip install pyfirmata
Interfacing Steps of Servo Motors by Python:
To interface a servo motor with Arduino using Python, the following steps are typically
followed:
1. Install the PyFirmata library on the computer.
2. Upload the StandardFirmata sketch to the Arduino board using the Arduino IDE.
3. Connect the servo motor to the Arduino board, ensuring proper power and ground
connections.
4. Write a Python script using PyFirmata to send PWM signals to the servo motor through the
Arduino board.
5. Use the pyfirmata library to control the servo motor's position by adjusting the PWM
signal's duty cycle.
6. Run the Python script to control the servo motor's movement based on the specified
commands or inputs.
Make Hardware Connections:
To connect a servo motor to an Arduino board, follow these steps:
1. Connect the red wire (power) of the servo motor to the 5V pin on the Arduino board.
2. Connect the black or brown wire (ground) of the servo motor to any GND pin on the
Arduino board.
3. Connect the yellow or white wire (control signal) of the servo motor to a PWM (Pulse
Width Modulation) pin on the Arduino board, such as pin 9 or 10.
4. Ensure that the servo motor has a stable power supply and that the Arduino board is
properly connected to a computer for communication.
Run Python Script: Copy the provided Python script into a Python (.py) file. Update the COM
port (COM3 in the example) in the script to match the port your Arduino is connected to. Save
the file and run it using Python.
Code:
>> from pyfirmata import Arduino, SERVO
>>from time import sleep
>>port = 'COM3'
>>pin = 10
>>board = Arduino(port)
>>board.digital[pin].mode = SERVO
>>while True:
>> for i in range(0, 180):
>> board.digital[pin].write(i)
>> sleep(0.015)
>>sleep(1)
>>for i in range(180, 0, -1):
>> board.digital[pin].write(i)
>> sleep(0.015)
>>sleep(1)

Once the Python script is running, observe the Servomotor connected to pin of the Arduino
board. It should on and rotate 0 to 180 degrees, then delays for 1 second, rotates the servo from
180 to 0 degrees, and again delays for 1 second. This process repeats indefinitely in an infinite
loop as controlled by the Python script.
VIII. Resources required
Sr. Name of Resource Specification Quantity Remarks
No. (if any)
1. Computer System Computer (i3-i5 preferable As per Batch For all
RAM> 2GB) Size experiments
2. Operating System Windows/Linux
3. Development Python IDE/ PyScripter
Software
4. Arduino Board Any basic Arduino board
5. Peripherals Servomotor, jumper wire,
etc

IX. Resources used (Additional)


Sr. Name of Resource Specification Quantity Remarks
No. (if any)
1. Computer System
2. Operating System
3. Development
Software
4. Arduino Board
5. Peripherals

X. Practical related Questions


1. What is a servo motor, and how does it differ from other types of motors?
2. Describe the role of PWM (Pulse Width Modulation) in controlling servo motors.
3. How does the sleep() function in Python contribute to smooth servo motor movement?
(Space for answers)
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
.....................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
XI. Exercise
1. Write a python program to define the rotation range for the servo motor by using for loop.
2. Write a Python program to make a servo motor connected to rotate from 0 to 180 degrees
and back in a loop.
3. Write a Python program to rotate a servo motor at a constant speed of 90 degrees per
second.
(Space for answers)
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
XII. References/ Suggestions for further Reading

1. Htttps://www.realpython.com/arduino-python/
2. https:// www.learn.sparkfun.com/tutorials/servo-motor-control-with-an-arduino
3. https://www.learn.adafruit.com/guides/beginner/

Marks Obtained Dated signature of


Teacher
Process Related Product Related Total (25)
(15) (10)

You might also like