Você está na página 1de 22

By Sudheer Katta

What is ARDUINO ??
Arduino is an open-source electronics prototyping platform. Based on flexible, easy-to-use hardware and software.

It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments

How Arduino Hardware Helps Us?


Printed Circuit Board with Microcontroller , serial to USB converter IC which is used in interfacing.

Board with Computer and Power management circuit

Does it have a Hardware ?


Check out all the products on Arduino.cc

Fig : Arduino Nano(smallest)

Fig : Arduino UNO (mostly used)

Fig : Arduino Leonardo

Fig : Arduino lillypad (from its design)

How to Program Arduino Board ??

Its an Open source You can download it from Arduino.cc

Arduino IDE Software !!

Is it Hard to code in Arduino


As simple as C /C++

Ample of Libraries , Projects and References on its website


For now we will work on Arduino duemilanove board You can use any of the hardware for your projects

ARE You excited to start coding and checkout that little hardware (Arduino)

Basic Syntax

Every Arduino code has the following format void setup(){...} void loop(){...}

Setup() function description

The setup()
function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the Arduino board

Loop() function description

loop () :
After creating a setup() function, which initializes and sets the initial values the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.

Outline of function
Digital I/O : To control digital Input and output pins
pinMode() digitalWrite() digitalRead()

Analog I/O : control Analog Pins


analogRead() analogWrite()

Serial Communication : To communicate with compute


Serial.begin() Serial.print() Serial.read()

DIGITAL I/O : pinMode()

Description : Configures the specified pin to behave either as an input or an output.

Syntax pinMode(pin, mode) Pin - the number of the pin whose mode you wish to set Mode - either INPUT or OUTPUT Returns: None

DIGITAL I/O : digitalWrite()


Description : Write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup resistor. Writing LOW will disable the pullup.

Syntax digitalWrite(pin, value) Pin - the pin number Value - HIGH or LOW Returns: none

DIGITAL I/O : digitalRead()


Description : Reads the value from a specified digital pin, either HIGH or LOW.

Syntax digitalRead(pin) Pin - the number of the digital pin you want to read (int) Returns: HIGH or LOW

Analog I/O : analogRead()


Description : Reads the value from the specified analog pin. The Arduino board contains a 6 channel 10-bit analog to digital converter. It will map input voltages between 0 and 5 volts into integer values between 0 and 1023. It takes about 100 microseconds (0.0001 s) to read an analog input. Syntax analogRead(pin) : Pin - the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega) Returns: int (0 to 1023)

Analog I/O : analogWrite()


Description : Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying bright nesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz.

Pins 3, 5, 6, 9, 10, and 11 can be used as PWM. You do not need to call pinMode() to set the pin as an output before calling analogWrite().

Syntax analogWrite(pin, value) pin: the pin intended to write value: the duty cycle: between 0 (always off) and 255 (always on) Returns: None

SERIAL COMMUNICATION
Used for communication between the Arduino board and a computer or other devices.
All Arduino boards have at least one serial port (also known as a UART or USART). It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB.

Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.

SERIAL FUNCTIONS
Serial.begin(baud_rate) Possible choices of Baud rate are: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200
Serial.print(val) Val is the value to be printed. Any data type Serial.read() Does not take any parameters. Returns the first byte of incoming serial data available (or -1 if no data is available).

Delay functions
delay() : Pauses the program for the amount of time (in milliseconds) specified as parameter delayMicroseconds() : Pauses the program for the amount of time (in microseconds) specified as parameter

Lets start coding !


EXAMPLE
int buttonPin = 3; // pin numbered 3 on Arduino void setup()// setup starts serial and the bpin { Serial.begin(9600); pinMode(buttonPin, INPUT); } // loop checks the button pin each time and will send serial if it is pressed void loop() { if (digitalRead(buttonPin) == HIGH) serialWrite('H'); else serialWrite('L'); delay(1000); }

Test your self


TASKS TO BE DONE DURING THE SESSION
Take Digital input from pin 7 and output same value on pin 8. Display values on the screen

SOLUTION
// this constant won't change: const int buttonPin = 2; // the pin that the pushbutton is attached to const int ledPin = 13; // the pin that the LED is attached to // Variables will change: int buttonState = 0; // current state of the button void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); // compare the buttonState to its previous state if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); Serial.println("on"); } else { digitalWrite(ledPin, LOW); Serial.println("off"); } }

Você também pode gostar