INTRODUCTION TO ARDUINO PROGRAMMING & INTERFACING Engr. Franz Duran, MEP-ECE RapidSignal Electronics Franz Duran OVERVIEW Microcontrollers & Robotics Franz Duran OVERVIEW DAY 1 (Morning) o Introduction to Microcontroller Technology o Introduction to Arduino microcontroller board o Introduction to RapiDuino Microcontroller Learning Module o Basic Arduino Programming and Interfacing Interfacing with LEDs, switches, & LCD Microcontrollers & Robotics Franz Duran OVERVIEW DAY 1 (Afternoon) o Interfacing with 4x4 keypad o Interfacing with Analog Sensors o Interfacing with Serial Communication Microcontrollers & Robotics Franz Duran OVERVIEW DAY 2 (Morning) o Introduction to P-BOT mobile robot trainer o Maze mobot programming o Line follower mobot programming Microcontrollers & Robotics Franz Duran OVERVIEW DAY 2 (Afternoon) o Line follower mobot race o Android-controlled mobot o DIY line follower mobot Microcontrollers & Robotics Franz Duran MICROCONTROLLER BASICS Microcontrollers & Robotics Franz Duran What is a microcontroller? MCU A single-chip computer Invented in the 1970’s Used as “embedded” controller Microcontrollers & Robotics Franz Duran Where are MCUs used? used as dedicated controllers in: o Home and office appliances o Consumer & Personal electronics o Medical equipment o Industrial equipment o Automotive electronics o Naval/Avionics/Aerospace Microcontrollers & Robotics Franz Duran Why use a microcontroller? Low-cost Flexible Small outline Low-power Microcontrollers & Robotics Franz Duran Why use a microcontroller? Low-cost o Typical price range: P50 to P1000 Flexible Small outline Low-power Microcontrollers & Robotics Franz Duran Why use a microcontroller? Cheap Flexible o Re-programmable o High-integration devices Small outline Low-power Microcontrollers & Robotics Franz Duran Why use a microcontroller? Cheap Flexible Small outline o small physical size = low PCB footprint o tiny packages: DIP, SOP Low-power Microcontrollers & Robotics Franz Duran Why use a microcontroller? Cheap Flexible Small outline Low-power o Battery-operated application o Use SLEEP/low-power operation Microcontrollers & Robotics Franz Duran ARDUINO MICROCONTROLLER Microcontrollers & Robotics Franz Duran What is an Arduino? A microcontroller board + programming IDE Microcontrollers & Robotics Franz Duran What is an Arduino? A complete MCU board o ATMEGA microcontroller o USB circuit o Power supply circuit o Reset button o Female header connectors Microcontrollers & Robotics Franz Duran What is an Arduino? Easy-to-use Arduino Development environment o Program is called Sketch Microcontrollers & Robotics Franz Duran Arduino ECOSYSTEM Arduino Board o Simple, open-hardware design Arduino Programming IDE o C/C++-based, open-source o Built-in functions Arduino Community o Design Arduino-compatible circuits (Shields) o Software and hardware collaboration Microcontrollers & Robotics Franz Duran RAPIDUINO SWORD3 Microcontrollers & Robotics Franz Duran RapiDuino Sword3 ARDUINO BOARD Microcontrollers & Robotics Franz Duran RapiDuino Sword3 gizDuino Arduino Uno Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Microcontrollers & Robotics 2x16 Character LCD Franz Duran RapiDuino Sword3 Button Switches Microcontrollers & Robotics Franz Duran RapiDuino Sword3 4x4 Keypad Microcontrollers & Robotics Franz Duran RapiDuino Sword3 4x4 Keypad Microcontrollers & Robotics Franz Duran RapiDuino Sword3 7-segment displays Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Lightemitting diodes Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Real-time clock w/ back-up battery Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Serial EEPROM Microcontrollers & Robotics Franz Duran RapiDuino Sword3 buzzer Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Serial Communication Circuit Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Bluetooth Transceiver Module Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Analog voltage sources Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Temperature sensor Microcontrollers & Robotics Franz Duran RapiDuino Sword3 lightdependent resistor Microcontrollers & Robotics Franz Duran RapiDuino Sword3 Power Supply Circuit Microcontrollers & Robotics Franz Duran BASIC ARDUINO PROGRAMMING & INTERFACING Microcontrollers & Robotics Franz Duran LED Interfacing Build an LED circuit Interface the LED circuit to the Arduino Create an Arduino program that will turn on the LED Microcontrollers & Robotics Franz Duran What is an LED? Light-emitting diode o An electronic device that emit visible light when activated. o Typically used as electrical status indicators Microcontrollers & Robotics Franz Duran LED Interfacing Circuit schematic Microcontrollers & Robotics Franz Duran LED Circuit Circuit on breadboard Microcontrollers & Robotics Franz Duran LED Circuit on Rapiduino Connect LED1 to Arduino Pin 7 Microcontrollers & Robotics Franz Duran Arduino Environment Open the Arduino programming environment Microcontrollers & Robotics Franz Duran Writing the Sketch void setup() { } void loop() { } Microcontrollers & Robotics Franz Duran Writing the Sketch void setup() { pinMode(7, OUTPUT); digitalWrite(7, HIGH); } //Pin 7 is output pin. //LED is on. void loop() { } Microcontrollers & Robotics Franz Duran Writing the Sketch void setup() { pinMode(7, OUTPUT); digitalWrite(7, HIGH); pinMode(5, OUTPUT); digitalWrite(5, HIGH); } //Pin 7 is output pin. //LED is on. void loop() { } Microcontrollers & Robotics Franz Duran Writing the Sketch Click the Verify button Verify button Microcontrollers & Robotics Franz Duran Steps for Uploading the Program 1. Connect the USB cable Note: Make sure USB-to-serial driver is installed first Microcontrollers & Robotics Franz Duran Steps for Uploading the Program 2. Power up the Rapiduino board Microcontrollers & Robotics Franz Duran Steps for Uploading the Program 3. Select the COM port number of the Serial Port Microcontrollers & Robotics Franz Duran Steps for Uploading the Program 4. Select the Arduino Duemilanove w/ ATmega328 board Microcontrollers & Robotics Franz Duran Steps for Uploading the Program 5. Press down the Reset button Microcontrollers & Robotics Franz Duran Steps for Uploading the Program 6. While Reset button is pressed, click the Upload button Upload button 7. Release the Reset button Microcontrollers & Robotics Franz Duran Steps for Uploading the Program Wait… … until uploading is done. Microcontrollers & Robotics Franz Duran Steps for experimenting with the Rapiduino 1. Assemble the Arduino application hardware o Use ordinary solid wires 2. Write the sketch program 3. Upload program to Arduino Microcontrollers & Robotics Franz Duran Arduino Sketch int LED1Pin = 7; //PIN7 is 'renamed' as LED1 void setup() { pinMode(LED1Pin, OUTPUT); digitalWrite(LED1Pin, HIGH); } void loop() { //nothing... } Microcontrollers & Robotics Franz Duran Arduino Program Called as Sketch o A simplified combination of C/C++ programming language Consist of: o setup() function o loop() function Microcontrollers & Robotics Franz Duran Arduino Program The setup() function o 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 Franz Duran Arduino Program The loop() function o The loop() function repeatedly execute all codes appearing within its body. o Use it to actively control the Arduino board, i.e. read input, process information, send output Microcontrollers & Robotics Franz Duran Arduino Program void setup() { //code1 //code2 } start code1 code2 code3 void loop() { //code3 //code4 //code5 } code4 code5 Microcontrollers & Robotics Franz Duran LED Interfacing start LED Blinker program Configure Output pin LED is off LED is on Time delay LED is off Time delay Microcontrollers & Robotics Franz Duran Arduino Sketch: LED Blinker int LED1Pin = 7; void setup() { pinMode(LED1Pin, OUTPUT); digitalWrite(LED1Pin, LOW); } //Pin 7 is output pin //LED is initially low void loop() { digitalWrite(LED1Pin, HIGH); delay(500); //Turn on LED. //Time delay for 0.5 second. digitalWrite(LED1Pin, LOW); delay(250); //Turn off LED. //Time delay for 0.25 second. } Microcontrollers & Robotics Franz Duran Button Switch Interfacing Button switch: o a.k.a pushbutton or button o Used as simple input devices Common example: o Tact switches Microcontrollers & Robotics Franz Duran Button Switch Interfacing Microcontrollers & Robotics Franz Duran Button Switch Interfacing Circuit on breadboard Microcontrollers & Robotics Franz Duran 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 pin o If button is pressed, Pin 5 reads Logic 0; else, if not pressed reads as Logic 1 Microcontrollers & Robotics Franz Duran Button Switch Interfacing Assemble circuit on Rapiduino: o LED1 Pin 7 o BUTTON1 Pin 5 Microcontrollers & Robotics Franz Duran Arduino Sketch void loop() { int ButtonStat; int LED = 7; int BUTTON = 5; ButtonStat = digitalRead(BUTTON); void setup() { pinMode(BUTTON, INPUT); if(ButtonStat == LOW) { digitalWrite(LED, HIGH); } else { digitalWrite(LED, LOW); } pinMode(LED, OUTPUT); digitalWrite(LED, LOW); } } Microcontrollers & Robotics Franz Duran 2x16 Character LCD Interfacing Output device for displaying characters 2 rows, 16 characters Microcontrollers & Robotics Franz Duran 2x16 Character LCD Interfacing 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 16 LED_CA POWER Pins CONTROL Pins 1 16 DATA Pins BACKLIGHT LED pins LED Backlight cathode Microcontrollers & Robotics LED Backlight anode Franz Duran 2x16 Character LCD Interfacing Circuit on breadboard Microcontrollers & Robotics Franz Duran 2x16 Character LCD Interfacing Microcontrollers & Robotics Franz Duran 2x16 Character LCD Interfacing Assemble circuit on Rapiduino: o LCD_RS Pin 12 o LCD_EN Pin 11 o LCD_D4 Pin 14 o LCD_D5 Pin 15 o LCD_D6 Pin 16 o LCD_D7 Pin 17 o LCD_RW Ground (NOTE: Short J3 jumper) Microcontrollers & Robotics Franz Duran 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); lcd.print("with Bluetooth!"); //Move cursor to 2nd line //Print a text on second line } void loop() { //Do nothing... } Microcontrollers & Robotics Franz Duran END Franz Duran