Arduino Prof. Dr. Magdy M. Abdelhameed

Similar documents
FUNCTIONS For controlling the Arduino board and performing computations.

keyestudio Keyestudio MEGA 2560 R3 Board

Introduction to Arduino. Wilson Wingston Sharon

More Arduino Programming

Arduino Part 2. Introductory Medical Device Prototyping

Arduino Uno Microcontroller Overview

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

How to Use an Arduino

IME-100 Interdisciplinary Design and Manufacturing

Introduction to Arduino

FUNCTIONS USED IN CODING pinmode()

<Table of content> <Parts and description> <Circuit board Guide> <Pins> <Basics of Coding> <Intro to Arduino> <Intro> <Downloading Arduino IDO>

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

Introduction to Microcontrollers Using Arduino. PhilRobotics

TANGIBLE MEDIA & PHYSICAL COMPUTING INTRODUCTION TO ARDUINO

ARDUINO LEONARDO ETH Code: A000022

PROGRAMMING ARDUINO COURSE ON ADVANCED INTERACTION TECHNIQUES. Luís Carriço FCUL 2012/13

ARDUINO LEONARDO WITH HEADERS Code: A000057

Arduino Uno. Arduino Uno R3 Front. Arduino Uno R2 Front

ARDUINO. By Kiran Tiwari BCT 2072 CoTS.

Introduction to Arduino

Introduction To Arduino

ARDUINO MEGA 2560 REV3 Code: A000067

IME-100 ECE. Lab 3. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

ARDUINO YÚN Code: A000008

ARDUINO UNO REV3 Code: A000066

ARDUINO MICRO WITHOUT HEADERS Code: A000093

TABLE OF CONTENTS INTRODUCTION LESSONS PROJECTS

Arduino ADK Rev.3 Board A000069

Arduino Programming. Arduino UNO & Innoesys Educational Shield

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

EXPERIMENT 7 Please visit to learn all features of arduino before you start the experiments

USER MANUAL ARDUINO I/O EXPANSION SHIELD

IME-100 ECE. Lab 4. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

BASIC ARDUINO WORKSHOP. Mr. Aldwin and Mr. Bernardo

Arduino and Matlab for prototyping and manufacturing

Arduino Programming and Interfacing

ARDUINO MEGA ADK REV3 Code: A000069

ARDUINO PRIMO. Code: A000135

Lecture 7. Processing Development Environment (or PDE)

Chapter 2 The Basic Functions

ARDUINO UNO REV3 SMD Code: A The board everybody gets started with, based on the ATmega328 (SMD).

University of Portland EE 271 Electrical Circuits Laboratory. Experiment: Arduino

ARDUINO YÚN MINI Code: A000108

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

Serial.begin ( ); Serial.println( ); analogread ( ); map ( );

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

CTEC 1802 Embedded Programming Labs

Laboratory 1 Introduction to the Arduino boards

Arduino Uno. Power & Interface. Arduino Part 1. Introductory Medical Device Prototyping. Digital I/O Pins. Reset Button. USB Interface.

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Procedure: Determine the polarity of the LED. Use the following image to help:

4Serial SIK BINDER //77

EEG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

Arduino Uno R3 INTRODUCTION

Robotics/Electronics Review for the Final Exam

Electronic Brick Starter Kit

CN310 Microprocessor Systems Design

Connecting Arduino to Processing

ARDUINO M0 PRO Code: A000111

ArdOS The Arduino Operating System Quick Start Guide and Examples

SX1509 I/O Expander Breakout Hookup Guide

Arduino Platform Part I

The Big Idea: Background: About Serial

TANGIBLE MEDIA & PHYSICAL COMPUTING MORE ARDUINO

analogwrite(); The analogwrite function writes an analog value (PWM wave) to a PWM-enabled pin.

Connecting Arduino to Processing a

Arduino For Amateur Radio

Lab 2 - Powering the Fubarino. Fubarino,, Intro to Serial, Functions and Variables

Review of the syntax and use of Arduino functions, with special attention to the setup and loop functions.

Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package.

ARDUINO INDUSTRIAL 1 01 Code: A000126

Microcontrollers and Interfacing week 8 exercises

Introduction to Microcontrollers

