40
INTRODUCTION TO ARDUINO MICROCONTROLLERS By: Mujahid Hussain [email protected]

Introduction to Arduino Microcontroller

Embed Size (px)

Citation preview

Page 1: Introduction to Arduino Microcontroller

INTRODUCTION TO ARDUINOMICROCONTROLLERSBy: Mujahid [email protected]

Page 2: Introduction to Arduino Microcontroller

Start with the name of Allah (SWT), Who is most Merciful and Most Beneficent

Page 3: Introduction to Arduino Microcontroller

WHAT IS ARDUINO

• Arduino is an open-source project that created microcontroller-based kits for building digital devices and interactive objects that can sense and control physical devices.• These systems provide sets of digital

and analog input/output (I/O) pins that can interface to various expansion boards (termed shields) and other circuits. 

Page 4: Introduction to Arduino Microcontroller

WHAT IS MICRO-CONTROLLER

• A microcontroller is basically a small-scale computer with generalized (and programmable) inputs and outputs.• The inputs and outputs can be manipulated by and can

manipulate the physical world.• Programmers work in the virtual world.• Machinery works in the physical world.• How does one connect the virtual world to the physical world?• Simply enter the Microcontroller.

Page 5: Introduction to Arduino Microcontroller

ARDUINO TYPES• Many different versions• Number of input/output channels• Form factor• Processor

• Leonardo• Due• Micro• LilyPad• Esplora• Uno

Page 6: Introduction to Arduino Microcontroller

ARDUINO UNO

• Invented / Launched in 2010• The pins are in three groups:

• 14 digital pins• 6 analog pins• 6 PWM pins• Digital pin 0 & 1 is used for RX TX• 16MHz Clock speed

• 32KB Flash memory• 2KB SRAM• 1KB EEPROM

Page 7: Introduction to Arduino Microcontroller

• Analog Reference pin (orange)• Digital Ground (light green)• Digital Pins 2-13 (green)• Digital Pins 0-1/Serial In/Out - TX/RX (dark green)

- These pins cannot be used for digital i/o (digitalRead and digitalWrite) • Reset Button - S1 (dark blue)• In-circuit Serial Programmer (blue-green)• Analog In Pins 0-5 (light blue)• Power and Ground Pins (power: orange, grounds:

light orange)• External Power Supply In (9-12VDC) - X1 (pink)• Toggles External Power and USB Power (place

jumper on two pins closest to desired supply) - SV1 (purple)• USB (used for uploading sketches to the board

and for serial communication between the board and the computer; can be used to power the board) (yellow)• Microcontrollers

https://www.arduino.cc/en/Reference/Board

Page 8: Introduction to Arduino Microcontroller

THE ARDUINO IDEThe main features you need to know about are:• Code area: This is where you will type all your

code• Info panel: This will show any errors during

compiling or uploading code to your Arduino• Verify: This allows you to compile your code

to code the Arduino understands. Any mistakes you have made in the syntax of your code will be show in the info pannel

• Upload: This does the same as verify but will then send your code to your Arduino if the code is verified successfully

• Serial Monitor: This will open a window that allows you to send text to and from an Arduino. We will use this feature in later lectures.

Page 9: Introduction to Arduino Microcontroller

THE ARDUINO IDE

By far one of the most valuable part of the Arduino software is its vast library of example programs. All features of the Arduino are demonstrated in these.

Optional libraries usually add their own examples on how to use them.

If these examples don’t cover what you need…. Google it!

Page 10: Introduction to Arduino Microcontroller

BEFORE WE BEGIN CODING

Page 11: Introduction to Arduino Microcontroller

STRUCTURE OF AN ARDUINO “SKETCH”

void setup() {   // put your setup code here, to run once:

} void loop() {   // put your main code here, to run repeatedly:   

}

Page 12: Introduction to Arduino Microcontroller

FIRST PROGRAM, SINGLE LED SKETCHint onBoardLED; // Variable Defined

void setup(){   

//Arduinos have an on-board LED on pin 13onBoardLED = 13; pinMode(onBoardLED, OUTPUT);

}

void loop() {

digitalWrite(onBoardLED, HIGH);delay(500); //delay measured in millisecondsdigitalWrite(onBoardLED, LOW);delay(500);

}

Page 13: Introduction to Arduino Microcontroller

LED LIGHT

Page 14: Introduction to Arduino Microcontroller

USER INTERFACE (VISUAL BASIC)

Page 15: Introduction to Arduino Microcontroller

USER INTERFACE CODE (VISUAL BASIC)

Page 16: Introduction to Arduino Microcontroller

ARDUINO PROGRAM (SERIAL COM)

• The baud rate is the rate at which information is transferred in a communication channel. In the serial port context, "9600 baud" means that the serial port is capable of transferring a maximum of 9600 bits per second.

Page 17: Introduction to Arduino Microcontroller

4 LED BLINK SKETCHvoid setup( ) { pinMode(1, OUTPUT); pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(7, OUTPUT);}

void loop( ) { digitalWrite(1, HIGH); delay (200); digitalWrite(1, LOW);

digitalWrite(3, HIGH); delay (200); digitalWrite(3, LOW);

digitalWrite(5, HIGH); delay (200); digitalWrite(5, LOW);

digitalWrite(7, HIGH); delay (200); digitalWrite(7, LOW);}

Page 18: Introduction to Arduino Microcontroller

BREADBOARD

Page 19: Introduction to Arduino Microcontroller

BREADBOARD

