Tuesday, February 17, 2015

BASIC INPUT OUTPUT IN ARDUINO

This tutorial will focus on how to use basic arduino IDE for input & output & reading writing basic I/O  data to any pin or from any pin.

command line for an arduino program uses

void setup() function to make permanent initializations of pins.

void loop() function includes those statements that keep repeating throughout the program execution.

FOLLOWING are some basic i/o functions:
int sensor_x= A1;   
#define sensory A2;

pinMode(1, INPUT);
pinMode(3,OUTPUT);
pinMode(A0, INPUT);
pinMode(sensor_x, INPUT);
pinMode(sensory, INPUT);

All the above functions are different ways to select a single pin as input or output/ analog or digital. & are necessarily a part of Void Setup() function.
The pinMode command here is similar to
DDRX command in AVR or Port initialization command in 8051.

digitalWrite(3,HIGH);
int data_on_pin_1= digitalRead(1);
int data_Analog0=analogRead(A0);
int data_Analog1=analogRead(sensor_x);
int data_Analog2=analogRead(sensory);
analogWrite(3, 150);

Semicolons are a must. Also Its a case sensitive language so watch the keywords properly.These can be either a part of Void setup() or void loop() function depending on the nature of your application.

DigitalWrite has 2 arguments ( pin number, value)
For digitalWrite the values can be HIGH (means logic 1 ) or LOW (means logic 0).

For digitalRead values read will only be either 1 or 0.

For analogWrite, since we hv an 8bit DAC, the values can be  between 0-255.
thus the resolution becomes (5/256)
So analog voltage output at the PWM pin when you give analogWrite value 100 will be
100 * (5/256)= 1.95 volts.
value 0 will act as LOW.
& Value 255 will act as HIGH.

For analogRead, we have an inbuilt 10 bit ADC & so the values can be between 0-1023.Thus the resolution becomes(5/1024).
So analog Voltage input will be written to the integer data_analog1. if you have your sensor connected to pin 0.

See them as digitalWrite & analogWrite equivalent to PortX command in AVR.
& analogRead & digitalRead equivalent to PinX command in AVR.

we'll use these commands for manipulation in coming tutorials.

& also learn about serial communication with other Pc & making yout microcontrollers speak to each other.

If you have any questions or doubts, feel free to ask them in comments below.

-Abhirup

No comments:

Post a Comment