L O A D I N G
blog banner

Getting started with Arduino: Writing your first Program

Welcome to my programming tutorial with an Arduino. As promised, these tutorials can be understood by you even if you’ve never written a line of code. Let’s begin with understanding the basic structure of an Arduino and then we’ll jump to learning how to write our own code. Before beginning with this tutorial, make sure that you have the following:

  • Arduino UNO with cable
  • LED x 1
  • Resistor – 220 Ohm
  • Arduino IDE

Structure of an Arduino

Image Credits: https://www.electronicwings.com/arduino/digital-gpio-of-arduino

Take a look at your Arduino and if you have an Arduino UNO, you’ll notice 13 GPIO pins, these pins are where we’ll be connecting our components’ positive terminal. The negative terminal always goes to the ground. On the other side, you’ll notice Analog pins labeled as A0-A5 and other pins such as 5V, 3.3V, GND, etc. We’ll know more about them as we proceed with the tutorials.

Structure of an Arduino Code

There are two basic blocks in an Arduino Code, known as void setup() and void loop(). The part with parenthesis is known as a function so, setup and loop are two functions.

  • void setup(): Every piece of code that is written in this block is described only once in a program, i.e., in the beginning.
  • void loop(): Everything written here, in this block is iterated again and again in the program. So, if you want something to happen continuously with a component, this is where your code for that particular component should go.

Blinking an LED

For the starters, connect the positive terminal (longer leg) of the LED to a 220 Ohm resistor and the other end of the resistor to GPIO 13, as shown in the circuit diagram. Connect the negative terminal (shorter one) of the LED to GND pin.

Circuit Diagram – Designed using Adobe Tinkercad

Code

While writing the code, you’ll have to decide what is to be declared in the beginning and what has to continue happening till we decide to stop it. For instance, in the beginning, we need to declare the pin number of the LED and whether it is INPUT or OUTPUT. INPUT is used when we need a value from a sensor, while OUTPUT is used when we have to display something, it can be on the LCD, LED, OLED, etc. The function that has to be used only once is pinMode(). The ones that have to be repeated are, digitalWrite() and delay(). So, let’s begin with the code. Have a look at the comments (beginning with ‘//’ to understand it).

void setup() // The code is executed only in the beginning
{
  pinMode(13, OUTPUT); // Taking output at the component attached to PIN 13
}
void loop() // The code is executed repeatedly
{
  digitalWrite(13, HIGH); // Ordering the LED to glow
  delay(1000); // Wait for one second. The unit used here is milliseconds.
  digitalWrite(13, LOW); // Ordering the LED to turn off
  delay(1000); // Wait for one second
}

In the setup block, we’ve defined pinMode(13, OUTPUT); to tell the IDE that we have an LED connected to pin 13 of our Arduino Board and we expect an output inside it. In the loop block, we’ve told the IDE that we want to glow the LED, that is at pin 13 for one second and we want to turn it off for one second after that. Theoretically, this block will keep repeating forever.

Execute the given code and try to type it up yourself in the Arduino IDE. If the LED is not blinking, test it with a normal battery as there might be a fault. If the problem still persists or you have some questions, feel free to drop a comment below and let me know. Stay tuned for the next part of the series.

Happy Tinkering!

Leave a Reply

Your email address will not be published. Required fields are marked *



shares