Programming Arduino Uno
Hello there! In the previous blog, we introduced you to Arduino. Now, it is time to try your hand at programming.

Generally, we code in Arduino IDE, compile it and upload it to the Arduino. And the Arduino mostly behaves as expected. Simple? The programming language we use for programming the Arduino devices is Arduino Programming Language. But this is nothing to be scared of, it is just a framework built on top of C++, which means most syntax is the same.

But there are quite a few differences too. One of the main differences is that unlike C++, it doesn’t have a main() function, but it does require two main functions: setup() and loop(). A program written in Arduino Programming Language is called Sketch.

Now let’s start programming our Arduino board. If you’ve been following the blogs so far, you must know what we are going to use for simulation.


  1. Go to tinkercad and create a new personal account. You can sign in with google for your ease.

  2. You will see a screen like this after completing the signup process.


  3. Select Circuits from the left panel and then create new circuit.


  4. Next, you should see a blank screen with some components listed on the right side. Search for Arduino Uno R3, LED, and a resistor and drag them on the blank screen one by one.

  5. Make connections like the ones shown below and click on the Start Simulation button on the top right.


  6. Wow! The led is blinking. Note that not only the LED which we connected to the Arduino but the LED on the board is also blinking.




But you didn’t program it to do so. How did it happen? Let’s figure that out. First, Stop Simulation. Click on that little code button near the Start Simulation button. Open the Blocks menu and click on Text. You should see this:


This is the default program that is causing the led to blink.

Let’s try to understand how this program is doing so.

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  digitalWrite(13, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(13, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}


The first thing is you can see two functions: setup() and loop().

setup(): This function is called at the very beginning and only once, when the program starts, and when the Arduino is shut down and restarted. So, for example, if you wanted something to be done only once, you will include those tasks in the setup.

loop(): This function is repeatedly called while the program is running, this function governs the regular functioning of the Arduino board. So, if you want a task to be done repeatedly by an Arduino board then those tasks must reside in a loop().

Line 3: pinMode(13, OUTPUT);

This sets pin 13 on the Arduino board as an output pin.

Lines 8, 10: digitalWrite(13, HIGH); and digitalWrite(13, LOW);

These lines are used to set pin 13 high and low respectively.

Lines 9, 12: delay(1000);

These lines are giving a delay of 1000 milliseconds between setting pin HIGH and LOW.

As the loop() is called repeatedly, the led starts blinking.


Isn’t it amazing? Congratulations! You learned the “Hello World” of Arduino today. For more information on Arduino programming language you can refer to the this link.

Now it's the time for you to do a small assignment (Mail your answers to puneet@zine.co.in):

  1. Why is the led on the Arduino Uno Board blinking?

  2. What will happen if we remove “delay(1000)” from the last line in the above program? Why do you think it is happening?

  3. What is the extension of a sketch?

  4. Redraw the circuit shown below.


  5. Write a program such that the led lights up when the push button is pressed and turns off when we release the push button. Submit the code below.

  6. Simulate the hypothetical circuit you studied in Circuits Elements blog on tinkercad and share the link.