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

Post on 02-Oct-2020

19 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Arduino course2015

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.

IntroductionA microcontroller is the brain of an electronic device

IntroductionThe internals

IntroductionThe internals

IntroductionThe internals

IntroductionKnown manufacturers

• Atmel

• Analog Devices

• Dallas Semiconductor

• Intel

• Microchip Technology

• National Semiconductor

• STMicroelectronics

• Texas Instruments

• Xilinx

IntroductionKnown manufacturers

• Atmel– ATtiny

– ATmega

– ATxmega

IntroductionKnown manufacturers

• Atmel– ATtiny

– ATmega

– ATxmega

Introduction• ATmega 328

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

IntroductionArduino Uno r3

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.

The physical side of Arduino

Electroniccomponents

Passives

Resistors

Electroniccomponents

Passives

Resistors

Electroniccomponents

Passives

Capacitors

Electroniccomponents

Passives

Inductors

Electroniccomponents

Actives

Diodes

Electroniccomponents

Actives

Transistors

SchematicsWhat you will find on an Arduino board

Coding

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.

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]

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*/

Statements and

operators

• Compound Operators:

• ++ (increment)

• -- (decrement)

• += (compound addition)

• -= (compound subtraction)

• *= (compound multiplication)

• /= (compound division)

Control statements

• If Conditioning:

• if(condition)

• {

• statements-1;

• …

• Statement-N;

• }

• else if(condition2)

• {

• Statements;

• }

• Else{statements;}

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

• }

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;• }

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.

Input and output

• Led blinking example:

• Used functions:

• pinMode();

• digitalRead();

• digitalWrite();

• delay(time_ms);

• other functions:

• analogRead();

• analogWrite();//PWM.

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

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

}

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

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

}

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

Reading data via the Serial

interface

void setup()

{

Serial.begin(9600);

}

void serialtest()

{

int i;

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

Serial.println(i);

}

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

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;

}

e-mail: robotiklubi@robotiklubi.ee

tel. +372 53408660

top related