Ok, serialEvent "runs after loop()" and it is not an interrupt. AS far as I know main() has two functions - setup and "infinite" loop. So where does serialEvent comes in?

And if I do not want loop() to run and use only setup than I cannot use serialEvent.


Yes you are correct, the event handlers will not do anything in setup. Main runs "setup()", then loops infinity through "loop()", but gives the serial event handlers a chance to run in between. The actual main that runs on the arduino is this:

int main(void)


#if defined(USBCON)


    for (;;) {
        if (serialEventRun) serialEventRun();

    return 0;

this is found (on linux at least) in arduino/hardware/arduino/avr/cores/arduino/main.cpp

"serialEventRun()" is in HardwareSerial.cpp, but is not very interesting.

To be clear, Serial itself will still work fine in setup(). If you want to use the same event handler function, all you have to do is call "serialEventRun()" yourself periodically.

  • To be fair, you only need to call serialEventRun() if you define a serialEvent{,1,2,3}() function. – Ignacio Vazquez-Abrams Sep 9 '14 at 2:12
  • That was what I intended to get across, I'll edit for more clarity. – BrettAM Sep 9 '14 at 2:42
  • Thanks,I feel that 120% people using Arduino main() should have this posted on their refrigerator. – Vaclav Sep 12 '14 at 20:22
  • To clarify, this "serial event" callback is for handling incoming data over Serial, right? If I'm not receiving any data on the device, and I'm merely Serial.printing things, I should be unaffected if I have an infinite loop in setUp, correct? – Alexander Oct 30 '17 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.