Basics of Arduino Programming
In previous tutorial we have studied basics of programming. Now if we have never programmed before we still know core elements of programming.
Let’s move forward by learning the basics of Arduino programming and setup its environment. Download Arduino IDE from Arduino Official Website for respective Operating System that you are using. In case you need help in setting up Arduino IDE follow this detailed tutorial ‘Setting Up Arduino Environment’.
If your Arduino Environment is setup you should be looking at this window.
This window is called Arduino Sketch Window. In this window you can cook your Arduino sketch and upload it to your board.
Let’s come to Arduino Programming.
Arduino programming language is a derived version of C Language. Difference is few additive and modified functions for hardware programming. So, if you already know C language you are all set with basics you just need to grasp few Arduino functions.
Core Components of Arduino Sketch
Arduino programming is based on few key things. The main part of Arduino Sketch is its two built-in functions.
- void setup()
- void loop()
void setup() This function is a sort of pre-setting function which runs only once we run code every time. It is used to establish few meanings and functions which we tend not to change while running of code. We essentially use it to define pin function once program starts.
*Whenever we reset our device or turn on and turn off. It run setup() once every time.
void loop() This function is the one which runs continuously throughout the running of our code. In this function our main functional code is written which can be a process or device running routine. It essentially uses the pre-sets defined in setup() and process the rest of functionality. Everything that we need to run and again we write in this loop() function
Now let’s study some pre-built functions which we need to make any device or robot. Following are those essential functions.
Before understanding these functions individually usually Arduino function are formed in this cemetery first letter is small alphabets and second letter start with capital alphabet like in above case analogRead has ‘analog’ in lower case and ‘Read’ is in upper case.
pinMode() This function is intended to be used for defining pin purpose in setup(). It can also be used in loop() but we mostly use it in setup() to define purpose of pin throughout running of program before we start to run our program. This function function take in two parameters like this: pinMode(pin_number,type) whereas, pin_number will be replaced with a number say Pin5 and type will be replaced with INPUT, OUTPUT, INTERUPT etc. So if we want to use Pin 5 as OUTPUT pin then this function will look like this: pinMode(5,OUTPUT).
Exception: Digital pins on board can be referred with the numbers corresponding to the specific pins. Whereas, in case of analog pins we will add letter corresponding to pin number i.e. A0,A1 etc.
digitalRead() This function is used to take input from digital pin. Which will always give value as 0 or 1 i.e. ON or OFF. Arduino IDE will interpret digital input as HIGH and LOW. In simple words it will tell us if there is any voltage on pin or not.
This function takes one parameter which is pin number and this is how we use it digitalRead(5). But to be sure if we are not making any mistake we need to define pin 5 as input using pinMode in setup().
digitalWrite() This function is used to give out digital output which is HIGH/LOW or 1/0. Arduino IDE instruct controller to give output as voltages on its pin this voltage is equal to output voltage micro-controller is capable of giving. This takes in one parameter which is pin number I.e. on which pin you have to give digital output.
analogWrite() This function helps us to give analog output it similar example from daily life can be changing speed of fan. We give variable (abruptly changing) voltage on output to vary speed or amplitude. This function takes in two parameters pin number and value of output i.e. analogWrite(pin_number, value). Pin number can be any number of pin on micro-controller and value can be any number between 0 and 255 (specifically for Arduino Uno with 8-Bit DAC).
Basically, it divides 0 and 5 volts in 256 equal steps. Value of each step in-case of Arduino Uno will be 5/225 equals to 0.0196v, this means it can give a changing voltage of as low as 0.0196.
analogRead() This function enables us to read analog input i.e. varying input to understand the input more specifically and detect change on input. This comes in handy while working with different sensors. This function actually detect voltage value on input and divide it into 1024 steps (specifically for Arduino UNO with 10-bit ADC). This function take in one parameter which is pin number similar to digitalRead(). And to understand the significance of change on input we can divide 5/1025 and its equal to 0.0048v which means it can change of input as low as 0.0048v.
delay() This function pauses further execution of the program for the provided amount of time. It accepts input in milliseconds, so if you give this function a value of 1000, it delays further execution of the program for a second, as one second equals thousand milliseconds.
Recalling the basic concepts of programming along with syntax of code we studied, we will write a code involving linkage of input and output to understand how to write a basic Arduino sketch.
Let’s start by writing a basic code in which we will take an input and trigger a digital output based of that,
int temp = 0;
int SensorPin = A1;
int AlarmLight = 13;
temp = analogRead(SensorPin);
if(temp > 25)
Variable/Memory: Variable and memory are generally of three types integer, characters and string which are written as follow.
int x = 10;
char letter = a;
String name = “Ali”;
In this syntax there are two notable things. We referred integers with a short from and we created a variable/memory element ‘x’ in which we store number 10 whenever we call ‘x’ it will tell number 10 or whenever we use or multiply ‘x’ it was consider it as number 10.
Similarly, we used ‘char to a character and likewise we used ‘String’ to store combination of characters. The use of ‘int’, ‘char’ and ‘String’ is to define the type of memory element. This helps in batter handling of memory organization and interpretation of different functions.
Secondly we used ; (semi-colon) at end of each line which means this is the end of line and now next line is being started.
Conditional Loop We can see a conditional loop in above example where a loop is running again and again with a condition that check temperature on each iteration and if there is a temperature change i.e. temperature higher than 25degrees turn on the alarm light.