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
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