Page 20: Introduction to Arduino Microcontroller

STRUCTURE

setup() {• 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 powerup or reset of the Arduino board.

}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.

}

Page 21: Introduction to Arduino Microcontroller

CONTROL STRUCTURES

• if• if...else• for• switch case• while• do... while• break• continue• return• goto

Page 22: Introduction to Arduino Microcontroller

IF

• IF which is used in conjunction with a comparison operator, tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

if (someVariable > 50) {

// do something here }

Page 23: Introduction to Arduino Microcontroller

IF / ELSE

• if/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together.

if (pinFiveInput < 500) { // action A }

else { // action B }

if (pinFiveInput < 500) { // do Thing A }

else if (pinFiveInput >= 1000) { // do Thing B } else

{ // do Thing C }

Page 24: Introduction to Arduino Microcontroller

SWITCH / CASE STATEMENTS:

• Like if statements, switch...case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions.switch (var) {

    case 1:      //do something when var equals 1      break;    case 2:      //do something when var equals 2      break;    default:       // if nothing else matches, do the default      // default is optional    break;  }

Page 25: Introduction to Arduino Microcontroller

WHILE

• while loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.

var = 0;while(var < 200){  // do something repetitive 200 times  var++;}

Page 26: Introduction to Arduino Microcontroller

ARITHMETIC OPERATORS

= (assignment operator)+  (addition)- (subtraction)* (multiplication)/ (division)% (modulo)

Page 27: Introduction to Arduino Microcontroller

COMPARISON OPERATORS

• == (equal to)• != (not equal to)• < (less than)• > (greater than)• <= (less than or equal to)• >= (greater than or equal to)

Page 28: Introduction to Arduino Microcontroller

BOOLEAN OPERATORS

• && (and)• || (or)• ! (not)

Page 29: Introduction to Arduino Microcontroller

COMPOUND OPERATORS

• ++ (increment)• -- (decrement)• += (compound addition)• -= (compound subtraction)• *= (compound multiplication)• /= (compound division)• %= (compound modulo)• &= (compound bitwise and)• |= (compound bitwise or)

Page 30: Introduction to Arduino Microcontroller

VARIABLE - CONSTANTS

• HIGH | LOW (High = On) (Low = Off)• INPUT | OUTPUT (Input = Receive) (Output = Send)• true | false

Page 31: Introduction to Arduino Microcontroller

VARIABLE- DATA TYPES

• void• boolean• char• unsigned char• byte• int• unsigned int• word

• long• unsigned long• short• float• double• string - char array• String - object• array

Page 32: Introduction to Arduino Microcontroller

FUNCTIONS- DIGITAL I/O

•pinMode()Configures the specified pin to behave either as an input or an output.Syntax: pinMode(pin, mode)int ledPin = 13;  // LED connected to digital

pin 13

void setup(){  pinMode(ledPin, OUTPUT);      // sets the digital pin as output}

Page 33: Introduction to Arduino Microcontroller

FUNCTIONS- DIGITAL I/O

•digitalWrite()• Write a HIGH or a LOW value to a digital pin.

Syntax: digitalWrite(pin, value)

void loop(){  digitalWrite(ledPin, HIGH);   // sets the LED on  delay(1000);                  // waits for a second  digitalWrite(ledPin, LOW);    // sets the LED off  delay(1000);                  // waits for a second}

Page 34: Introduction to Arduino Microcontroller

FUNCTIONS- DIGITAL I/O

•pinMode() and •digitalWrite()

int ledPin = 13;  // LED connected to digital pin 13

void setup(){  pinMode(ledPin, OUTPUT);      // sets the digital pin as output}void loop(){  digitalWrite(ledPin, HIGH);   // sets the LED on  delay(1000);                  // waits for a second  digitalWrite(ledPin, LOW);    // sets the LED off  delay(1000);                  // waits for a second}

Page 35: Introduction to Arduino Microcontroller

FUNCTIONS- DIGITAL I/O

•digitalRead()1. Reads the value from

a specified digital pin, either HIGH or LOW.

2. pin: the number of the digital pin you want to read (int)

int ledPin = 13; // LED connected to digital pin 13int inPin = 7;   // pushbutton connected to digital pin 7int val = 0;     // variable to store the read value

void setup(){  pinMode(ledPin, OUTPUT);      // sets the digital pin 13 as output  pinMode(inPin, INPUT);      // sets the digital pin 7 as input}

void loop(){  val = digitalRead(inPin);   // read the input pin  digitalWrite(ledPin, val);    // sets the LED to the button's value}

Page 36: Introduction to Arduino Microcontroller

MATH FUNCTIONS

• min()• max()• abs()• constrain()• map()• pow()• sqrt()

Page 37: Introduction to Arduino Microcontroller

TRIGONOMETRY FUNCTIONS

• sin()• cos()• tan()

Page 38: Introduction to Arduino Microcontroller

CHARACTERS FUNCTIONS

• isAlphaNumeric()• isAlpha()• isAscii()• isWhitespace()• isControl()• isDigit()• isGraph()

• isLowerCase()• isPrintable()• isPunct()• isSpace()• isUpperCase()• isHexadecimalDigit()

Page 39: Introduction to Arduino Microcontroller

MISCELLANEOUS

• Random Numbers• randomSeed()• random()

• Communication• Serial()• Stream()

• setTimeout()

Page 40: Introduction to Arduino Microcontroller

THANKS

• Presented by:Mujahid HussainMCS Student@Preston University Main Campus