arduino course 2015 - ttÜ robotiklubi · introduction arduino uno r3 microcontroller atmega328p...

40
Arduino course 2015

Upload: others

Post on 02-Oct-2020

19 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Arduino course2015

Page 2: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Contents Introduction 1. What is a microcontroller?2. What is Arduino? 3. Different types of Arduinos.

The physical side of Arduino1. Electrical components2. Schematics and circuitry

Coding structure and examples 1. Data types and operators.2. What is a “Function”?3. Control statements [if, if… else, switch case.].4. Loop statements[while, for, do… while.].5. Common functions.

Page 3: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionA microcontroller is the brain of an electronic device

Page 4: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionThe internals

Page 5: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionThe internals

Page 6: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionThe internals

Page 7: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionKnown manufacturers

• Atmel

• Analog Devices

• Dallas Semiconductor

• Intel

• Microchip Technology

• National Semiconductor

• STMicroelectronics

• Texas Instruments

• Xilinx

Page 8: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionKnown manufacturers

• Atmel– ATtiny

– ATmega

– ATxmega

Page 9: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionKnown manufacturers

• Atmel– ATtiny

– ATmega

– ATxmega

Page 10: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Introduction• ATmega 328

Page 11: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionArduino Uno r3

Microcontroller ATmega328P

Operating Voltage 5V

Input Voltage (recommended)

7-12V

Input Voltage (limit) 6-20V

Digital I/O Pins14 (of which 6 provide PWM output)

PWM Digital I/O Pins 6

Analog Input Pins 6

DC Current per I/O Pin 20 mA

DC Current for 3.3V Pin 50 mA

Flash Memory32 KB (ATmega328P)of which 0.5 KB used by bootloader

SRAM 2 KB (ATmega328P)

EEPROM 1 KB (ATmega328P)

Clock Speed 16 MHz

Page 12: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

IntroductionArduino Uno r3

Page 13: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

The physical side of Arduino

Digital or Analog?

• All physical quantities are analog.• Analog means that the quantity can take any value

between its minimum value and maximum value.• Digital means that the quantity can take specific levels of

values with specific offset between each other.

• Ex: 1- Digital: • English alpha consists of 26 letter, there is no letter

between A and B.• - Square waves are Digital.• Ex.: 2- Analog: • Temperature, can take any value[-1,12.8,25.002,… etc.].• - Sine waves are analog.

Page 14: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

The physical side of Arduino

Page 15: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Electroniccomponents

Passives

Resistors

Page 16: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Electroniccomponents

Passives

Resistors

Page 17: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Electroniccomponents

Passives

Capacitors

Page 18: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Electroniccomponents

Passives

Inductors

Page 19: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Electroniccomponents

Actives

Diodes

Page 20: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Electroniccomponents

Actives

Transistors

Page 21: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

SchematicsWhat you will find on an Arduino board

Page 22: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Coding

Page 23: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

CodingWorkflow:• Open the IDE.

• Write code and logic.

• Click the verify/compile button to check your program for errors.

• Connect the Arduino via USB to the PC.

• Install drivers (on the first time).

• Setup serial port that’s being used.

• Setup the board which we need to program.

• Click „upload“ to send the code to arduino.

Page 24: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Data Types and

operators

• Integer: used with integer variables with value between 2147483647 and -2147483647.

• Ex: int x=1200;• Character: used with single character, represent value

from -127 to 128.• Ex. char c=‘r’;• Long: Long variables are extended size variables for

number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.

• Ex. long u=199203;• Floating-point numbers can be as large as

3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.

• Ex. float num=1.291; [The same as double type]

Page 25: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Statements and

operators

• Statement represents a command, it ends with ;

• Ex:

• int x;

• x=13;

• Operators are symbols that used to indicate a specific function:

• - Math operators: [+,-,*,/,%,^]

• - Logic operators: [==, !=, &&, ||]

• - Comparison operators: [==, >, <, !=, <=, >=]

