Sunday, 27 December 2015

Getting started with Arduino

Introduction to Arduino


Arduino is an open-source prototyping platform based on easy-to-use hardware and software. Arduino is simple to learn and easy to use and we can do many innovative projects with help of arduino. An Arduino board historically consists of an Atmel  8-, 16- or 32-bit AVR  microcontrollerTo do programming in arduino we have to use arduino programming language and arduino software(IDE).We write our program in arduino software(IDE). The Arduino IDE supplies a software library called "Wiring" from the Wiring project, which provides many common input and output procedures.
There are various types of arduino boards available like arduino uno, arduino mega, arduino nano, arduino le0rnado  etc. Based on our need we can choose particular board. Let us start our learning by simple and effective arduino board - arduino uno.
Arduino also has some shields which can interfaced with arduino for specific purpose like GSM shield for using GSM, Ethernet shield for internet. Wifi shield for connecting arduino with wifi etc.these shields are readily available and can be easily interfaced with arduino.

Arduino uno

The Arduino uno has ATMEGA328P microcontroller. To power the arduino simply connect it with USB cable or use proper adaptor for power supply.The operating volate is 5V DC. There are 14 digital I/O pins where we can give digital input or we can take digital output. and out of these 14 pins 6 pins are PWM(pulse width modulation)pins.and there are six analog input pins where we can give analog input.it is used when we are using an analog sensor like temperature or pressure sensor. we can connect these sensors to analog input pins.it also has Rx and Tx pins used for serial communication.which can be used while connecting with GSM. To power the arduino simply connect it with USB cable or use proper adaptor for power supply.The operating volate is 5V DC. Arduino uno uses 16 MHZ quartz crystal for clock input.

Image of Arduino uno


Image result for arduino uno



Programming in Arduino


Let us start by simple program blinking of LED

# define LED_PIN 13
void setup() 
{
pinMode(LED_PIN, OUTPUT); // Enable pin 13 for digital output
}
void loop() 
{ 
digitalWrite(LED_PIN, HIGH); // Turn on the LED 
delay(1000); // Wait one second (1000 milliseconds)
digitalWrite(LED_PIN, LOW); // Turn off the LED 
delay(1000); // Wait one second
}


let us assume that a LED is connected to pin 13.This is the simple program to make led blink for every second.At first we are defining pin no 13 as LED_PIN. we can define it by any name like #define LED 13; or #define Light 13.what ever name we are defining we have to use that in our program.instead of # define we can also use like this -  int LED_PIN=13; or # define LED_PIN 13.

Every arduino program has two functions
1) void setup()-Runs only one
2)  void loop()- runs infinitely 

In void setup() we will be assigning whether given pin is used as input or output.so in this program we have assigned LED_PIN( pin 13) as output.so this pin should be used as output not as input.

void loop ()- our actual coding starts here, we write our program/logic here.In the above program first statement of void loop () is digitalWrite(LED_pin, High). The function of digital write () is to make specified pin high or low.In above statement  pin 13 will be high and LED which is connected to pin 13 will glow.and next we are calling delay function, here we are providing a delay of 1 second,so pin will remain high for at least one second, the next statement is digitalWrite(LED_PIN, LOW);, this will make pin 13 as low and LED will turn off and again we are calling delay function, so LED will be off for at least one second.This code keeps on repeating indefinitely and LED continuously blinks.

Here we are connecting LED to pin 13 directly, suppose if we want to automate a Big lamp, we can connect it directly because Lamp will not glow in 5 Volt output from arduino, so for this purpose we need to use a transistor and a relay. Here transistor will act as a switch, base of transistor is connected to output pin of arduino and if output of arduino is high,  transistor will turn on and relay coil is connected to output of transistor will be energized and with help of relay we can control lamp.if information about transistor is needed refer my article on transistor.

Next let us see another simple program 

int LED_pin =13;
int inpin=7; 
int val=0;
void setup()
{
pinMode(LED_pin,OUTUPT);// enable pin 13 for digital output
pinMode(inpin,INPUT);// enable pin 7 for digital input
}
void loop()
{
val=digitalRead(inpin); // read the digital value and store it in val
digitalWrite(LED_pin,val); // turn on or turn off the led based on the value from val.
}

 Let us assume an LED is connected to pin 13 and a digital input is connected to pin  7.
Here first we are defining LED_pin as 13 and inpin as 7, and we are declaring a variable val.and in setup() part we are defining LED_pin as output and inpin as input. and loop part first statement is 
val=digitalRead(inpin);this will read digital value from the pin no 7 and store it in variable val, the function digitalRead() is used to read value from the specified  digital pin either high or low.and next statement is digitalWrite(LED_pin,val); it means the LED_pin will be high if val is high, ie if input given to pin no 7 is high then value stored in val is high and if input is low and value stored in val is low. so based on the value on "val"the LED connected to pin no 13 will turn on or off.

No comments:

Post a Comment