In the previous article related to Arduino ,we have acquainted with the board and familiarized the pins.Here we will say about the programming section of Arduino. For executing and compiling the code to check results for yourself ,we recommend installing Arduino IDE software in your PC.You can download it from www.arduino.cc official website. Here we well be explaining the programming part along with a example of blinking a led .Arduino programming has language very similar to the C programming language.You write your code in the IDE ,compile the code,upload it in your microcontroller and run it.
Familiarizing the IDE
The Arduino IDE comes with lots of pre-installed libraries and ports.You can also import libraries and board and include them in our code.There in the IDE you have the files,edit,sketch,tools and help menu.From the tools menu you can select your board and ports and even adjust other options.Just below these menu you have the verify,upload,new,open and save buttons in order.The search icon on the top right corner of the IDE will direct you to serial monitor through which you can both send and receive message to and from the microcontroller.
In the bottom portion you have a break screen .This will show all your error messages and indicate if the code was uploaded successfully , if compilation is done, etc. In the lop left you can see you file name of your sketch is present.
The Code
In the beginning of the you have the space to declare global variables(like int x;), define pins, include the libraries (eg #include <SoftwareSerial.h> ) .Mainly there are two important sections(functions) in the code the void setup and void loop.Setup function is where you set ready the pins for connection. Like you specify the pin and the mode(if its input or output) you are using ,baud rates ,the status alarms etc.
You use the pinMode function to specify the pin number and the mode of operation.It has 2 arguments .
example - pinMode(13 ,OUTPUT) ;
#define name pin is also a method to define the pins.Usually you use this to define the analog pins, like #define abc A0
The Serial.begin() function helps you to set the baud rate(communication speed) in bits per second.It tells the arduino to get ready to exchange the messages with serial monitor at the specified rare. You can provide it as argument simply like Serial.begin(9600);
The Serial.print() function is used so that you can see the value displayed on the monitor.If you use println() ,then print the value inside the function in the next line.
For example if the temperature sensor is hooked to arduino and we can see the recordings in the monitor and mark changes.
Another example is that using an ultrasonic sensors can give you proximity measures.
Comments in the arduino code are given at end of a statements using the " // " symbol.Multiline comments can be enclosed by /*......................*/
One thing you must not forget is that you have to end each code statement with " ; " symbol.
Inside the loop function you can put in the code you wish , that should repeat over again and again infinitely.the INPUT and OUTPUT here is mentioned with respect to the microcontroller only.
Now there are some other methods that you should know .We know that there is both ani=alog and digital pins in the board.So how do you take reading and send inputs to the components(output with respect to the arduino).The digitalRead(), digitalWrite() , analogRead() and analogWrite() function helps us in this.
The digitalWrite() function is used to write into a digital pin ,to give output either HIGH or LOW.It has 2 attributes ,the pin number and the HIGH or LOW.
eg.
digitalWrite(pin,HIGH) ; //for 5V or 3.3V maximum output. and digitalWrite(LOW); // for 0V minimum output
The digitalRead() function has only one argument ; the pin number.It return either a "HIGH" or "LOW" value.Simply it gives the status of any digital pin in arduino. Here instead of giving pin number as an argument you can also give the defined variable to avoid repeatedly using the number.
digitalRead(pin) ;
The analogWrite() function is simply used to give variable inputs (PWM values) to the devices(OUTPUT from arduino) .There is a range of value from 0 to 1023 that you can give as the argument.This simply gives you any voltage value in between 0V and 5V.
analogWrite(pin,value);
The analogRead() function is used most often to read values(variable analog) from a sensor or any other device.These have only one argument , the pin number.
analogRead(pin) ;
Disadvantages
Although the arduino software comes with these much of features there are also some disadvantages with arduino.
There are libraries available ,but some of which we would need to download it from the internet and then install them.Although we intervene with the hardware and software, we don't get to know much on what is actually happening inside the microcontroller. It is even easy to copy paste the code available in the internet.But that isn't a good practice.
First of all you need to get understanding of the process and then code by yourself.You can check code available on net for references.The libraries are designed for easy usage but have less computational efficiency.
Arduino has not been that powerful in order to stand up with the industrial hardware. On the other hand it is easy to adapt, utilize and has straight forward programming codes which is an advantage.
A Simple Program
Now let us look at a simple arduino program to blink a led.
First of all you define the pin on the board that you need to select as output.In the setup function u=you can specify the baud rate and also the pin and mode of transfer.Using the defined digital pin you can set it high whenever you want the bulb to glow.Setting this pin to low will put your pin output value to 0 , that means that the bulb wont glow.Specifying the delay you can provide the time up to which you need to keep your bulb on or off.
Circuit diagram is as shown below.
It is better to connect the led to a resistor , to avoid sudden flow of high currents to the led and blew ups.The anode of led is connected to the pin number 13 and cathode is connected to the ground to complete the circuit.
Full code for the above experiment:
int led = 13 ; // declaring the variable to store the pin number
void setup() {
// put your setup code here, to run once:
Serial.begin(9600) ; //baud rate or data transfer rate
pinMode(led,OUTPUT) ;
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led,HIGH);
delay(2000); //putting LED on for 2 sec
digitalWrite(led,LOW) ;
delay(2000); // putting led off for 2 sec
}
So the was a short guide for the beginners.Next we will also be back with some usage and properties of some components in the project side.
Keep reading!!
0 Comments