Created on: 6 January 2022
Arduino example sketches for beginners are found inside the Arduino IDE. Quickly start using Arduino by loading example sketches to an Arduino board. Load and modify Arduino example sketches in this part of the Arduino tutorial for beginners. A sketch is a program written for an Arduino board.
Part 3 of the Arduino Tutorial for Beginners
The previous part of this Arduino tutorial for beginners shows how to load the Arduino IDE software application to a host computer. It also shows how to load a first sketch to the target Arduino board. Complete that part of the tutorial before continuing with this part.
The Blink example sketch in the previous part of this tutorial flashes or blinks the on-board LED on an Arduino Uno or MEGA 2560. New official Arduino boards have this sketch factory loaded. If your board already had the Blink sketch loaded, you would not have seen any changes after loading the Blink sketch again. In this case, the on-board LED continues blinking at the same rate. Modify the Blink sketch and see the LED blink rate change, as described next.
Start the Arduino IDE application. Open the Blink example sketch: Select File → Examples → 01.Basics → Blink on the top Arduino IDE menu.
The Blink sketch contains a big comment block at the top. Scroll down to find the code that blinks the on-board LED. Comments are shown in a gray color between the /* and */ characters. Text typed between an opening forward slash and asterisk /* and closing asterisk forward slash */ is ignored by the software tools that run when the sketch is uploaded to an Arduino board. Place any comments or notes that you want between these characters. This type of comment can span multiple lines. The sketch name and description are contained in the top comment block of the Blink sketch. Detailed information about the on-board LED, and a history of the sketch follows. After that, find a link to the Arduino Blink sketch tutorial page.
A second type of comment starts with double forward slashes //. In this case, everything after the // is part of the comment. This is a single-line comment that turns everything after the // on the same line into a comment. Consequently the next line after the comment is not part of the comment.
The image below shows the Blink sketch code with the top comment block removed. As can be seen, it contains a mix of single-line comments and code. The code is everything in the image that is not a comment. Comments describe what is happening in the sketch.
When a sketch is loaded to an Arduino board, it is first built by software tools that automatically run. The build process consists of preprocess, compile and link stages. This converts the code from human readable text to something that runs on the Arduino board.
Colored words in the sketch are a result of the Arduino IDE using syntax highlighting. Syntax highlighting is the highlighting of Arduino language keywords, definitions and functions. This is more easily understood after more is learned about writing Arduino sketches.
Change the rate that the on-board LED blinks at in the Blink sketch, as follows. Change 1000 to 200 in both instances it is found in the code. The following image shows the modified Arduino Blink sketch. Red dots in the image mark the modified lines of code. Notice that the comments at the right of each modified line of code are updated to reflect the changes made in the code.
Click the Upload button in the Arduino IDE. This uploads the modified sketch to the target Arduino board. Notice that the on-board L LED blinks faster. As a result of the modified code, the LED is now on for 0.2 seconds and off for 0.2 seconds. The on and off times were originally 1000 milliseconds, also written 1000ms, which is one second (1s). This is because there are 1000ms in 1s. When 1000 is changed to 200 in the sketch, the LED on and off times change from 1000ms to 200ms. 200ms is 0.2s.
Arduino example sketches are read-only. This means that they cannot be overwritten. When an example sketch is modified and saved, it must be saved to a new location. Three ways of saving a sketch in the Arduino IDE are firstly, click the Save toolbar icon (the arrow pointing down). Hover the mouse cursor over any toolbar icon and its name is shown at the right of the icons. Secondly, save the file using the keyboard shortcut Ctrl + S (hold down the Ctrl key and then press the s key). Thirdly, select File → Save on the top Arduino IDE menu bar. Because the file is read-only, the IDE prompts to save the file to a different location. Use the dialog box that opens to save the Blink sketch to your Arduino folder. Change the name to something like Blink_Fast before saving.
This part of the Arduino tutorial for beginners is a short introduction to Arduino example sketches for beginners. Later in the tutorial other example sketches are loaded. In addition to this, the structure of a sketch is explained. For now, experiment with the Blink sketch. For example, change the LED on time to 200ms and off time to 1000ms.