Franz Duran INTRODUCTION TO A RDUINO PROGRAMMING & INTERFACING Engr. Franz Duran, MEP-ECE...

76
Franz Duran INTRODUCTION TO ARDUINO PROGRAMMING & INTERFACING Engr. Franz Duran, MEP-ECE RapidSignal Electronics

Transcript of Franz Duran INTRODUCTION TO A RDUINO PROGRAMMING & INTERFACING Engr. Franz Duran, MEP-ECE...

FranzDuran

INTRODUCTION TO ARDUINO PROGRAMMING &

INTERFACING

Engr. Franz Duran, MEP-ECERapidSignal Electronics

FranzDuran

OVERVIEW

Microcontrollers & Robotics

FranzDuran

OVERVIEW

DAY 1 (Morning)o Introduction to Microcontroller Technologyo Introduction to Arduino microcontroller boardo Introduction to RapiDuino Microcontroller

Learning Moduleo Basic Arduino Programming and Interfacing

Interfacing with LEDs, switches, & LCD

Microcontrollers & Robotics

FranzDuran

OVERVIEW

DAY 1 (Afternoon)o Interfacing with 4x4 keypado Interfacing with Analog Sensorso Interfacing with Serial Communication

Microcontrollers & Robotics

FranzDuran

OVERVIEW

DAY 2 (Morning)o Introduction to P-BOT mobile robot traineroMaze mobot programmingo Line follower mobot programming

Microcontrollers & Robotics

FranzDuran

OVERVIEW

DAY 2 (Afternoon)o Line follower mobot raceo Android-controlled mobotoDIY line follower mobot

Microcontrollers & Robotics

FranzDuran

MICROCONTROLLER BASICS

Microcontrollers & Robotics

FranzDuran

What is a microcontroller?

MCUA single-chip computerInvented in the 1970’sUsed as “embedded” controller

Microcontrollers & Robotics

FranzDuran

Where are MCUs used?

used as dedicated controllers in:oHome and office appliancesoConsumer & Personal electronicsoMedical equipmento Industrial equipmentoAutomotive electronicsoNaval/Avionics/Aerospace

Microcontrollers & Robotics

FranzDuran

Why use a microcontroller?

Low-costFlexibleSmall outlineLow-power

Microcontrollers & Robotics

FranzDuran

Why use a microcontroller?

Low-costo Typical price range: P50 to P1000

Flexible Small outline Low-power

Microcontrollers & Robotics

FranzDuran

Why use a microcontroller?

Cheap

FlexibleoRe-programmableoHigh-integration devices

Small outline Low-power

Microcontrollers & Robotics

FranzDuran

Why use a microcontroller?

Cheap Flexible

Small outlineo small physical size = low PCB footprinto tiny packages: DIP, SOP

Low-power

Microcontrollers & Robotics

FranzDuran

Why use a microcontroller?

Cheap Flexible Small outline

Low-powero Battery-operated applicationoUse SLEEP/low-power operation

Microcontrollers & Robotics

FranzDuran

ARDUINO MICROCONTROLLER

Microcontrollers & Robotics

FranzDuran

What is an Arduino?

A microcontroller board + programming IDE

Microcontrollers & Robotics

FranzDuran

What is an Arduino?

A complete MCU boardo ATMEGA microcontrolleroUSB circuito Power supply circuitoReset buttono Female header connectors

Microcontrollers & Robotics

FranzDuran

What is an Arduino?

Easy-to-use Arduino Development environmento Program is called Sketch

Microcontrollers & Robotics

FranzDuran

Arduino ECOSYSTEM

Arduino Boardo Simple, open-hardware design

Arduino Programming IDEoC/C++-based, open-sourceo Built-in functions

Arduino CommunityoDesign Arduino-compatible circuits (Shields)o Software and hardware collaboration

Microcontrollers & Robotics

FranzDuran

RAPIDUINO SWORD3

Microcontrollers & Robotics

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

ARDUINO BOARD

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Arduino Uno gizDuino

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

2x16 Character LCD

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Button Switches

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

4x4 Keypad

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

4x4Keypad

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

7-segment displays

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Light-emitting diodes

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Real-time clock w/ back-up battery

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Serial EEPROM

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

