Find out how HackerEarth can boost your tech recruiting

Learn more
piller_image

Arduino programming for beginners-1

Arduino programming

After understanding the hardware of the Arduino UNO board in the previous article,  let’s now get started with Arduino programming.

Arduino programs are written in the Arduino Integrated Development Environment (IDE). Arduino IDE is a special software running on your system that allows you to write sketches (synonym for program in Arduino language) for different Arduino boards. The Arduino programming language is based on a very simple hardware programming language called processing, which is similar to the C language. After the sketch is written in the Arduino IDE, it should be uploaded on the Arduino board for execution.

The first step in programming the Arduino board is downloading and installing the Arduino IDE. The open source Arduino IDE runs on Windows, Mac OS X, and Linux. Download the Arduino software (depending on your OS) from the official website and follow the instructions to install.

Now let’s discuss the basics of Arduino programming.

The structure of Arduino program is pretty simple. Arduino programs have a minimum of 2 blocks,

Preparation & Execution

Each block has a set of statements enclosed in curly braces:

void setup( )

{

statements-1;

.

.

.

statement-n;

}

void loop ( )

{

statement-1;

.

.

.

statement-n;

}

Here, setup ( ) is the preparation block and loop ( ) is an execution block.

The setup function is the first to execute when the program is executed, and this function is called only once. The setup function is used to initialize the pin modes and start serial communication. This function has to be included even if there are no statements to execute.

void setup ( )

{

pinMode (pin-number, OUTPUT); // set the ‘pin-number’ as output

pinMode (pin-number, INPUT); // set the ‘pin-number’ as output

}

After the setup ( ) function is executed, the execution block runs next. The execution block hosts statements like reading inputs, triggering outputs, checking conditions etc..

In the above example loop ( ) function is a part of execution block. As the name suggests, the loop( ) function executes the set of statements (enclosed in curly braces) repeatedly.

Void loop ( )

{

digitalWrite (pin-number,HIGH); // turns ON the component connected to ‘pin-number’

delay (1000); // wait for 1 sec

digitalWrite (pin-number, LOW); // turns OFF the component connected to ‘pin-number’

delay (1000); //wait for 1sec

}

Note: Arduino always measures the time duration in millisecond. Therefore, whenever you mention the delay, keep it in milli seconds.

Now, let’s take a giant leap and do some experiments with Arduino

  • Blinking the LED
  • Fade-in and fade-out the LED

In the process of experimenting with Arduino, writing the Arduino program is not the only important thing, building the breadboard circuit is equally important.

Let’s take a look at how the breadboard circuit has to be built for both the experiments.

Components required:

  • Arduino UNO R3 -1
  • Breadboard -1
  • Breadboard connectors -3
  • LED -1
  • 1K resistor -1

Blinking LED

Steps in building a breadboard connection:

Step-1: Connect the Arduino to the Windows / Mac / Linux system via a USB cable

Step-2: Connect the 13th digital pin of Arduino to the positive power rail of the breadboard and GND to the negative

Step-3: Connect the positive power rail to the terminal strip via a 1K ohm resistor

Step-4: Fix the LED to the ports below the resistor connection in the terminal strip

Step-5: Close the circuit by connecting the cathode (the short chord) of the LED to the negative power strip of the breadboard

circuit diagram of blinking LED with Arduino UNO

Arduino program for LED blink (Version-1)

int LED =13; // The digital pin to which the LED is connected


void setup ( )
{


pinMode (LED, OUTPUT); //Declaring pin 13 as output pin


}


void loop( ) // The loop function runs again and again
{

digitalWrite (LED, HIGH); //Turn ON the LED
delay(1000); //Wait for 1sec
digitalRead (LED, LOW); // Turn off the LED
delay(1000); // Wait for 1sec

}

Arduino program for LED blink (Version-2)

void setup ( )
{

pinMode (13, OUTPUT); //pin 13 is set as output pin

}

void loop( ) // The loop function runs again and again
{

digitalWrite (13,HIGH); // Turn ON the LED on pin 13
delay (1000); //Wait for 1sec
digitalWrite (13, LOW); //Turn OFF the LED on pin 13


}

In version-1 of the LED blink program LED is declared globally and is set to pin number 13. This will reduce the number of iterations required to update the pin number in the program when you connect the LED to the other digital pin. Whereas, the pin number has to be changed in 3 different statements in version-2.

