How to Create a New Sketch Template for Arduino

September 13, 2019 | 11:25
When you launch the Arduino IDE for the first time or when you create a new sketch from the ‘File’ –> ‘New’ menu (Ctrl+N), the editor opens a new sketch template containing skeletons for the functions setup and loop. This is quite practical as you will need them anyway, so this saves you a little bit of time, and makes it easier not to forget (one of) them.

This is what the default Arduino sketch template looks like:
void setup() {
  // put your setup code here, to run once:
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
}


I hate the Arduino default sketch template

Unfortunately, I don’t like it for several reasons:
  1. The way the functions are declared with empty brackets is not recommended as it is not clear if the programmer forgot to put something inside the brackets or if the space was intended to be left empty. Although it is very common to do things this way, and allowed, better practice is to fill empty brackets with the keyword ‘void’ as it removes all doubts;
  2. I know by now that I have to put my code ‘here’, so whenever I start a new sketch, I find myself deleting this comment in both functions;
  3. Personally, and only because I am a finicky person, I hate placing opening braces at the same line as the declaration. For me an opening brace must be in the same column as the corresponding closing brace;
  4. Why didn’t they include a standard license, copyright or file description paragraph as a block comment?
So, in the end, cleaning up the default sketch template to make it fit my wishes and desires costs me more time than writing everything from scratch. If only you could change this template?
As it turns out, you can, and here is how.

Three easy steps to happiness

  1. Open the file <arduino>\examples\01.Basics\BareMinimum\BareMinimum.ino in Notepad, Notepad++ or some other text editor that does not add all sorts of formatting all by itself. Here ‘<arduino>’ is to be replaced by the path to your ‘arduino.exe’ file;
  2. Edit the file to make it fit your wishes and desires;
  3. Save your work when done.
Next time you create a new sketch it will contain your template.

Note that you cannot do this in the Arduino IDE itself because it tries to protect the BareMinimum example from being overwritten.

A custom sketch template also is a great way to automatically add the lines of code – Serial.begin(115200); comes to mind – or license text that you use in every sketch. Keep the template as simple as possible to avoid repeated unnecessary editing later.

My default template

Below is my personalised Arduino sketch template that you can use as a starting point. It has a function called 'splash' that prints version information about the program to the serial port. You can download it below.
/*
 * Project:
 * Description:
 * Creation date:
 * Author: Elektor, CPV
 * (C) Elektor, www.elektor.com
 * License: GPL-3.0
 */

const char *p_project = "xxx";
const uint8_t version_hi = 0;
const uint8_t version_lo = 1;

void splash(void)
{
  Serial.print("Elektor project: ");
  Serial.println(p_project);
  Serial.print("Version ");
  Serial.print(version_hi);
  Serial.print('.');
  Serial.println(version_lo);
  Serial.println("https://www.elektor.com\n");
}

void setup(void)
{
  Serial.begin(115200);
  splash();
}

void loop(void)
{
}
Products
Attachment
arduino default template
Personalised BareMinimum example
Unpack the archive and place the ino file (overwrite the existing file) in [arduino]\examples\01.Basics\BareMinimum\
Downloading of this attachment is reserved for Elektor GREEN and GOLD members only. Click here to log in.
Loading comments...
related items