LOG Arduino Sketches 101

So this Lazy Old Geek (LOG) gets bored sometimes and wants something to do. Actually, sometimes I get tired of trying to solve hardware problems, which are sometimes software problems. So I decided to do a tutorial on Arduino sketches.
LOG Arduino Sketches 101
What is a Sketch? I’m not sure where the term Arduino sketch came from but it is basically the program or code that is written to tell an Arduino what to do.

There are already some great tutorials on sketches. Here’s a couple of great ones:

For some readers, the problem may be that they go into too much detail too fast.

TIP: Since I’m old and can’t remember things as well as I used to, this is my goto reference when I’m writing sketches:

Step 1: Sketches 100

For beginners to Arduino, here’s some basic things you need to know.

Arduino, e.g., Arduino UNO
Personal computer (I’m only going to talk about Windows)
USB cable, one that will connect to the Arduino
Arduino software (environment), get it from:
Download it and install it on your computer.

Technobabble: Yes, I know there are a lot of variations on these requirements, but I’m LAZY.

So this will (probably) create a directory under your ‘Documents’ called Arduino and a subdirectory named Arduino-1.0.3 (or whatever version you downloaded) and a bunch of subdirectories. I call this the Arduino environment.
One of the files is called Arduino.exe. (see picture)
Suggestion: I would right-click on Arduino.exe and ‘send to desktop’. I even changed the name to Arduino.exe-1.0.3 as I have a couple of versions I use at different times.
WARNING: Some sketches, especially those written before version 1.0 will not work on newer versions.

Notice that I have a directory called ‘MTS’. This is where I create most of the sketches I write. I have this directory under Arduino-1.0.3 as that is where I developed it and ‘know’ that it works.

The next picture shows the location of a sketch, Sketch1.ino.
TIP: Arduino sketches all have the file extension of .ino or .pde. I think this is required for them to work. I don’t know (or care) where they came up with these extensions.

TIP: Notice that Sketch1.ino is under a directory called Sketch1. This is important! When writing sketches they must be in a directory with the same name as the sketch and capitalization is important. If memory serves me the name cannot start with a number.

Writing/editing Sketch files: Sketch files are text so they can be edited using Wordpad. (For some reason when I tried Notepad, the line feeds weren’t there). You can also use other text editors. Now I like to use a free text editor called Notepad++:

Technobabble: Sketches are text files written in Arduino language. The Arduino language is basically C++.

Step 2: Arduino Environment

Arduino Environment

So let’s start.
Connect the USB cable to the Arduino and to a USB connector on your computer. Most likely you will see some LEDs blinking on your Arduino.
TIP: You may need to load the driver for the USB. There is a driver subdirectory under your Arduino-1.0.3 directory.

Click on that Arduino.exe icon. You will see something like this: (see picture)
The next thing you will want to do is click on Tools and select Serial Port. Unfortunately, I don’t have a screen saver to capture this).
Technobabble: Serial port? Those went away years ago. Well, not exactly. USB stands for Universal Serial Bus. This USB driver actually emulates the ‘old’ serial ports of the good ole days.

So you may see only one serial port listed and it will be checked. Yeah, you are in luck and don’t have to figure out which serial port is connected.
But sometimes, I have a bunch of stuff connected and have to figure out which serial port is the right one. If you just connected your Arduino, it’s probably the largest number.
TIP: If you disconnect and re-connect your Arduino, you might have to close and open Arduino.exe. Sometimes you might have to restart your computer.

The next thing you need to do is click on Tools and select Board. Now a list of Boards will appear. Select the one you are using, e.g., Arduino UNO.

Step 3: The simplest Sketch

The first picture shows the simplest sketch. It’s not even worth loading as it doesn’t do anything. But it’s a good learning step.
The Arduino language is very strict. When you have a  (  you must also have a  ) . When you have a  { you must have a  } .

All Arduino sketches require everything in this one to be there. There may be a bunch of code before void setup(). There may be a bunch of code between both of the { } s and there may be code after the last } but all of these needs to be in there somewhere.

WARNING: Capitalization is important. If you capital Void or Setup or Loop, it will not work.
TIP: While capitalization is important, spacing and line feed are not (usually). You could have:
void setup(){}    void setup(   ) {  }
void setup()
It doesn’t care.

TIP: void setup() everything between this { and this } is only run once.
TIP: void loop() everything between this { and this } is run repeatedly until power is removed.
Technically, this is not always the case, you can write the program where it gets stuck inside loop().