buzzer

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Serial Communication

Circuit

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Bluetooth Transceiver

Module

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Analog voltage sources

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Temperature sensor

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

light-dependent

resistor

FranzDuran

RapiDuino Sword3

Microcontrollers & Robotics

Power Supply Circuit

FranzDuran

BASIC ARDUINO PROGRAMMING & INTERFACING

Microcontrollers & Robotics

FranzDuran

LED Interfacing

Build an LED circuitInterface the LED circuit to the

ArduinoCreate an Arduino program that will

turn on the LED

Microcontrollers & Robotics

FranzDuran

What is an LED?

Light-emitting diodeo An electronic device that emit visible light

when activated.o Typically used as electrical status indicators

Microcontrollers & Robotics

FranzDuran

LED Interfacing

Microcontrollers & Robotics

Circuit schematic

FranzDuran

LED Circuit

Microcontrollers & Robotics

Circuit on breadboard

FranzDuran

LED Circuit on Rapiduino

Connect LED1 to Arduino Pin 7

Microcontrollers & Robotics

FranzDuran

Arduino Environment

Open the Arduino programming environment

Microcontrollers & Robotics

FranzDuran

Writing the Sketch

void setup() {

}

void loop() {

}

Microcontrollers & Robotics

Example #

1

FranzDuran

Writing the Sketch