Workshop on Microcontroller Based Project Development

Intro to Arduino. Zero to Prototyping in a Flash! Material designed by Linz Craig and Brian Huang

INDUSTRIAL TRAINING:6 MONTHS PROGRAM TEVATRON TECHNOLOGIES PVT LTD

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

Introduction to Arduino (programming, wiring, and more!)

Prototyping & Engineering Electronics Kits Basic Kit Guide

Alessandra de Vitis. Arduino

Update: Ver 1.3 Dec Arduino Learning Guide For Beginner Using. Created by Cytron Technologies Sdn Bhd - All Rights Reserved

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

IR Breakbeam Sensors. Created by lady ada. Last updated on :32:59 PM UTC

Lab 2 - Powering the Fubarino, Intro to Serial, Functions and Variables

Schedule. Sanford Bernhardt, Sangster, Kumfer, Michalaka. 3:10-5:00 Workshop: Build a speedometer 5:15-7:30 Dinner and Symposium: Group 2

Laboratory 5 Communication Interfaces

Computer Architectures

Goal: We want to build an autonomous vehicle (robot)

Distributed Real- Time Control Systems. Lecture 3 Embedded Systems Interfacing the OuterWorld

Introducting Itsy Bitsy 32u4

The Arduino IDE and coding in C (part 1)

Welcome to Apollo. For more information, please visit the website and select Apollo. Default Code

Arduino Programming Part 4: Flow Control

Seeeduino LoRaWAN. Description

Arduino Workshop. Overview. What is an Arduino? Why Arduino? Setting up your Arduino Environment. Get an Arduino based board and usb cable

SquareWear Programming Reference 1.0 Oct 10, 2012

ARDUINO EXPERIMENTS ARDUINO EXPERIMENTS

Lab 02 Arduino 數位感測訊號處理, SPI I2C 介面實驗. More Arduino Digital Signal Process

Transcription:

Course Code: MDP 454, Course Name:, Second Semester 2014 Arduino

What is Arduino? Microcontroller Platform Okay but what s a Microcontroller? Tiny, self-contained computers in an IC Often contain peripherals Different packages available Vast array of size and power available

Why Arduino? Students, hobbyist, artists & designers open source hardware Open Source Physical Computing Platform open source free to inspect & modify community wiki, forums, tutorials

What can it do? Input Sensors (buttons, light sensors, ultrasound, ) Digital Input (Serial, SPI, I2C) Output LEDs & Lasers LCD/OLED/Touch Displays Speakers Motors, Servos, Steppers Control and Communication Drive other machinery Directly or using a communication protocol

Sensors

Arduino hardware Arduino models differ in design but all share some basic functionality AVR Microcontroller Atmel AVR Mega168 or AVR Mega 328 Powered either directly or via USB power Communications Serial (older models) USB (most models) Connections to a USB interface (smaller models) Pins for various functions

Digital pins Pins 0-13 (GPIO pins) Can output 40 ma Either input our output (default to input)

Digital pins Switching without Pull-up and pull-down resistors

Digital pins Switching with Pull-up and pull-down resistors Pull-down Pull-up

Digital pins What happens if we Digital Read a pin that is not connected? 20K internal pull-up resistor Serial pins 0 (RX) and 1 (TX). Connected to FTDI USB-to-TTL Pin 13 is different (input) (RES + LED) Analogue pins A0-A5 can be used as digital pins 14-19 (GPIO) Read up to 5V. Don t fry your pin!

Pulse Width Modulation (PWM) PWM used to create analogue output on digital pins Simulate voltage between 5V and 0V using square waves (signal switching between on and off) Pins 3, 5, 6, 9, 10, and 11

Analogue pins Pins A0-A5 (signal switching between on and off) 10 bit resolution (from 0-5V to int 0-1023) 100 microseconds (0.0001 s) to read an analog input, maximum reading rate is about 10,000 times a second.

Other pins ICSP (In Circuit Serial Programming) directly program AVR micro-controllers I2C / TWI (Two-wire interface) communication Inter IC control Arduino analog input A4 = I2C SDA (serial data line) Arduino analog input A5 = I2C SCL (serial clock line) External interrupts Event-handling without polling Interrupt 0 (on digital pin 2) and 1 (on digital pin 3) Analogue reference (Set analogue reference for ADC)

