Faster Serial communication

So I'm trying to program a simple communication between arduino and python in order to display a camera feed (camera model: OV7725). I started by making a fast connection, however, a 64x128 grayscale image (0.96" OLED display size) takes 0.735 seconds (1,36fps), so it isn't very useful. I tried speeding up the communication by removing x and y coordinates and using Serial.write() instead of Serial.print(). I don't know how to make it faster, any idea? I'm pretty new and I don't know much.

Arduino code:

void setup() {
  Serial.begin(115200);
  while (!Serial) {}

  int max_distance_sq = sq(32.5) + sq(64.5);

  for (int j = 0; j < 64; j++)
  {
    for (int i = 0; i < 128; i++)
    {
      float distance_sq = (sq(32.5-j) + sq(64.5-i)) / max_distance_sq;
      Serial.write(255 - int(distance_sq * 255));
    }
  }
}

void loop() {
}

Python code:

import serial
import numpy as np
import matplotlib.pyplot as plt
import time


img = np.zeros((64,128))

while True:
    try:
        arduino = serial.Serial('COM3', 115200) #USB torre 2
        break
    except:
        continue

start_time = time.time()
i = 0
j = 0

while True:
    data = int(arduino.read(1)[0])
    img[j][i] = data
    
    if i == 127 and j == 63:
        break
    
    if i == 127:
        i = 0
        j += 1
    else:
        i += 1
    
    
plt.imshow(img, cmap='gray')
print(time.time() - start_time)

arduino.close()

Unfortunatly I'm using a Arduino micro and 115200 is the fastest baudrate allowed, I'll try it on a ESP32 at a higher baudrate and edit this message with the result

It is for the Micro, I searched it and tried it and the speed didn't improved. I'm currently trying the ESP32 but for some reason the image isn't right, it seems like there is a sync problem, maybe I'm reading garbage at the beginning

The micro is a 32u4 board with native full speed usb.

1 Like

I'm searching info on that and I can't find if I can use it or how to configure it. Could you give me some advice please?

I've done some tests with ESP32 and it seems like the limitation now is Python, as I can run the code with 460800 bps with no problem and takes 0.259s, but at 1Mbps it takes 0.25s and the image is corrupted. I might have to use another way to read the image. About the first problem I talked about when using ESP32 is that, on reset, it sends some information that Python read. Just connect GPIO15 to GND and solved

Have you tried increasing the baud rate ?

Serial.begin(2000000);    //for instance

I tried. Arduino Micro can't go over 115200bps and Python misses bytes at 1Mbps with ESP32

This link might give some insights into configuration on the Python side. I'm not very knowledgeable of Python and have not direct experience.

https://meghanvyang.medium.com/how-to-set-up-usb-serial-program-connection-789237d167a8

What else is competing with the Python code for processor time?

Plenty of people have run Arduinos a lot faster than that over the years. Your problem lies elsewhere.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.