Hello, I just can not find a solution to the simple task of activating a pin on arduino through a function call in python. The problem is that there is a delay of half a second due to (as I understand it) the expectations of the readiness of the microcontroller. Is it possible to avoid delays?

Python

import serial, time ON = ":00000008f8" ser = serial.Serial('COM3', 57600) time.sleep(0.28) // если убираю здесь, то пин не активируется #ser.flush() ser.write(ON) time.sleep(1) ser.close() 

Arduino:

 const int pin = 2; void pulseLed() { digitalWrite(pin, HIGH); // sets the pin on delay(200); // pauses for 50 microseconds digitalWrite(pin, LOW); // sets the pin off delay(200); // pauses for 50 microseconds } void setup() { pinMode(pin, OUTPUT); // set pin to input // initialize serial: Serial.begin(57600); } void loop() { // serial read section while (Serial.available()) // this will be skipped if no data present, leading to { if (Serial.available() >0) { pulseLed(); } } } 
  • This problem does not occur because of the expectation of the microcontroller, but because of the port waiting. The only way to win is to use a delay like yours, and keep the port open while it is needed. - Avernial

2 answers 2

When the host accesses the serial port of Arduino (the function call serial.open() ), the so-called soft reset of the microcontroller occurs. This launches the Arduino bootloader, which waits for a while to see if a special sequence of characters will come from the host to start the program for downloading the new firmware.

Since the special sequence does not arrive, the bootloader transfers control to the user's firmware, launching your sketch.

If you want the microcontroller to wait for the program to start on the host and not to be reset when the serial port is opened, then you should remove the soft reset jumper on your Arduino board.

Without this jumper, when loading the sketch to the Arduino IDE, you will need to manually press the reset button to start the bootloader.

  • Excellent answer, thank you - Egor Mikheev
  • @Vanyamba Electronics only works with some amendments, read the answer below. - Egor Mikheev

I have no Arduino Nano jumper, I dropped out the C4 resistor, but the device began to lag and behave unpredictably, put the resistor between +5 and GND, everything began to work as a watch. Who is interested in the link to the library: FireBox - a simple way to connect Arduino with Python