Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P...
Transcript of Arduino course 2015 - TTÜ Robotiklubi · Introduction Arduino Uno r3 Microcontroller ATmega328P...
![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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/1.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081512/6022048ae1f18d7732651da2/html5/thumbnails/39.jpg)
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;
}