Arduino Environment

The Arduino Environment

Board Type

Serial (COM) port

The Environment

Parts of the code

Comments Comments can be anywhere

Comments Comments can be anywhere Comments created with // or /* and */

Comments Comments can be anywhere Comments created with // or /* and */ Comments do not affect code

Comments Comments can be anywhere Comments created with // or /* and */ Comments do not affect code You may not need comments, but think about the community!

Operators The equals sign = is used to assign a value == is used to compare values

Operators And & Or && is and is or

Variables Basic variable types: Boolean Integer Character String

Declaring variables Boolean: boolean variablename;

Declaring variables Boolean: boolean variablename; Integer: int variablename;

Declaring variables Boolean: boolean variablename; Integer: int variablename; Character: char variablename;

Declaring variables Boolean: boolean variablename; Integer: int variablename; Character: char variablename; String: stringname [ ];

Assigning variables Boolean: variablename = true; or variablename = false;

Assigning variables Boolean: variablename = true; or variablename = false; Integer: variablename = 32767; or variablename = -32768;

Assigning variables Boolean: variablename = true; or variablename = false; Integer: variablename = 32767; or variablename = -32768; Character: variablename = A ; or stringname = SparkFun ;

Variable Scope

Setup void setup ( ) { } The setup function comes before the loop function and is necessary for all Arduino sketches

Setup void setup ( ) { } The setup header will never change, everything else that occurs in setup happens inside the curly brackets

Setup Outputs are declare in setup, this is done by using the pinmode function This particular example declares digital pin # 13 as an output, remember to use CAPS

Setup Serial communication also begins in setup This particular example declares Serial communication at a baud rate of 9600. More on Serial later...

Setup You can also create internal pullup resistors in setup, to do so digitalwrite the pin HIGH This takes the place of the pullup resistors currently on your circuit 7 buttons

Setup Setup, Interrupts void setup ( ) { attachinterrupt (interrupt, function, mode) } You can designate an interrupt function to Arduino pins # 2 and 3 This is a way around the linear processing of Arduino

Setup Setup, Interrupts void setup ( ) { attachinterrupt (interrupt, function, mode) } Interrupt: the number of the interrupt, 0 or 1, corresponding to Arduino pins # 2 and 3 respectively Function: the function to call when the interrupt occurs Mode: defines when the interrupt should be triggered

Setup Setup, Interrupts void setup ( ) { attachinterrupt (interrupt, function, mode) } LOW whenever pin state is low CHANGE whenever pin changes value RISING whenever pin goes from low to high FALLING whenever pin goes from low to high Don t forget to CAPITALIZE

If statements if ( this is true ) { do this; }

If statements if ( this is true ) { do this; }

If statements if ( this is true ) { do this; }

If statements if ( this is true ) { do this; }

Else else { do this; }

Basic Repetition loop For while

Basic Repetition void loop ( ) { }

Basic Repetition void loop ( ) { }

Basic Repetition void loop ( ) { } The void in the header is what the function will return (or spit out) when it happens, in this case it returns nothing so it is void

Basic Repetition void loop ( ) { } The loop in the header is what the function is called, sometimes you make the name up, sometimes (like loop) the function already has a name

Basic Repetition void loop ( ) { } The ( ) in the header is where you declare any variables that you are passing (or sending) the function, the loop function is never passed any variables

Basic Repetition void loop ( ) { }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here //this could be anything }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }

Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }

