Arduino – Sketch, Taking a Look at Programs



Last time, we looked at various calculations using Ohm’s law as a starting point. It’s an unavoidable subject in order to design an electrical circuit, but expanding your knowledge while thinking hard and worrying about all those things is on the other hand really fun. So, I would be very happy if you could put things into practical exercise as you immerse yourself in Arduino.
Starting from this article, alongside designing an electrical circuit, we will touch a little bit at a time on Arduino programming.

Understanding the Function and Flow of a Program That Makes LEDs Light Up

Below, you can see the program code for lighting up a LED, as we learned last time.

Seeing this code might be difficult to comprehend for people who have no experience with programming. Please don’t be worried, because you will get used to it as we investigate each item one by one. Programming is basically defined by a programming language, so each language has its own grammar, and following the rules of this grammar, we will go on giving commands to the computer. The computer will go on executing the commands in the given order, so through programming, we will have the computer achieve what we have in mind.

Putting it easily, the computer will not do anything which we haven’t included in the program. (It will only do the things we wrote.)
Looking at it from the other side, a program is like a very obedient child, which moves completely according to the commands.

Among the various programming languages, programming in Arduino is simply called making a sketch. In the case of other programming languages, in order to command a single process, it is often required to explain the computer down to the most subtle behaviors in order to have it work as you intend. In Arduino it is very simple to describe the required processes. Well then, let us go into each element one by one.

Arduino’s Fundamental Structure: Setup and Loop

Basically, an Arduino program runs on two main big structures. These are setup() and loop().

setup() function

The setup() function is the very first process which is called when Arduino is connected to power and starts up. In this instance, we use it to set up the pins.

setup() – The very first process which is called when Arduino has started up

In a program to light up LEDs, we use the command pinMode(). pinMode() is a command which is used to set up which of the several Arduino pins should be used in which way. In this instance, we used the command pinMode(13,OUTPUT), which announces that we use pin 13 as an output. If you would set it up as pinMode(13,INPUT) on the other hand, when the electric current reaches the Arduino main body from the wire connected to pin 13, pin 13 will use the value of this current as an input into the Arduino main body.

loop() function

loop() is the main programming component of Arduino. When Arduino is connected to a power source, after setup() has been processed, this loop() function will execute the process contained in the loop repeatedly until power is disconnected.

loop() – initiating after setup(), the process will be repeated until power is disconnected

Now, let us take a look at the concrete content of the loop() function.

This digitalWrite() function is used when setting an electric current as output for a specified pin number. The output is generally 5V.

The next function in the program is delay(). This command is used to let Arduino wait a specified amount of time. In this instance, it is used to let it wait for one second while the LED is lighting up.

The digitalWrite function is used again immediately after this to set the output to LOW(0V), and then delay is used again to let it wait for 1 sec while the LED is off.

Putting together the flow of the process looks as follows:

  1. loop() function starts the process
  2. Set pin 13 output to 5V: Turns on the LED – digitalWrite(13,HIGH);
  3. Wait 1 sec: delay(1000);
  4. Set pin 13 output to 0V: Turns off the LED – digitalWrite(13,LOW);
  5. Wait 1 sec: delay(1000);
  6. Process continues by going back to the start of the loop()

So, this would be the program to let a LED switch on and off alternatively for 1 second.

Adding a Little Bit to This

So, now you understand the flow of a program, let us try to make some alterations to both the program and the electric circuit. Until now, we’ve been using a single LED, but let’s try using two now. Let’s first make the LEDs light up at the same time, and then think about how we can make them light up in an alternating way. Before we go into the explanation, I would recommend you to try thinking about it once for yourself.

Lighting up Two LEDs Simultaneously

Arduino lighting 2 leds

There are various methods you could think of in order to light up two LEDs simultaneously, but if we were to adjust the program and circuit we’ve been using so far, you could do it by adjusting the circuit as follows.


How would we go about having them light up in an alternating way? For this, let’s try using an additional pin in addition to pin 13 as output.


Have you gotten used to adjusting the electrical circuit as you make changes to the program? Now, to conclude, let us try lighting up a row of 4 LEDs in order. While you’re at it, you can experiment by making the value of the delay longer or shorter.


Starting from the next article, we’ll create an actual functional product as we use various electrical components.