Arduino
Arduino
http://arduino.cc/
Download
version 1.8.5.
Arduino
• Open source hardware i Software platforma
Dijelovi Arduino uno IDE
Tool Bar
Arduino web editor
• https://create.arduino.cc/
• Podržan s Chrome, Firefox, Safari...
• Treba kreirati free Arduino account
• Kada je plugin instaliran dobije se web editor:
Konfiguriranje
• Arduino spojiti s računalom
• Izabrati Select Board or Port
Primjeri
• Examples dobijete kategorije
Odličan za početnike
• DigitalReadSerial
Dodavanje tabova Arduino IDE
Rad s tabovima
• Kreirati tabove led.h i led
• U jednom su definirane konstante u drugom je kod
Manipulacija s dvije varijable
void setup() {
// Samo se jednom izvrši kod boot_anja Arduino_a
var2 = 5; // var2 dobije vrijednost 5 once
}
void loop(){
// Kods e izvršava od gore prema dolje i kontinuirano ponavlja
if (var1 > var2){ // Ako je var1 većas od var2
var2++; // Inkrement var2 za 1
} else { // Ako var1 nije veća od var2
var2 = 0; // var2 postaje 0
}
}
Rad s breadboard (eksperimentalnom pločicom)
Spajanje LED diode
LED blinkanje:
// Deklariranje LED pin_a
int LED = 2;
void setup() {
// Deklariranje LED pina kao Output
pinMode(LED, OUTPUT);
}
void loop(){
// Uključivanje LED i wait 200 millisekundi
digitalWrite(LED, HIGH);
delay(200);
// Isključivanje LED i wait 200 millisekundi
digitalWrite(LED, LOW);
delay(200);
Fading the external LED
// Declare the LED pin with PWM
int LED = 3;
void setup() {
// Declare the pin for the LED as Output
pinMode(LED, OUTPUT)
}
void loop(){
// Here we will fade the LED from 0 to maximum, 255
for (int i = 0; i < 256; i++){
analogWrite(LED, i);
delay(5);
}
// Fade the LED from maximum to 0
for (int i = 255; i >= 0; i--){
analogWrite(LED, i);
delay(5);
}
}
PWM
RGB LED
Program
Declare the PWM LED pins void loop(){
int redLED = 9; // Change a few colors
int greenLED = 10; setColor(255, 0, 0); // Red Color
int blueLED = 11; delay(500);
void setup() { setColor(0, 255, 0); // Green Color
// Declare the pins for the LED as Output delay(500);
pinMode(redLED, OUTPUT); setColor(0, 0, 255); // Blue Color
pinMode(greenLED, OUTPUT); delay(500);
pinMode(blueLED, OUTPUT); setColor(255, 255, 0); // Yellow
} delay(500);
setColor(0, 255, 255); // Cyan
// A simple function to set the level for each color from 0 delay(500);
to 255 setColor(255, 0, 255); // Magenta
void setColor(int redValue, int greenValue, int blueValue) delay(500);
{
setColor(255, 255, 255); // White
analogWrite(redLED, 255 - redValue);
delay(500);
analogWrite(greenLED, 255 - greenValue);
}
analogWrite(blueLED, 255 - blueValue);
}
7-segment display
Program
// Declare the pins for the pinMode(pinUPL, OUTPUT); digitalWrite(pinDWL, LOW); digitalWrite(pinUPR, LOW);
Segment display pinMode(pinCT, OUTPUT); digitalWrite(pinUPL, LOW); digitalWrite(pinCT, LOW);
int pinUP = 2; // Upper segment } } digitalWrite(pinDWR, LOW);
int pinUPR = 3; // Up-right // If we want to write 1 digitalWrite(pinDW, LOW);
segment
void writeNumber(int value){ if (value == 1){ }
int pinDWR = 4; // Down-right
segment // First we erase the previous digitalWrite(pinUPR, LOW); }
value digitalWrite(pinDWR, LOW);
int pinDW = 5; // Down segment
digitalWrite(pinUP, HIGH); }
int pinDWL = 6; // Down-left
segment digitalWrite(pinUPR, HIGH); void loop(){
int pinUPL = 7; // Up-left segment digitalWrite(pinDWR, HIGH); // If we want to write 2 // A resetting count-down
int pinCT = 8; // Center segment digitalWrite(pinDW, HIGH); if (value == 2){ writeNumber(3);
digitalWrite(pinDWL, HIGH); digitalWrite(pinUP, LOW); delay(1000);
void setup() { digitalWrite(pinUPL, HIGH); digitalWrite(pinUPR, LOW); writeNumber(2);
// Declare the pins as Outputs digitalWrite(pinCT, HIGH); digitalWrite(pinCT, LOW); delay(1000);
pinMode(pinUP, OUTPUT); // If we want to write 0 digitalWrite(pinDWL, LOW); writeNumber(1);
pinMode(pinUPR, OUTPUT); if (value == 0){ digitalWrite(pinDW, LOW); delay(1000);
pinMode(pinDWR, OUTPUT); digitalWrite(pinUP, LOW); } writeNumber(0);
pinMode(pinDW, OUTPUT); digitalWrite(pinUPR, LOW); // If we want to write 3 delay(1000);
pinMode(pinDWL, OUTPUT); digitalWrite(pinDWR, LOW); if (value == 3){ }
digitalWrite(pinDW, LOW); digitalWrite(pinUP, LOW);
Button
void loop(){
toggle switch
toggle switch
int buttonPin1 = 2; } digitalWrite(LED1, LOW);
int buttonPin2 = 3; digitalWrite(LED2, LOW);
void loop(){ } else if (buttonValue1 == LOW){
int LED1 = 5; // Read the value of the inputs. It can be // Button is toggled to the second pin
int LED2 = 6; either 0 or 1 digitalWrite(LED1, LOW);
// 0 if toggled in that direction and 1 digitalWrite(LED2, HIGH);
otherwise
void setup() { } else {
int buttonValue1 =
// Define the two LED pins as outputs digitalRead(buttonPin1); // Button is toggled to the third pin
pinMode(LED1, OUTPUT); int buttonValue2 = digitalWrite(LED1, HIGH);
pinMode(LED2, OUTPUT); digitalRead(buttonPin2); digitalWrite(LED2, LOW);
}
// Define the two buttons as inputs with if (buttonValue1 == HIGH && }
the internal pull-up resistor activated buttonValue2 == HIGH){
pinMode(buttonPin1, INPUT_PULLUP); // Switch toggled to the middle. Turn
pinMode(buttonPin2, INPUT_PULLUP); LEDs off
Button to serial
int buttonPin = 2; // Read the value of the input. It can either be
1 or 0.
void setup() { int buttonValue = digitalRead(buttonPin);
// Define pin #2 as input // Send the button value to the serial
connection
pinMode(buttonPin, INPUT_PULLUP);
Serial.println(buttonValue);
void loop(){
Button debouncing
// Declare the pin for the button // Read the value of the input. It can either be 1 or 0
int buttonPin = 2; int buttonValue = digitalRead(buttonPin);
// Variable for keeping the previous button state if (buttonValue != previousButtonValue && millis() -
int previousButtonValue = HIGH; lastDebounce >= debounceTime){
// Reading is useable, print it
long lastDebounce = 0; // Last time the button was pressed Serial.println(buttonValue);
long debounceTime = 50; // Debounce delay
// Reset the debouncing timer
void setup() { lastDebounce = millis();
// Define pin #2 as input and activate the internal pull-up resistor // Change to the latest button state
pinMode(buttonPin, INPUT_PULLUP); previousButtonValue = buttonValue;
// Establish the Serial connection with a baud rate of 115200 }
Serial.begin(115200); // Allow some delay for the Serial data to be transmitted
} delay(10);
}
void loop(){
1,000 buttons to 1 pin
1,000 buttons to 1 pin
// Declare the Analog pin on the Arduino board Serial.println("S3");
int buttonPin = A0; } else if (buttonValue >= 300 && buttonValue < 400){
// A value between 300 - 400 represents the second button
void setup() { Serial.println("S2");
// Establish the Serial connection with a baud rate of 9600 } else if (buttonValue >= 400){
Serial.begin(9600); // A value greater than 400 represents the first button
} Serial.println("S1");
}
void loop(){
// Read the value of the input. It can vary from 0 - 1023
int buttonValue = analogRead(buttonPin); // Delays the execution to allow time for the Serial transmission
if (buttonValue < 200){ delay(25);
// A value under 200 represents no button pushed }
Serial.println("0");
} else if (buttonValue >= 200 && buttonValue < 300){
// A value between 200 - 300 represents the third button
Multipleksiranje
Button multiplexing - 4051
// Define the input pin on the Arduino and the 3 } // Then we read port IO3
selection pins connected to the 4051 digitalWrite(A, HIGH);
int buttonPin = 2; void loop(){ digitalWrite(B, HIGH);
int A = 10; // We first read port IO0 digitalWrite(C, LOW);
int B = 9; digitalWrite(A, LOW); int buttonIO3 = digitalRead(buttonPin);
int C = 8; digitalWrite(B, LOW); // Then we print to Serial the values
digitalWrite(C, LOW); // We print them in-line separated by a space
void setup() { int buttonIO0 = digitalRead(buttonPin); Serial.print(buttonIO0);
// Define pin #2 as input with the pull up // Then we read port IO1 Serial.print(" ");
resistor on
digitalWrite(A, HIGH); Serial.print(buttonIO1);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(B, LOW); Serial.print(" ");
// Define the output pins going to the control
lines of the Multiplexer digitalWrite(C, LOW); Serial.print(buttonIO2);
pinMode(A, OUTPUT); int buttonIO1 = digitalRead(buttonPin); Serial.print(" ");
pinMode(B, OUTPUT); // Then we read port IO2 Serial.println(buttonIO3);
pinMode(C, OUTPUT); digitalWrite(A, LOW); // Delays the execution to allow time for the
serial
// Establish the Serial connection with a baud digitalWrite(B, HIGH);
rate of 9600 digitalWrite(C, LOW); delay(25);
Serial.begin(9600); int buttonIO2 = digitalRead(buttonPin); }
Senzori
Jednostavni senzor – potenciometar
Temperaturni senzor
Detekcija pokreta – PIR senzor
Mjerenje udaljenosti – infracrveni i ultrazvučni
Redukcia šuma
Akcelerometar
Lokalizacija – GPS
Potenciometar
10-bit resolution
Napon između: 0 and 5 V na 5V Arduinu imati će vrijednosti između
0 and 1023.
Napon od 2.5 V će biti jednak 512, što je pola opsega
Potenciometar
int LED = 13; // Deklariranje LED void loop(){
int sensorPin = A2; // Deklariranje analog porta // Čitanje vrijednosti senzora
na koji je spojen senzor int val = analogRead(sensorPin);
// Print te vrijednosti na Serial
void setup(){ Serial.println(val);
// Start Serial konekcije // Blinkanje LED s kašnjenjem četvrtine
Serial.begin(9600); vrijednosti senzora
// Postavljanje LED pina kao OUTPUT digitalWrite(LED, HIGH);
pinMode(LED, OUTPUT); delay(val/4);
} digitalWrite(LED, LOW);
delay(val/4);
}
Senzor temperature – LM35
Senzor temperature – LM35
// Declare the LEDs in an array // On the LM35 each degree Celsius digitalWrite( LED[1], HIGH);
int LED [5] = {2, 3, 4, 5, 6}; equals 10 mV digitalWrite( LED[2], HIGH);
int sensorPin = A0; // 20C is represented by 200 mV which } else if (val > 53 && val < 57){ // 26 - 28
means 0.2 V / 5 V * 1023 = 41 C
// Declare the used sensor pin
// Each degree is represented by an digitalWrite( LED[0], HIGH);
analogue value change of approximately 2
void setup(){ digitalWrite( LED[1], HIGH);
// Set all LEDs off
// Start the Serial connection digitalWrite( LED[2], HIGH);
for (int i = 0; i < 5; i++){
Serial.begin(9600); digitalWrite( LED[3], HIGH);
digitalWrite(LED[i], LOW);
// Set all LEDs as OUTPUTS } else if (val > 57){ // Over 28 C
}
for (int i = 0; i < 5; i++){ digitalWrite( LED[0], HIGH);
pinMode(LED[i], OUTPUT); digitalWrite( LED[1], HIGH);
if (val > 40 && val < 45){ // 20 - 22 C
} digitalWrite( LED[2], HIGH);
digitalWrite( LED[0], HIGH);
} digitalWrite( LED[3], HIGH);
} else if (val > 45 && val < 49){ // 22 - 24
C digitalWrite( LED[4], HIGH);
void loop(){ digitalWrite( LED[0], HIGH); }
// Read the value of the sensor digitalWrite( LED[1], HIGH); delay(100);
int val = analogRead(sensorPin); } else if (val > 49 && val < 53){ // 24 - 26 // Small delay for the Serial to send
Serial.println(val); // Print it to the Serial C }
digitalWrite( LED[0], HIGH);
Detekcija kretanja – PIR senzor (pasivni IR)
PIR senzor
int LED = 13; // Declare the built-in //LED // Approximately 1-2 seconds
int sensorPin = 2; // Declare the used delay(3000); // We are waiting 3
//sensor pin }
void loop(){
Pull-down otpornik s NPN tranzistorom
S PNP tranzistorom
N – KANALNI MOSFET (FQP30N06, IRF510, IRF520)
Skoro svi Arduini imaju 8-bit PWM signal na frekvenciji od 490 Hz.
Izuzetak su novije varijante npr. Uno koji na pinovima 5 i 6, daju 980 Hz.
8-bit PWM znači da izlazna vrijednost može biti od 0 do 255 a sredina
je na 127.
Vrtnja u oba smjera
Polarizacija motora određuje smjer vrtnje.
Za to je potreban H – Bridge ili neki od shieldova.
Kod - vrtnja u oba smjera različitim brzinama
// Deklariranje upotrebljenih pinova digitalWrite(directionPin, HIGH); delay(1000);
int directionPin = 12; // Set speed // Motor full speed forwards
int pwmPin = 3; analogWrite(pwmPin, val); setMotor(255);
} delay(1000);
// If the value is from -255 to 0 the // Motor stop
void setup() { motor will spin backwards setMotor(0);
// Set directionPin kao OUTPUT if (val < 0){ delay(1000);
pinMode(directionPin, OUTPUT); // Set smjera // Motor half speed backwards
// PWM pinovi ne zahtjevaju digitalWrite(directionPin, LOW); setMotor(-127);
pinMode() funkciju // Set speed, -val because the value is delay(1000);
} negative and positive is requried
// Motor full speed backwards
analogWrite(pwmPin, -val);
setMotor(-255);
// Custom function which controls the }
delay(1000);
speed and direction using one variable. }
// Motor stop
void setMotor(int val){
setMotor(0);
// If val is from 0 to 255 the motor will void loop(){
spin forwards delay(1000);
// Turn motor on half speed forwards
if (val >= 0){ }
setMotor(127);
// Set the direction
Arduino UNO R3 – najpopularniji kod maker
(community) zajednice
Arduino UNO R3
• Može se koristiti AC-to-DC adapter ili baterija. Izvor napajanja se može
spojiti s 2,1 mm utikačem s (+) u sredini a vanjski dio je (-).
• Arduino Uno radi na 5 V ali može imati maksimalni ulazni napon 20V
• Ipak se ne preporučuje korištenje većeg napona od 12V
• Koristi linearni regulator (za kontrolu napona spojenog na pločicu.
• USB Port: može se upotrijebiti za napajanje ili programiranje pločice.
• RESET button: Ovaj button, kad se pritisne, resetirati će pločicu.
• ICSP za USB: (In-Circuit Serial Programming) pinovi se upotrebljavaju
flash_anje firmware na USB interface chip.
• Digitalni i PWM konektori: pinovi, označeni s 0 do 13, mogu se koristit
kao digitalni ulazni ili izlazni. Pinovi označeni s tildom (~) mogu se još
koristiti kao Pulse-Width Modulation (PWM) izlaz.
Arduino UNO R3
• Analogni ulazni konektori: Pinovi označeni s A0 do A5 mogu biti
upotrijebljeni kao analogni ulazi. Ovi pinovi mogu se koristiti za čitanje
izlaza iz analognog senzora.
• Power i External Reset (napajanje i vanjski reset): Ovi pinovi daju
masu i napajanje za vanjske sklopove i senzore od Arduina. Arduino se
također može napajati preko tih pinova. Postoji i reset pin koji se
može upotrijebiti za reset Arduina.
• ATmega328: Mikrokontroler za Arduino Uno ploču