Skip to main content

A sketch is compiled code that is uploaded to an Arduino board.

This tag should be used when writing code for the Arduino specifically using the Arduino IDE.

A sketch has the following structure:

void setup()
{
    ...
}

void loop()
{
    ...
}

The setup function is called once when the sketch starts. The loop function then is called repeatedly until the board is either shut down or restarted.

A sketch is saved with the .ino extension.


If you are used to more traditional C programming, effectively what is done by the above is:

int main ()
  {
  init ();  // set up hardware, particularly timers used by delay()
  setup (); // your own code, to be run once only
  while (true)
    loop ();  // do this repeatedly
  return 0;   // this is never executed
  }

It's a bit more complex than that, but that is the basic idea. Also the IDE lets you take control by writing your own main() function like this:

int main ()
  {
  ...  // do whatever you want here
  return 0;
  }

If you do that, the "normal" main is ignored.

Code Language (used for syntax highlighting): lang-cpp