void setup() { pinMode(7, OUTPUT); //Pin 7 is output pin. digitalWrite(7, HIGH); //LED is on.}

void loop() {

}

Microcontrollers & Robotics

Example #

1

FranzDuran

Writing the Sketch

void setup() { pinMode(7, OUTPUT); //Pin 7 is output pin. digitalWrite(7, HIGH); //LED is on. pinMode(5, OUTPUT); digitalWrite(5, HIGH);}

void loop() {

}Microcontrollers & Robotics

Example #

1

FranzDuran

Writing the Sketch

Click the Verify button

Microcontrollers & Robotics

Verify button

FranzDuran

Steps for Uploading the Program

1. Connect the USB cable

Microcontrollers & Robotics

Note: Make sure USB-to-serial driver is installed first

FranzDuran

Steps for Uploading the Program

2. Power up the Rapiduino board

Microcontrollers & Robotics

FranzDuran

Steps for Uploading the Program

3. Select the COM port number of the Serial Port

Microcontrollers & Robotics

FranzDuran

Steps for Uploading the Program

4. Select the Arduino Duemilanove w/ ATmega328 board

Microcontrollers & Robotics

FranzDuran

Steps for Uploading the Program

5. Press down the Reset button

Microcontrollers & Robotics

FranzDuran

Steps for Uploading the Program

6. While Reset button is pressed, click the Upload button

7. Release the Reset button

Microcontrollers & Robotics

Upload button

FranzDuran

Steps for Uploading the Program

Wait…

… until uploading is done.

Microcontrollers & Robotics

FranzDuran

Steps for experimenting with the Rapiduino

1. Assemble the Arduino application hardwareoUse ordinary solid wires

2. Write the sketch program

3. Upload program to Arduino

Microcontrollers & Robotics

FranzDuran

Arduino Sketchint LED1Pin = 7; //PIN7 is 'renamed' as LED1

void setup() { pinMode(LED1Pin, OUTPUT); digitalWrite(LED1Pin, HIGH); }

void loop() { //nothing... }

Microcontrollers & Robotics

Example #

1

FranzDuran

Arduino Program

Called as Sketcho A simplified combination of C/C++

programming language

Consist of:o setup() functiono loop() function

Microcontrollers & Robotics

FranzDuran

Arduino Program

The setup() functiono The setup() function is called when a sketch

program begins executing. o It is used to initialize variables, configure pin

modes, begin using libraries, etc. o The setup() function will only run once, after

each powerup or reset of the Arduino board.

Microcontrollers & Robotics

FranzDuran

Arduino Program

The loop() functiono The loop() function repeatedly execute all

codes appearing within its body. oUse it to actively control the Arduino board,

i.e. read input, process information, send output

Microcontrollers & Robotics

FranzDuran

Arduino Programvoid setup() { //code1 //code2}

void loop() { //code3 //code4 //code5}

Microcontrollers & Robotics

code1

code5

start

code2

code4

code3

FranzDuran

LED Interfacing

LED Blinker program

Microcontrollers & Robotics

Example #

2ConfigureOutput pin

start

LED is off

Time delay

LED is on

Time delay

LED is off

FranzDuran

Arduino Sketch: LED Blinkerint LED1Pin = 7;

void setup() { pinMode(LED1Pin, OUTPUT); //Pin 7 is output pin digitalWrite(LED1Pin, LOW); //LED is initially low}

void loop() { digitalWrite(LED1Pin, HIGH); //Turn on LED. delay(500); //Time delay for 0.5 second. digitalWrite(LED1Pin, LOW); //Turn off LED. delay(250); //Time delay for 0.25 second.}

Microcontrollers & Robotics

Example #

2

FranzDuran

Button Switch Interfacing

Button switch:o a.k.a pushbutton

or buttono Used as simple input

devices

Common example:o Tact switches

Microcontrollers & Robotics

FranzDuran

Button Switch Interfacing

Microcontrollers & Robotics

FranzDuran

Button Switch Interfacing

Microcontrollers & Robotics

Circuit on breadboard

FranzDuran

Button Switch Interfacing

Program Description:o If button is pressed,

turn on LED.o Else, if button is not

pressed, turn off LED.

Operation:o Button is connected to

Pin 5. o Pin 5 is configured as

input pino If button is pressed,

Pin 5 reads Logic 0; else, if not pressed reads as Logic 1

Microcontrollers & Robotics

FranzDuran

Button Switch Interfacing

Assemble circuit on Rapiduino:o LED1 Pin 7o BUTTON1 Pin 5

Microcontrollers & Robotics

FranzDuran

Arduino Sketch

int LED = 7;int BUTTON = 5;

void setup(){ pinMode(BUTTON, INPUT); pinMode(LED, OUTPUT); digitalWrite(LED, LOW); }

void loop(){ int ButtonStat;

ButtonStat = digitalRead(BUTTON);

if(ButtonStat == LOW) { digitalWrite(LED, HIGH); } else { digitalWrite(LED, LOW); } }

Microcontrollers & Robotics

Example #

3

FranzDuran

2x16 Character LCD Interfacing

Output device for displaying characters

2 rows, 16 characters

Microcontrollers & Robotics

FranzDuran

2x16 Character LCD Interfacing

Microcontrollers & Robotics

Pin number Name Description

1 GND GND

2 +5V +5V

3 VO Contrast Voltage

4 RS Register Select

5 RW Read/Write

6 EN Enable

7 D0 Data Bit 0

8 D1 Data Bit 1

9 D2 Data Bit 2

10 D3 Data Bit 3

11 D4 Data Bit 4

12 D5 Data Bit 5

13 D6 Data Bit 6

14 D7 Data Bit 7

15 LED_AN LED Backlight anode

16 LED_CA LED Backlight cathode

POWERPins

CONTROLPins

DATAPins

BACKLIGHTLED pins

1 16

FranzDuran

2x16 Character LCD Interfacing

Microcontrollers & Robotics

Circuit on breadboard

FranzDuran

2x16 Character LCD Interfacing

Microcontrollers & Robotics

FranzDuran

2x16 Character LCD Interfacing

Assemble circuit on Rapiduino:o LCD_RS Pin 12o LCD_EN Pin 11o LCD_D4 Pin 14o LCD_D5 Pin 15o LCD_D6 Pin 16o LCD_D7 Pin 17 o LCD_RW Ground (NOTE: Short J3 jumper)

Microcontrollers & Robotics

FranzDuran

2x16 Character LCD Interfacing

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 14, 15, 16, 17); //lcd pins

void setup() { lcd.begin(16, 2); //set up the LCD's number of columns and rows: lcd.print("RapiDuino SWORD3"); //Print a text on first line lcd.setCursor(1, 1); //Move cursor to 2nd line lcd.print("with Bluetooth!"); //Print a text on second line}

void loop() { //Do nothing... }

Microcontrollers & Robotics

Example #

4

FranzDuran

END