corso base arduino completo
TRANSCRIPT
![Page 2: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/2.jpg)
www.roboproject.it
www.veronafablab.it
![Page 3: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/3.jpg)
SOMMARIO1. Installazione Drivers
2. Caratteristiche tecniche Arduino UNO
3. Arduino UNO Board
4. Arduino IDE
5. Blink
6. Codice Colori Resistenze
7. Breadboard
8. Circuiti in Serie e in Parallelo
9. LEDs e corrente
10. Tipi di Dato
11. Variabili locali e globali
12. Operatori
13. IF
14. IF – ELSE
15. Funzioni su PIN digitali
16. Push Button
17. Comunicazione Seriale
18. SWITCH – CASE
19. LED RGB
20. Pulse With Modulation – PWM
21. Funzioni Analogiche
22. Potenziometro
23. Riassumendo…
24. Funzione MAP
25. Ciclo FOR
26. Funzioni
27. Ciclo WHILE
28. Ciclo DO - WHILE
29. LDR – Light Dependent Resistor
30. Librerie
31. Link Utili
![Page 5: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/5.jpg)
Installazione Drivers(Windows)
![Page 6: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/6.jpg)
Installazione Drivers(Windows)
![Page 7: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/7.jpg)
Installazione Drivers(Windows)
![Page 8: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/8.jpg)
Installazione Drivers(Windows)
![Page 9: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/9.jpg)
Installazione Drivers(Windows)
![Page 10: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/10.jpg)
Installazione Drivers(Windows)
![Page 11: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/11.jpg)
Installazione Drivers(Windows)
![Page 12: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/12.jpg)
Caratteristiche tecnicheArduino UNO
Microcontroller ATmega328
Operating Voltage 5V
Input Voltage (recommended)
7-12V
Input Voltage (limit) 6-20V
Digital I/O Pins 14
PWM Digital I/O Pins 6
Analog Input Pins 6
DC Current per I/O Pin 40 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 32 KB
Flash Memory for Bootloader
0.5 KB
SRAM 2 KB
EEPROM 1 KB
Clock Speed 16 MHz
![Page 13: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/13.jpg)
Arduino UNO Board
1
2
345 6
1. Connettore USB (com. seriale)2. Alimentazione jack3. GND4. 5V5. 3.3V6. Vin (alimentazione di ingresso)
![Page 14: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/14.jpg)
Arduino UNO Board7/8
10
9
7. Pin DIGITALI8. Pin DIGITALI con PWM9. Pin ANALOGICI10. Reset11. Main IC - ATmega 328
11
![Page 15: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/15.jpg)
Arduino UNO Board1214
12. Power LED13. RX/TX LEDs14. LED (pin 13)
13
![Page 16: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/16.jpg)
Arduino IDEIntegrated Development Environment
![Page 17: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/17.jpg)
Blink
Ciclo infinito
LED acceso;
pausa di 1 sec;
LED spento;
pausa di 1 sec;
Codice: lezione_1_1_blink
![Page 18: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/18.jpg)
Circuito Blink LED
I LEGGE DI OHMR = Resistenza [Ω][ohm]
V = Tensione [V][Volt]I = Corrente [A][Ampere]
R = V / I
V = I x R
I = V / R
Codice: lezione_1_1_blink
![Page 19: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/19.jpg)
Codice Colori Resistenze
ElectroDroid – Android App
![Page 21: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/21.jpg)
Circuiti in Serie e in Parallelo
SERIE PARALLELO
Due o più componenti sono collegatiin modo da formare un percorsounico per la corrente elettrica che liattraversa.
I componenti sono collegati ad unacoppia di conduttori in modo che latensione elettrica sia applicata a tuttinello stesso modo.
![Page 22: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/22.jpg)
Serie
Req = R1 + R2 + … + Rn
![Page 23: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/23.jpg)
Parallelo
Con 2 resistenze
Req = 𝑅1 ∙ 𝑅2
𝑅1 + 𝑅2
Con n resistenze
Rtot = 1
1
𝑅1+
1
𝑅2+… +
1
𝑅𝑛
![Page 24: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/24.jpg)
LEDs in Serie
NB: Corrente in output per ogni singolo pin I = 40mAÈ sempre bene non superare la soglia dei 30mAIn tot vengono erogati MAX 200mA
Esempio: • 2 LEDs in serie con un
assorbimento di 15mA l’uno15mA*2 = 30mA OK
• 3 LEDs in serie con un assorbimento di 20mA l’uno20mA*3 = 60mA NO!
5V ∆V = 5V – 2,5V
Se ogni LED necessita di 2,5Vcollegando 3 LEDs in serie,questi non si accendono.
∆V = 5V - (2,5V * 3)∆V = -2,5V , ∆V < 0
![Page 25: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/25.jpg)
LEDs in Parallelo
NB:Vale lo stesso discorso della corrente fatto per i LEDs in serie!
5V 5V 5V 5V
![Page 26: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/26.jpg)
Tipi di Dato
TIPO MIN MAX OCCUPAZIONE ESEMPIO
boolean FALSE (0) TRUE (1) 8 bit TRUE
char -128 127 8 bit ‘A’
byte 0 255 8 bit B10010
int -32768 32767 16 bit 250
float -3,4028235E+38 3,4028235E+38 32 bit 1,005
http://arduino.cc/en/Reference/HomePage
1 byte = 8 bit
![Page 27: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/27.jpg)
Variabili locali e globali
GLOBALE
LOCALE
![Page 28: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/28.jpg)
Operatori
OPERATORE SIGNIFICATO ESEMPIO
= Uguale (assegnazione) a = 5
== Uguale (confronto) a == 5
!= Diverso a != 5
&& AND a && b
|| OR a || b
< Minore a < 5
> Maggiore a > 5
<= Minore o uguale a <= 5
>= Maggiore o uguale a >= 5
Vedi la sezione «I Dati del C++» qui
![Page 29: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/29.jpg)
IF (istruzione condizionale)
Utilizzato per eseguire in modo selettivo delle istruzioni
if (condizione)
//istruzioni da svolgere se la condizione è VERA;
![Page 30: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/30.jpg)
IF - ELSE
Utilizzato per eseguire in modo selettivo delle istruzioni
if (pin < 500)// istruzione A;
else// istruzione B;
![Page 31: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/31.jpg)
Funzioni su PIN digitali
FUNZIONE ESEMPIO
pinMode(); pinMode(x, OUTPUT);
digitalWrite(); digitalWrite(x, HIGH);
digitalRead(); digitalRead(x);
La funzione digitalRead(x) può ritornare due valori:• HIGH• LOW
![Page 32: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/32.jpg)
Un esempio pratico: Push Button
Codice: lezione_2_1_button
![Page 33: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/33.jpg)
Il circuito: Push Button
NB: Online si trovano molti esempi con il LED collegato in questomodo. È SBAGLIATO! Sul pin 13 è presente una resistenzainterna ma è collegata al LED on-board non sull’output!
Codice: lezione_2_1_button
![Page 34: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/34.jpg)
Il circuito: Push ButtonCodice: lezione_2_1_button
![Page 35: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/35.jpg)
Push Button con mantenimento ultimo statoCodice: lezione_3_1_buttonState
![Page 36: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/36.jpg)
Comunicazione Seriale
FUNZIONE SIGNIFICATO
Serial.begin(BAUD_RATE); Inizializzazione comunicazione seriale, utilizzata nel void setup()
Serial.print(x); Stampa su monitor seriale (x)
Serial.println(x); Stampa su monitor seriale (x) e va a capo
Serial.available(); Numero di byte disponibili per la lettura dalla porta seriale
Serial.read(); Legge i dati in arrivo sulla porta seriale
![Page 37: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/37.jpg)
SWITCH – CASE (struttura di controllo)
switch (espressione)case x://istruzioni;break;
case y://istruzioni;break;
default://istruzioni;
Utilizzato per sostituire sequenze di IF. Particolarmente utile quando in un programma si deve dare all’utente la possibilità di scegliere tra più opzioni.
![Page 38: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/38.jpg)
SWITCH – CASE (struttura di controllo)
switch(<espressione intera>)case (<valore costante 1>):<sequenza di istruzioni>break;
case (<valore costante 2>)<sequenza di istruzioni>break;
….….
default: // è opzionale<sequenza di istruzioni>
• Valuta il valore dell’espressione intera passata comeparametro all’istruzione switch.
• Rimanda lo svolgimento del programma al blocco incui il parametro dell’istruzione case ha lo stessovalore di quello dell’istruzione switch.
• Se il blocco individuato termina con un’istruzionebreak allora il programma esce dallo switch.
• Altrimenti, vengono eseguiti anche i blocchisuccessivi finchè un’istruzione break non vieneindividuata oppure non si raggiunge l’ultimo bloccodello switch.
• Se nessun blocco corrisponde ad un valore uguale aquello dell’istruzione switch allora viene eseguito ilblocco default, se presente.
![Page 40: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/40.jpg)
Circuito: LED RGB
Codice: lezione_2_2_RGB / _3_2_RGB_Switch
![Page 41: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/41.jpg)
Circuito: LED RGB
Codice: lezione_2_2_RGB / _3_2_RGB_Switch
![Page 44: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/44.jpg)
Funzioni Analogiche
FUNZIONE SIGNIFICATO
analogRead(pin); INPUT - Legge un valore dal pin analogico specificato (0 - 1023)
analogWrite(pin, valore); OUTPUT - Genera una frequenza sui pin digitali PWM (0 - 255)
![Page 45: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/45.jpg)
Potenziometro (o trimmer)
TrasduttoreDispositivo che fornisce in uscita una grandezza elettrica in funzione della
grandezza fisica da rilevare
1. 5V2. GND3. Uscita
![Page 46: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/46.jpg)
Circuito Potenziometro + LED PWM
Codice: lezione_3_3_potPWM
![Page 47: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/47.jpg)
Circuito Potenziometro + LED PWM
Codice: lezione_3_3_potPWM
![Page 48: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/48.jpg)
Riassumendo…
OUTPUT INPUT
digitalWrite(pin, val);Imposta l’uscita digitale ad HIGH o LOW (5V – 0V)
digitalRead(pin);Legge un valore compreso tra 0V e 5V restituendo LOW o HIGH
analogWrite(pin, val);Genera una frequenza (0 - 255) in uscita sul pin digitale PWM specificato
analogRead(pin);Legge un valore dal pin analogico specificato (0 – 1023)
• pinMode();• delay();• Serial: Serial.begin(); Serial.available();
Serial.print(); Serial.println(); Serial.read(); …• map(a, b, c, y, z);
![Page 49: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/49.jpg)
Funzione MAP
int nuova = map(cosa?, min cosa, max cosa, min nuova, max nuova);
Rimappa un numero da un range ad un altro. Utile per esempio quando si ha in ingresso un sensore analogico e si vuole usare
quel valore su un’uscita PWM.
![Page 50: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/50.jpg)
Ciclo FOR (struttura di controllo)
Utilizzato per ripetere n volte un blocco di istruzioni
for (inizializzazione; condizione; incremento)//istruzioni;
![Page 51: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/51.jpg)
Circuito Potenziometro + LEDs
Codice: lezione_4_1_POT_LEDs
![Page 52: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/52.jpg)
Potenziometro + LED RGB + Pulsante
Codice: lezione_4_2_BUTTON_RGB_POT
![Page 53: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/53.jpg)
FUNZIONILe funzioni servono a racchiudere alcune funzionalità del programma (composte dalinee di codice) e a far si che in ogni momento sia possibile richiamare tale funzionalitàda altri punti del programma stesso. In altre parole, si può identificare una serie diistruzioni con un preciso nome: quello di una funzione.
Codice: lezione_5_1_funz
![Page 54: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/54.jpg)
Pot. + LED RGB + 2 Pulsanti + 4 LEDs
Codice: lezione_4_3_ALL_FUNZIONI
![Page 55: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/55.jpg)
WHILE (struttura di controllo)
while (condizione)//istruzioni;
Ciclo che esegue le istruzioni contenute tra le parentesi graffe finche la condizione risulta VERA
Condizione: controllo booleano che viene effettuato ogni volta prima di eseguire le istruzioni.
![Page 56: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/56.jpg)
DO - WHILE (struttura di controllo)
do//istruzioni;
while (condizione);
La differenza fondamentale tra il ciclo do-while e i cicli for e while è che il do-while esegue l’esecuzione del ciclo almeno per una volta.
Condizione: controllo booleano che viene effettuato ogni volta al termine del blocco di istruzioni.
Codice: lezione_5_2_doWhile
![Page 58: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/58.jpg)
LIBRERIEL’ambiente Arduino può essere esteso attraverso l’uso di
librerie. Le librerie includono funzionalità extra per manipolare hardware o dati particolari.
#include <libreria.h>
Necessario per includere nelprogetto SW le funzionalitàdella libreria specificata
![Page 59: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/59.jpg)
Servo Motore
Segnale
V+
GND
Con Arduino il modo più semplice per pilotare dei servo motori è utilizzare la classe Servo
![Page 60: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/60.jpg)
Metodi della classe Servo
METODO COSA FA?
attach() Permette di specificare su quale pin è connesso il nostro servo e legarlo all’oggetto Servo
attached() Controlla che un oggetto di tipo Servo sia collegata ad un pin
detach() Rimuove il collegamento tra l’oggetto Servo e il pin a cui era legata
read() Legge la posizione angolare del nostro servo, restituisce l’ultimo valore passato con write()
write() Impartisce al servo l’angolo a cui posizionarsi, su servo a rotazione continua imposta la velocità di rotazione 0=velocità massima in un senso, 90=fermo, 180=velocià massima nella direzione inversa
writeMicroseconds() Imposta la velocità di rotazione del servo, in un servo standard il valore va da 1000 a 2000, in un servo a rotazione continua si comporta allo stesso modo della write().
![Page 61: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/61.jpg)
LINK UTILI
• Download materiale lezioni
• Arduino Language Reference
• Fritzing
• Lista Shields Arduino
• Plug-in Visual Studio per Arduino
• Microsoft IoT (Internet of Things)
• Microsoft Open Technologies
• NetDuino
• Intel Galileo - BUY
• DFRobot
• SparkFun
• Robot Italy
![Page 62: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/62.jpg)
www.roboproject.it
www.veronafablab.it
![Page 63: Corso base arduino completo](https://reader033.vdocuments.pub/reader033/viewer/2022051300/589faf5e1a28abf9038b4bff/html5/thumbnails/63.jpg)
Ringraziano per la partecipazione
Marco Dalla Pria