Arduino Programming Language



Hello friends! Welcome back to ElectroDuino. This is Arduino Tutorial #3 – How To Program Arduino | Arduino LED Blinking. After understanding How to Download & Install Arduino IDE in Arduino Tutorial #2. In this blog post, we will discuss Introduction to the Arduino Programing Language, How To Program an Arduino in Arduino IDE Software, and as an example, we explain the Arduino LED Blinking code.

Arduino Programing Language

The Arduino software is open-source. The C/C++ Programing Language used in Arduino IDE to write the Arduino program. It is pretty much the simpler form of C language. Arduino IDE all functions and libraries are written in C/C++. Also, we can use Java programming language to communicate with the Arduino board via serial port communication. But Java will just complicate things in Arduino. So, here We will learn how to create a project in Arduino using the C language.

Sketch − The Arduino program terminology is called “sketch”.

Programming

Arduino programming structure

Arduino uses its own programming language, which is similar to C. However, it’s possible to use Arduino with Python or another high-level programming language. In fact, platforms like Arduino work well with Python, especially for applications that require integration with.

The Arduino programming structure consists of two function, one is Setup( ) function and another one isLoop( ) function

  • Setup( ) function

  1. Arduino is built around a straightforward programming language that's meant to be welcoming to newcomers — but if you're really short on experience, it might be worth trying a tool like ArduBlock. Rather than typing out your code, you'll be able to construct your program visually using the same selection of functions as you would normally.
  2. The Arduino language is C, but it is very different from most C varieties. The Arduino language has a lot of abstraction built in, especially in the hardware interfaces, which makes it very simple to use. If you have a background in Java, C and C should be very similar. The main differences between Arduino and C are in the memory storage.

The setup() function is called when a sketch starts. This function is used to initialize the variables, pin modes, start using libraries, etc. The setup() function will only run once, after power ON or RESET of the Arduino board.

  • Loop( ) function

After creating the setup() function,then the loop() function is called in sketch. It is also mandatory in an Arduino program. The code inside the loop() function runs again and again. This function does precisely what its name suggests, and loops continuously, also it allowing to change the program and respond. This function is used to actively control the Arduino board.

Arduino LED Blinking

Every programming language has a basic code “Hello, World!”. But in Arduino programming, it is an “LED Blinking” code. You are getting this code in your Arduino IDE example section.

We are using this code to better understanding Arduino programming. Using the Blink sketch, we will turn ON the LED for one second and turn it OFF for one second, it’s continually running. Arduino board has a LED, that is connected to Pin 13 of the Arduino.

Also, we will connect an external LED to the Arduino pin 13, to show “how to connect a LED with Arduino”.

Components Required

Components Name Quantity
Arduino Uno Board (you can use other types of Arduino board like Arduino UNO, MEGA, pro mini, etc)1
USB cable for Arduino Uno1
LED1
330-ohm Resistor1
Connecting wireAs per required in the circuit diagram

Arduino LED circuit diagram

Circuit Wiring

LED PinArduino pin
LED positive (+) terminal connected to Arduino 13 pin through a 330-ohm resistor
LED negative (-) terminalconnected to Arduino GND pin

How To Program Arduino – LED Blinking Program/sketch

At first, open your Arduino IDE software. Then open – File >> Example >> Basics >> Blink.

After following the above process, now you reach the blink LED example code window.

Before uploading the program to the Arduino board, we should “Verify or Compile” the code. Click on the first symbol of the Arduino IDE Menu Bar to “Verify or Compile” the code. Now, compiling is started.

After compiling, if your code has no error then you can see the “DONE Compiling” message on the Output panel of Arduino IDE.

Arduino Programming Language Name

Upload code to Arduino board

Now code is ready to upload in your Arduino board. First of all, connect the Arduino board to your computer using the USB cable.

Then go to the Arduino IDE “tool” section for select the “Board” and “port”. In my case Board is “Arduino UNO”, you can choose the Board type according to you Arduino and port is “COM6”, but your port may be different.

