How to Create a New Sketch Template for Arduino
Creating a new sketch in the Arduino IDE makes the editor open a template containing skeletons for the functions setup and loop. What if you don't like this template? Can you edit it somehow? Yes, you can, and here is how.
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:
Unfortunately, I don’t like it for several reasons:
As it turns out, you can, and here is how.
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.
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:
- 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;
- 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;
- 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;
- Why didn’t they include a standard license, copyright or file description paragraph as a block comment?
As it turns out, you can, and here is how.
Three easy steps to happiness
- 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;
- Edit the file to make it fit your wishes and desires;
- Save your work when done.
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)
{
}