The magic happens when you click the upload icon in the Arduino IDE. The program will be uploaded into the microcontroller of Arduino board and LED in the circuit starts blinking.

Arduino IDE

Fade-in and fade-out the LED

The step for the LED bling experiment are the same as those followed for building the breadboard circuit except that in step-2, you connect the 9th digital pin to the positive power rail of the breadboard.

Circuit diagram for fade-in and fade-out with Arduino IDE

Arduino program for LED fade-in and fade-out (Version-1)

int led = 9; // The digital pin to which the LED is connected
int brightness = 0; // Brightness of LED is initially set to 0
int fade = 5; // By how many points the LED should fade


void setup()
{
pinMode(led, OUTPUT); //pin 10 is set as output pin
}


void loop() // The loop function runs again and again
{
analogWrite(led, brightness); // set the brightness of LED


brightness = brightness + fade; //Increase the brightness of LED by 5 points


if (brightness <= 0 || brightness >= 255) // check the level of brightness

{

fade = -fade;

}
delay(30); // Wait for 30 milliseconds
}

The same output could be generated by modifying the above program.

Arduino program for LED fade-in and fade-out (Version-2)

int led=19; // The digital pin to which the LED is connected

void setup()
{

pinMode(led, OUTPUT); //pin 10 is set as output pin

}


void loop() // The loop function runs again and again
{

for (int fade=0; fade<=255; fade=fade+5)
{

analogWrite (led, fade); // Change the brightness of LED by 5 points
delay (30);

}
}

In both the versions of the LED fade-in and fade-out programs, the analogWrite statement is used for a led connected to a digital pin. The reason for this is that, digital pin 10 is a PWM pin. As discussed in the previous article, A tour of the Arduino UNO board, a PWM pin is capable of generating Analog output. In both the programs pin 10 is used as analog output pin.

If and for statements used in the above programs will be explained in detail in the next article.

Congratulations! You have completed the level-1 of Arduino programming. In level-2 of Arduino programming, we will discuss concepts like conditional statements, loops, and digital and analog I/O statements.

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

Related reads

Best Interview Questions For Assessing Tech Culture Fit in 2024
Best Interview Questions For Assessing Tech Culture Fit in 2024

Best Interview Questions For Assessing Tech Culture Fit in 2024

Finding the right talent goes beyond technical skills and experience. Culture fit plays a crucial role in building successful teams and fostering long-term…

Best Hiring Platforms in 2024: Guide for All Recruiters
Best Hiring Platforms in 2024: Guide for All Recruiters

Best Hiring Platforms in 2024: Guide for All Recruiters

Looking to onboard a recruiting platform for your hiring needs/ This in-depth guide will teach you how to compare and evaluate hiring platforms…

Best Assessment Software in 2024 for Tech Recruiting
Best Assessment Software in 2024 for Tech Recruiting

Best Assessment Software in 2024 for Tech Recruiting

Assessment software has come a long way from its humble beginnings. In education, these tools are breaking down geographical barriers, enabling remote testing…

Top Video Interview Softwares for Tech and Non-Tech Recruiting in 2024: A Comprehensive Review
Top Video Interview Softwares for Tech and Non-Tech Recruiting in 2024: A Comprehensive Review

Top Video Interview Softwares for Tech and Non-Tech Recruiting in 2024: A Comprehensive Review

With a globalized workforce and the rise of remote work models, video interviews enable efficient and flexible candidate screening and evaluation. Video interviews…

8 Top Tech Skills to Hire For in 2024
8 Top Tech Skills to Hire For in 2024

8 Top Tech Skills to Hire For in 2024

Hiring is hard — no doubt. Identifying the top technical skills that you should hire for is even harder. But we’ve got your…

How HackerEarth and Olibr are Reshaping Tech Talent Discovery
How HackerEarth and Olibr are Reshaping Tech Talent Discovery

How HackerEarth and Olibr are Reshaping Tech Talent Discovery

In the fast-paced tech world, finding the right talent is paramount. For years, HackerEarth has empowered tech recruiters to identify top talent through…

Hackerearth Subscribe

Get advanced recruiting insights delivered every month

View More

Top Products

Hackathons

Engage global developers through innovation

Hackerearth Hackathons Learn more

Assessments

AI-driven advanced coding assessments

Hackerearth Assessments Learn more

FaceCode

Real-time code editor for effective coding interviews

Hackerearth FaceCode Learn more

L & D

Tailored learning paths for continuous assessments

Hackerearth Learning and Development Learn more