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.
Below, you can see the program code for lighting up a LED, as we learned last time.
1 2 3 4 5 6 7 8 9 10 11 12 |
//setup - The very first process which is called when Arduino has started up void setup() { //This command is used to let Arduino know to use pin 13 for output pinMode(13, OUTPUT); } //loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected void loop() { digitalWrite(13, HIGH); //Setting the pin 13 output to HIGH=5V delay(1000); //1000 msec = wait 1 sec digitalWrite(13, LOW); //Setting the pin 13 output to LOW=0V delay(1000); //1000 msec = wait 1 sec } |
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.
Basically, an Arduino program runs on two main big structures. These are setup() and loop().
1 2 3 4 5 |
//setup - The very first process which is called when Arduino has started up void setup() { //This command is used to let Arduino know to use pin 13 for output pinMode(13, OUTPUT); } |
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
1 2 3 |
void setup() { //Here you will give the commands which Arduino needs to execute after starting 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.
1 2 3 4 5 6 7 |
//loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected void loop() { digitalWrite(13, HIGH); //Setting the pin 13 output to HIGH=5V delay(1000); //1000 msec = wait 1 sec digitalWrite(13, LOW); //Setting the pin 13 output to LOW=0V delay(1000); //1000 msec = wait 1 sec } |
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.
Now, let us take a look at the concrete content of the loop() function.
1 |
digitalWrite(13, HIGH); //Setting the pin 13 output to HIGH=5V |
This digitalWrite() function is used when setting an electric current as output for a specified pin number. The output is generally 5V.
1 |
(1000); //1000 msec = wait 1 sec |
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.
1 2 |
digitalWrite(13, LOW); //Setting the pin 13 output to LOW=0V delay(1000); //1000 msec = wait 1 sec |
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:
So, this would be the program to let a LED switch on and off alternatively for 1 second.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//setup - The very first process which is called when Arduino has started up void setup() { //Telling Arduino to use pin 12,13 for output pinMode(12, OUTPUT); pinMode(13, OUTPUT); } //loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected void loop() { digitalWrite(13, LOW); digitalWrite(12, HIGH); delay(1000); digitalWrite(12, LOW); digitalWrite(13, HIGH); delay(1000); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//setup - The very first process which is called when Arduino has started up void setup() { //Telling Arduino to use pin 2,5,8,13 for output pinMode(2, OUTPUT); pinMode(5, OUTPUT); pinMode(8, OUTPUT); pinMode(13, OUTPUT); } //loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected void loop() { digitalWrite(13, LOW); digitalWrite(2, HIGH); delay(150); digitalWrite(2, LOW); digitalWrite(5, HIGH); delay(150); digitalWrite(5, LOW); digitalWrite(8, HIGH); delay(150); digitalWrite(8, LOW); digitalWrite(13, HIGH); delay(150); } |
Starting from the next article, we’ll create an actual functional product as we use various electrical components.