How can I send and receive data from an Arduino MEGA 2560 and Python?
Your problem is that you aren't sending the values from python in bytes. A more reliable way is to send the data in bytes like dat.write(b'1') and then receive it on the Arduino using Serial.readString(). I also had many issues with this and this is the best solution. &quot;https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/#:~:text=The%20easiest%20way%20is%20to,Arduino%20IDE)&quot;
If not, you can use the I2C interface. Just be careful, the logic level if your Python is running on a Raspberry Pi.