• Syntax:

• ; Semicolon, {} curly braces, //single line comment, /*Multi-line comments*/

Page 26: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Statements and

operators

• Compound Operators:

• ++ (increment)

• -- (decrement)

• += (compound addition)

• -= (compound subtraction)

• *= (compound multiplication)

• /= (compound division)

Page 27: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Control statements

• If Conditioning:

• if(condition)

• {

• statements-1;

• …

• Statement-N;

• }

• else if(condition2)

• {

• Statements;

• }

• Else{statements;}

Page 28: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Control statements

• Switch case:

• 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

• }

Page 29: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Loop statements

• Do… while:• do• {• Statements;• }• while(condition); // the statements are run at least

once.

• While: • While(condition)• {statements;}• for• for (int i=0; i <= val; i++){• statements;• }

Page 30: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Code structure

• Void setup(){}

• Used to indicate the initial values of system on starting.

• Void loop(){}

• Contains the statements that will run whenever the system is powered after setup.

Page 31: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Input and output

• Led blinking example:

• Used functions:

• pinMode();

• digitalRead();

• digitalWrite();

• delay(time_ms);

• other functions:

• analogRead();

• analogWrite();//PWM.

Page 32: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

To sum it upFirst, tell Arduino what it is:• Declare pins as input or output• Set global variables

Then tell it what to do:• Read inputs• Do calculations• Set outputs This is done continuously, in a loop

Page 33: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

The program (blinking the

LED)

* Configuration:

First, tell Arduino what it is */

void setup() {

// initialize the digital pin as an output.

// Pin 13 has an LED connected on most Arduino boards:

pinMode(13, OUTPUT);

}

/* Then loop:

Tell Arduino what to do */

void loop() {

digitalWrite(13, HIGH); // set the LED on

delay(1000); // wait for a second

digitalWrite(13, LOW); // set the LED off

delay(1000); // wait for a second

}

Page 34: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Compiling and

uploading the code

• Type your code into the text window

• Push the ‘upload’ button

• Check if the TX and RX LEDs are blinking rapidly

• If the ‘Done uploading’ message displays Arduino is ready

Page 35: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

The program (blinking the

LED)

* Configuration:

First, tell Arduino what it is */

void setup() {

// initialize the digital pin as an output.

// Pin 13 has an LED connected on most Arduino boards:

pinMode(13, OUTPUT);

}

/* Then loop:

Tell Arduino what to do */

void loop() {

digitalWrite(13, HIGH); // set the LED on

delay(1000); // wait for a second

digitalWrite(13, LOW); // set the LED off

delay(1000); // wait for a second

}

Page 36: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Blinking the LED with a

button• As long as you hold on the button the LED is lit up

• When you hold the button the LED is flashing

• After pressing the button the LED starts flashing and stops when you press the button again

• When you press the button the frequency at which the LED is blinking will change– The LED is constantly blinking

– The frequencies are 2, 4 and 8 times per second

– Each time you press the button the frequency changes between those values

Page 37: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Reading data via the Serial

interface

void setup()

{

Serial.begin(9600);

}

void serialtest()

{

int i;

for(i=0; i<10; i++)

Serial.println(i);

}

Page 38: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Interrupts• On a standard Arduino board, two pins can be used as interrupts: pins 2 and 3.

• The interrupt is enabled through the following line:

attachInterrupt(interrupt pin, function, mode)

• Modes:

– LOW

– CHANGE

– RISING

– FALLING

Page 39: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

Interrupt example

int led = 13;

volatile int state = LOW;

void setup()

{

pinMode(led, OUTPUT);

attachInterrupt(1, blink, CHANGE);

}

void loop()

{

digitalWrite(led, state);

}

void blink()

{

state = !state;

}

Page 40: Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P Operating Voltage 5V Input Voltage (recommended) 7-12V Input Voltage (limit) 6-20V

e-mail: [email protected]

tel. +372 53408660