Basic Repetition while ( count<10 ) { //while action code goes here }

Basic Repetition while ( count<10 ) { //while action code goes here //should include a way to change count //variable so the computer is not stuck //inside the while loop forever }

Basic Repetition while ( count<10 ) { //looks basically like a for loop //except the variable is declared before //and incremented inside the while //loop }

Basic Repetition Or maybe: while ( digitalread(buttonpin)==1 ) { //instead of changing a variable //you just read a pin so the computer //exits when you press a button //or a sensor is tripped }

Arduino Programming Structure

Arduino Software Layout Declare variables at top Initialize setup() run once at beginning, set pins Running loop() run repeatedly, after setup()

Arduino Software Layout int ledpin = 13; led connected to control pin 13 Global Variables int asensor = 0; setup sensor 'asensor' on analog pin 0 int statepin = LOW; use this to hold the state of a pin

Arduino Software Layout setup() pinmode() set a pin as input or output serial.begin() setup to `talk' to the computer

Arduino Software Layout setup() pinmode(ledpin, Output); set the pin `ledpin' as an output serial.begin(9600); talk to the computer at 9600 baud rate

Arduino Software Layout loop() digitalwrite() set a digital pin high/low digitalread() read a digital pin s state analogread() read an analog pin analogwrite() write an analog PWM value delay() wait an amount of time millis() get the current tim

Input / Output 14 Digital IO (pins 0-13) 6 Analog In (pins 0-5) 6 Analog Out (pins 3, 5, 6, 9, 10, 11)

Digital? Analog? Digital only has two values: on/off Analog has many (infinite) values Computers don t really do analog - use quantization instead

Digital? Analog? Many states (Analog) or just two (HIGH/LOW - Digital) Number of states (or bins ) is resolution Common computer resolutions: 8-bit = 256 states 16-bit = 65,536 states 32-bit = 4,294,967,296 states

I/O to/from What? Sensors LDR / IR Switch / Potentiometer / Joystick / Piezo Accelerometer Ultrasonic Indicators LED / Lamps Buzzers Actuators Solenoid Stepper Motor Other Circuits Prototype shields Special ICs

Hello World! void setup() { Run Arduino IDE // start serial port at 9600 bps: Serial.begin(9600); } Install latest Arduino IDE from arduino.cc Write the code on the left into the editor Compile / Verify the code by clicking the play button void loop() Before uploading your sketch, check the board and the serial port are correct for your Arduino and for { your computer Serial.print("Hello World!\n\r"); // wait 2sec for next reading: delay(2000); Menu -> Tools -> Board Menu -> Tools -> Serial Port } Upload the code from the computer to the Arduino using the upload button

LED Blink Turn Led on and off according to the byte coming from the serial port, if a zero byte then the Led off else the Led on.

LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } void loop() { while (Serial.available()>0) { inbyte = Serial.read(); } if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the LED on } else { digitalwrite(ledpin, LOW); // sets the LED off } }

LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ Initialise int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } void loop() { while (Serial.available()>0) { inbyte = Serial.read(); } if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the LED on } else { digitalwrite(ledpin, LOW); // sets the LED off } } some of the variables

LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } void loop() { Setup LED pin and while (Serial.available()>0) { inbyte = Serial.read(); serial connection } } if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the LED on } else { digitalwrite(ledpin, LOW); // sets the LED off }

LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } Loop - Reading the void loop() { serial for info, when } while (Serial.available()>0) { inbyte = Serial.read(); something is received } turn if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the } else { digitalwrite(ledpin, LOW); // sets the LED off } theled on LED on

Digital Input/ Output Write Arduino code that uses to turn led on (on pin 13) when the button status is high (on pin 7) and led off when the button status is low; Also send this status via serial port with baud rate 19600.

Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ intledpin = 13; // choose the pin for the LED int inpin = 7; // choose the input pin (button) int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton as input Serial.begin(19200); // start serial communication to computer } void loop() { buttonval = digitalread(inpin); // read the pin and get the button's state if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } }

Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ intledpin = 13; // choose the pin for the LED int inpin = 7; // choose the input pin (button) int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton as input Serial.begin(19200); // start serial communication to computer } void loop() { buttonval = digitalread(inpin); // read the pin and get the button's state if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } } Initialise some of the variables

Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ intledpin = 13; // choose the pin for the LED int inpin = 7; // choose the input pin (button) int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton as input Serial.begin(19200); // start serial communication to computer } void loop() { buttonval = digitalread(inpin); // read the pin and get the button's state if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } } Setup LED pin, switch pin and serial connection

Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ int inpin = 7; // choose the input pin (button) button for info, when intledpin = 13; // choose the pin for the LED int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton asthe LED on and input } Serial.begin(19200); // start serial communication to computer void loop() { } buttonval = digitalread(inpin); // read the pin and get if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } Loop - Reading the button is press turn signal the computer change Of the button's state

Thank You For Your Attention! Questions?