ARDUINO By Kiran Tiwari BCT 2072 CoTS www.kirantiwari.com.np
SO What is an Arduino?
WELL!! Arduino is an open-source prototyping platform based on easy-to-use hardware and software.
Why Arduino? Simplifies working on a microcontroller Inexpensive Platform Simple, Clear and User Friendly Environment Easy Programming
MISSED SOMETHING? OPEN SOURCE!!
IMPORTING LIBRARY Open the Given Arduino Library Folder Copy the files to C:/Program Data/Labcenter Electronics/Proteus 8 Professional/Library/ Reopen Proteus and Use Arduino from Components Adding Panel NOTE : Program Data folder is Hidden
ARDUINO UNO DESCRIPTION 13 Pins, ~ PWM, 0&1: Rx & Tx ports USB Port SPI ( Serial Peripheral Interface) MISO MOSI Atmel ATmega 328 External Power Supply Analog Input Pins
Arduino IDE setup(){ // runs once at start of program // for initialization loop(){ // called repeatedly until board // powers off
Some Basic Programming Syntax In setup() pinmode( pinno, mode ); Mode : INPUT / OUTPUT Eg ; pinmode(13, OUTPUT); Serial.begin( baudrate ); baudrate : bits per second For Serial Communication Eg ; Serial.begin(9600);
Some Basic Programming Syntax In loop() digitalwrite ( pinno, state ); State : HIGH / LOW Eg ; digitalwrite (13, HIGH); digitalread ( pinno); Eg ; int value = digitalread(8); delay (milliseconds); Delays system by certain milliseconds Eg ; delay(1000); // for 1 sec
Some Basic Programming Syntax In loop() analogwrite ( pinno, value ); Value : 0 to 255 //(255/255) = 1 = max value Eg ; analogwrite (9, 255); // pinno with PWM analogread ( pinno); Eg ; int value = analogread (A0); Serial.println (data); Displays data on Serial Monitor with line break Eg ; Serial.println ( Text );
LED ON int ledpin = 13; void setup() { pinmode (13,OUTPUT); void loop() { digitalwrite (13,HIGH);
Exporting Hex File
Exporting Hex File
Ctrl + C Exporting Hex File
IN PROTEUS
IN PROTEUS
IN PROTEUS Ctrl + V
IN PROTEUS RUN THE SIMULATOR
LED BLINK ( IN PROTEUS )
LED BLINK ( IN IDE ) int ledpin = 13; void setup() { pinmode ( ledpin, OUTPUT ); void loop() { digitalwrite ( ledpin, HIGH ); delay(500); digitalwrite ( ledpin, LOW ); delay(500); Export Compiled Binary Run the Simulator
LED CHASER ( IN PROTEUS )
LED CHASER ( IN IDE ) void setup() { pinmode(11,output); pinmode(12,output); pinmode(13,output); Export Compiled Binary Run the Simulator void loop() { digitalwrite(11,high); digitalwrite(12,low); digitalwrite(13,low); delay(30); digitalwrite(11,low); digitalwrite(12,high); digitalwrite(13,low); delay(30); digitalwrite(11,low); digitalwrite(12,low); digitalwrite(13,high); delay(30);
LED TOGGLE ( IN PROTEUS )
LED TOGGLE ( IN IDE ) boolean currentstate = LOW; boolean laststate = LOW; boolean ledstate = LOW; void setup(){ pinmode(9, INPUT); pinmode(7, OUTPUT);
LED TOGGLE ( IN IDE ) void loop(){ currentstate = digitalread (9); if (currentstate == HIGH && laststate == LOW){ if (ledstate == HIGH){ digitalwrite(7, LOW); ledstate = LOW; else { digitalwrite(7, HIGH); ledstate = HIGH; laststate = currentstate; Export Compiled Binary Run the Simulator
LED FADE ( IN PROTEUS ) ~ PWM for analogwrite
LED FADE ( IN IDE ) int ledpin = 9; void setup() { pinmode( ledpin, OUTPUT ); void loop() { for(int i = 0 ; i<=255;i+=5){ analogwrite ( ledpin, i ); delay(10); for( int i =255; i>= 0; i -= 5 ){ analogwrite( ledpin, i ); delay(10); Export Compiled Binary Run the Simulator
LCD INTERFACING Liquid Crystal Display
LCD INTERFACING Liquid Crystal Display First Include LiquidCrystal header file #include<liquidcrystal.h> Create an Object for LiquidCrystal Eg ; LiquidCrystal lcd (12,11,5,4,3,2); Start the lcd lcd.begin (width, height); Eg ; lcd.begin (16,2); // for 16*2 LCD
LCD DISPLAY ( IN PROTEUS ) LM016L LCD RS pin to digital pin 12 LCD Enable pin to digital pin 11 LCD D4 pin to digital pin 5 LCD D5 pin to digital pin 4 LCD D6 pin to digital pin 3 LCD D7 pin to digital pin 2 LCD R/W pin to ground
LCD DISPLAY ( IN IDE ) #include<liquidcrystal.h> LiquidCrystal lcd (12,11,5,4,3,2); void setup() { lcd.begin(16,2); lcd.write("hello LCD"); void loop() { lcd.display(); Export Compiled Binary Run the Simulator
LCD BLINK ( IN PROTEUS ) LM016L LCD RS pin to digital pin 12 LCD Enable pin to digital pin 11 LCD D4 pin to digital pin 5 LCD D5 pin to digital pin 4 LCD D6 pin to digital pin 3 LCD D7 pin to digital pin 2 LCD R/W pin to ground
LCD BLINK ( IN IDE ) #include<liquidcrystal.h> LiquidCrystal lcd(12,11,5,4,3,2); void setup() { lcd.begin(16,2); lcd.write("hello LCD"); void loop() { lcd.display(); delay(500); lcd.nodisplay(); delay(500); Export Compiled Binary Run the Simulator
SOME MORE ( IN PROTEUS ) LM016L LCD RS pin to digital pin 12 LCD Enable pin to digital pin 11 LCD D4 pin to digital pin 5 LCD D5 pin to digital pin 4 LCD D6 pin to digital pin 3 LCD D7 pin to digital pin 2 LCD R/W pin to ground
SOME MORE ( IN IDE ) #include<liquidcrystal.h> LiquidCrystal lcd(12,11,5,4,3,2); void setup() { lcd.begin(16,2); lcd.write("hello Fellas!"); delay(1000);
SOME MORE ( IN IDE ) void loop() { lcd.clear(); delay(300); lcd.write("welcome"); delay(300); lcd.setcursor(9,0); lcd.write("to"); delay(300); lcd.setcursor(12,0); lcd.write("the"); delay(300); lcd.clear(); lcd.write("awesomeness!"); delay(300); Export Compiled Binary Run the Simulator
SENSORS Sense the Values Uses Analog Input Pins LDR ( Light Dependent Resistor ) Light Sensor Gas Sensor LM35 ( Temperature Sensor ) Ultrasonic Sensor ( Distance Measurer )
LDR ( IN PROTEUS ) TX to RX RX to TX
LDR ( IN IDE ) #define readpin A0 void setup() { pinmode( readpin, INPUT ); Serial.begin(9600); void loop() { int status = analogread( readpin ); Serial.println(status); delay(100); Export Compiled Binary Run the Simulator
LM35 ( IN PROTEUS ) DIGITAL THERMOMETER LM35
LM35 ( IN IDE ) #define TempPin A0 int TempValue; void setup() { Serial.begin (9600); // Sets the Baud Rate
LM35 ( IN IDE ) void loop() { TempValue = analogread (TempPin); float TempCel = ( TempValue/1024.0)*500; float TempFarh = (TempCel*9)/5 + 32; Serial.print ("TEMPRATURE in Celsius = "); Serial.print ( TempCel ); Serial.print("*C"); Serial.print(" "); Serial.print("TEMPRATURE = "); Serial.print(TempFarh); Serial.print("*F"); Serial.println (); delay(1000); Export Compiled Binary Run the Simulator
GAS SENSOR ( IN PROTEUS ) LOGICTOGGLE
GAS SENSOR ( IN IDE ) void setup() { pinmode(8,input); pinmode(13,output); void loop() { int status = digitalread (8); if(status==high){ digitalwrite(13,high); else{ digitalwrite(13,low); Export Compiled Binary Run the Simulator
ULTRASONIC SENSOR (IN PROTEUS )
ULTRASONIC SENSOR ( IN IDE ) const int triggerpin = 7; const int echopin = 6; void setup() { Serial.begin(9600);
ULTRASONIC SENSOR ( IN IDE ) void loop() { long duration, inches, cm; pinmode( triggerpin, OUTPUT); digitalwrite( triggerpin, LOW); delaymicroseconds (2); digitalwrite( triggerpin, HIGH); delaymicroseconds (10); digitalwrite( triggerpin, LOW); pinmode(echopin, INPUT);
ULTRASONIC SENSOR ( IN IDE ) duration = pulsein (echopin, HIGH); inches = microsecondstoinches (duration); cm = microsecondstocentimeters (duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100);
ULTRASONIC SENSOR ( IN IDE ) long microsecondstoinches(long microseconds) { return microseconds / 74 / 2; long microsecondstocentimeters(long microseconds) { return microseconds / 29 / 2; Export Compiled Binary Run the Simulator
SERVO MOTOR (IN PROTEUS )
SERVO MOTOR ( IN IDE ) #include < Servo.h > Servo myservo; int val; void setup() { pinmode (A0, INPUT); myservo.attach (9); void loop() { val = analogread(a0); val = map(val, 0, 1023, 0, 180); myservo.write ( val ); delay(15); Export Compiled Binary Run the Simulator
BLUETOOTH ( IN IDE ) char c; void setup() { pinmode(13,output); Serial.begin(9600); void loop() { if( Serial.available () ){ c = Serial.read (); switch(c){ case 'a' : digitalwrite(13,high); break; case 'b' : digitalwrite(13,low); break; Export Compiled Binary Run the Simulator
FOR MORE INFO AND HELP GOTO : www.kirantiwari.com.np