Professional Documents
Culture Documents
Apurv no.13
Apurv no.13
Apurv no.13
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
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/