TIP: anytime you see a ‘word’ followed by (), that means it’s a function, e.g., setup(). A function is a group of instructions that do something specific. Sometimes it’s preceded by void and sometimes by something else. Sometimes there is stuff inside the ().
Technobabble: If there’s stuff inside the (), the stuff is called parameters. Parameters are information used by the function.

Technobabble: If the word preceding the function is not ‘void’, that means the function returns some information.
e.g.,    int addTwoNumbers( int x, int y) could be a function that takes the numbers, adds them and returns the results.
The thing that’s nice about a function is that you can call it many different times with different parameters and different results. E.g., z= addTwoNumbers(7, 4),  z is 11.
E.g., z= addTwoNumbers(33, -4),  z is 29.

Step 4: Second sketch

So let’s do something. Instead of starting with the usual blink.ino, I decided to start with the traditional Hello Word. (see picture)

Okay, assuming you wrote this and saved it into a file. Let’s load it.
Sketch comments: Notice that the two new statements are between the { } of the setup() function.
Sketch comments: The first line is Serial.begin(9600);.
Since all of you readers are so alert, you know that this is a function. (The .begin means it’s a function in the Serial library. ) This function is already in one of all those files you unzipped.
The 9600 is a parameter passed to the function. In this case it’s the serial baud rate (the speed at which the communications occur. So how do I know that this means baud rate? In this case, I would look on the Arduino website and find this:
Note also that Serial is capitalized.

And don’t forget the all important semicolon  ;
All code statements need to end with  ;

Technobabble: The Arduino language is based on the language C++ which is based on C. Most programming of microcontrollers and microprocessors is done in some variation of C. C and C++ statements are very close to the way micros work so the compiler code is much more compact than what you would see in most languages. I am fairly certain most Windows operating systems were written in a variation of C.
Technobabble: I have to mention why it’s called C++. One of the new operators in C++ is ++. What this means is increment. So C++ is an increment of the language C. Oh, those programmer geeks are so clever.

Connect Arduino and start Arduino.exe, set/check Serial port and Boards.
Click on File, then Open and navigate to where you have the sketch (sketch1.ino), then click on it. (see next picture)
Okay, this shows your sketch.
TIP: You can actually edit the sketch using the Arduino environment but it doesn’t allow you to easily save it. However, this is a good way to make minor changes to try to get things to work.

The next step is verify and upload. In the upper left corner, there’s a row of icons. The first is a check mark called verify and the second is an arrow called upload. I would call them compile and compile and upload. I always click on the upload arrow as it automatically does the compile first then the upload. This saves me a mouse click and saves me from getting carpal tunnel syndrome. HA! HA!

Technobabble: So what is compile? Many of you know that Arduinos only speak 0s and 1s or more exactly 0v and 5v. For those of you who don’t, you should read my Computers Are Dumb:
So what compiling does is convert the Arduino language to machine language that the Arduino microcontroller can understand.
Hint: When you select the Board type, you are actually selecting the microcontroller type. There are several.
While it is compiling, it is also looking for syntax errors. Syntax errors are like grammatical errors like a missing paranthesis ( or semicolon ; or misspelling or wrong capitalization.
If it finds an error, it will tell you by but putting a red message at the bottom of the Arduino screen.

Upload: So if the compile is successful, upload sends the machine code over the USB to the Arduino and actually starts running the program.

Results: In the Arduino environment, on the line of icons, there’s one all the way to the right. I don’t know what the symbol is supposed to be but it’s the serial monitor.
Click on it.
Another window will open up. This is the serial monitor(see picture). On the bottom right, click on the down arrow and select 9600 baud (so that it matches up with what we put in the program)
The “Hello World!” will show up on the serial monitor screen.
HINT: Whenever you start or restart the serial monitor, the program in the Arduino restarts.

How did it work? The Serial.begin(9600) opens up the serial port and sets the baud rate to 9600.
The Serial.println() sends whatever is between the quotes to the USB connection.
The serial monitor sees it and puts it on the screen.

Okay, that was too easy. I’m going to make one simple change (see picture).
Can you guess what’s going to happen?
TIPS: The first picture is Notepad++. Under Language, I selected C++. Please note that some of the words are in color. I think the purple means a keyword that has special meaning to C++(and Arduino).
Also note if you highlight a parenthesis or bracket, it will highlight the corresponding one. Numbers are in orange and text is grey. This can be helpful if you’re writing code.

Here’s the results(see picture). Can you explain the difference?
Hint: Read the Tips in the last step.


For more detail: LOG Arduino Sketches 101

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.

Follow Us:
Scroll to Top