All important steps are completed. So, click on the “Upload” button. When uploading is successful you get the “Done Uploading” message on the Output panel of Arduino IDE.

Output Result

As a result, you can see Arduino LED and external LED turn ON for one second and turn OFF for one second, it’s continually running.

Wondering what programming language you should learn to program the Arduino? The short answer is C++, but if you want to know why it’s not obvious, and why there are quite a few different answers still hovering about…

Let’s Start at the End

At the heart of most Arduino boards lie microcontrollers made by Atmel, specifically from their AVR family. The only commands these chips actually understand are the AVR Instruction Set – a relatively small collection of Assembly commands. You can find them on the microcontroller datasheet.

If you’re really into it, you can learn this language and program the Arduino directly in it. However, for most applications you’ll want to write your program in a “higher”, more advanced and comfortable language, such as C, C++, Python, Pascal, even Basic or Ada. That’s where Compilers step in: they are computer programs that take your higher-language code and convert it to AVR Instruction Set commands, so that the Arduino can understand and run them.

The Arduino Integrated Development Environment

The good folks who designed the Arduino software chose what is probably the most common solution in the industry today: the C++ programming language. Under the hood of the humble Arduino Integrated Development Environment (IDE) hides a compiler called WinAVR, which is in fact a version of the famous GCC compiler, used mainly for compiling programs in C and C++.

If you take a look at files deep in the Arduino installation directory on your computer – for example, on Windows, at …hardwarearduinoavrcoresarduino – you’ll find source code files with the postfix .cpp (i.e. CPlus Plus). Open them in a text editor and you’ll see they are indeed written in C++. Also, if you already know C++, you can write your Arduino code in it and see that it compiles just fine.

Given such overwhelming evidence, why do people still insist on other answers? Let’s find out.

Programming the Arduino in C

If – sorry, I mean AS 😉 – you follow this blog, you’ll see that even I will talk a lot about C programming in the context of the Arduino. While C is NOT a “subset of C++”, in most aspects it is very, very similar to one, and we’ll only rarely need the advanced features of C++, So for all practical purposes we can safely program the Arduino as if we were working with pure C.

A Language of Its Own

“Hang on now,” I hear you say, “if Arduino programs (‘Sketches’) are in C++, why do they have the .ino postfix? How come the obligatory main function is not in them? And, most important of all, why does the official Arduino website claim that ‘The Arduino language is based on C/C++’?” [emphasis mine]

First, microcontrollers are not PCs. for instance, they don’t have hard drives, and no file systems. So standard-library C++ functions to handle files will fail on them. Does that make the programming language different? No. It’s like saying that if I only know some Chinese, and not the whole Chinese language, what I do know is not really in Chinese. Of course it is, it’s just that I’m limited to a subset of the language.

On the other hand, the Arduino developers spent a lot of effort writing specialized code libraries to make it more beginner-friendly. Useful Arduino functions such as digitalWrite are definitely not part of standard C++, but again, they were written in C++, so it’s not a different language.

Another beginner-oriented modification is the use of the setup and loop functions instead of main. This is just a cosmetic change: behind the scenes, the compiler actually takes your code and incorporates it into a proper C++ skeleton code, with main and all. You can even write the main function yourself, although it will break some automatic functionality for that particular program.

Java, Processing and Wiring

While Arduino programs are written in C++, the Arduino IDE isn’t. It was written in Java, based in part on previous and related software called Processing and Wiring. These may be interesting on their own right, but they are not what you need to program the Arduino board.

Arduino Programming Language Guide

In Summary

Arduino Programming Language Tutorial

If you wish to program the Arduino, you’ll need to know some C and/or C++. There is no lack of books and websites to teach you these, but they are mostly focused on programming for PCs, which has a different character from programming for MCUs (Micro Controller Units) – so you will definitely want to check out Arduino-specific resources as well. Until I write more about it, here’s a good place to start.