0% found this document useful (0 votes)
9 views5 pages

Real Time Servo Motor Visualization

Real_Time_Servo_Motor_Visualization

Uploaded by

Nesha Sd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Real Time Servo Motor Visualization

Real_Time_Servo_Motor_Visualization

Uploaded by

Nesha Sd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Real-Time Servo Motor Visualization with Arduino and Python

To achieve real-time visualization of the servo motor inputs from the Arduino, you'll need to set up serial communication

between the Arduino and your computer and then visualize the incoming data in Python.

1. Arduino Code to Send Data via Serial

First, modify the Arduino code to send the servo position data over the serial port.

```cpp

#include <Servo.h>

Servo myServo;

int pos = 0; // Variable to store the servo position

void setup() {

Serial.begin(9600); // Start serial communication at 9600 baud

myServo.attach(9); // Attach the servo to pin 9

void loop() {

// Generate a sine wave-like motion

for (int i = 0; i <= 180; i += 1) {

pos = 90 + 90 * sin(i * 3.14159 / 180); // Sine wave from 0 to 180 degrees

myServo.write(pos); // Move the servo to the position


Serial.println(pos); // Send the position to the serial port

delay(15); // Wait for the servo to reach the position

for (int i = 180; i >= 0; i -= 1) {

pos = 90 + 90 * sin(i * 3.14159 / 180); // Sine wave from 180 to 0 degrees

myServo.write(pos); // Move the servo to the position

Serial.println(pos); // Send the position to the serial port

delay(15); // Wait for the servo to reach the position

```

2. Python Code for Real-Time Visualization

Next, set up the Python script to read the data from the Arduino via the serial port and visualize it in real time.

Before running the Python code, make sure you have the `pyserial` package installed. You can install it using:

```bash

pip install pyserial

```

Here's the Python script:

```python

import serial
import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

# Set up the serial connection (adjust the COM port and baud rate as necessary)

ser = serial.Serial('COM3', 9600) # Replace 'COM3' with your Arduino's port

# Create a buffer to store the data

data = []

# Set up the figure and axis

fig, ax = plt.subplots()

x = np.linspace(0, 2 * np.pi, 1000)

line, = ax.plot(x, np.zeros_like(x))

# Set the axis limits

ax.set_xlim(0, 1000) # Show the last 1000 data points

ax.set_ylim(0, 180) # Servo position range

# Function to update the frame

def update(frame):

# Read a line of data from the serial port

try:

value = float(ser.readline().strip()) # Read and convert to float

data.append(value)

if len(data) > 1000:


data.pop(0) # Keep the buffer size to 1000

line.set_ydata(data[-1000:]) # Update the data for the plot

line.set_xdata(range(len(data[-1000:])))

except:

pass

return line,

# Create the animation

ani = animation.FuncAnimation(fig, update, frames=100, interval=50, blit=True)

# Display the animation

plt.show()

# Close the serial connection when done

ser.close()

```

### Explanation:

1. **Serial Communication**: The Arduino sends the current position of the servo motor over the serial port. The Python

script reads this data in real time.

2. **Data Buffering**: The Python script keeps a buffer of the last 1000 data points to display in the graph. You can

adjust this to show more or fewer points.

3. **Real-Time Plotting**: The `matplotlib` animation function updates the plot in real time with new data from the serial
port.

### Steps to Run:

1. **Upload** the Arduino code to your Arduino board.

2. **Run** the Python script on your computer. Make sure to replace `'COM3'` with the correct serial port where your

Arduino is connected.

3. **Watch** the real-time visualization of the servo motor input in the plot window.

You might also like