Cours Arduino Complet
Microcontroleurs et Systemes Embarques
Chapitre 1 : Introduction a Arduino
Decouverte de la plateforme Arduino
1.1 Qu'est-ce qu'Arduino ?
Historique
- 2005 : Creation a Ivrea, Italie par Massimo Banzi et David Cuartielles
- 2006 : Prix Ars Electronica
- 2010 : Plus de 120 000 cartes vendues
- Aujourd'hui : Des millions d'utilisateurs dans le monde
Pourquoi Arduino ?
Economique
Cartes accessibles des 5 euros
Open Source
Schemas et code libres
Facile
Ideal pour debuter
Communaute
Millions d'utilisateurs
Extensible
Shields et modules
Multiplateforme
Windows, Mac, Linux
1.2 Les Differentes Cartes Arduino
| Carte | Microcontroleur | Digital I/O | Analog In | Flash | SRAM |
|---|---|---|---|---|---|
| Arduino Uno | ATmega328P | 14 | 6 | 32 KB | 2 KB |
| Arduino Mega | ATmega2560 | 54 | 16 | 256 KB | 8 KB |
| Arduino Nano | ATmega328P | 14 | 8 | 32 KB | 2 KB |
| Arduino Leonardo | ATmega32U4 | 20 | 12 | 32 KB | 2.5 KB |
| Arduino Due | AT91SAM3X8E | 54 | 12 | 512 KB | 96 KB |
Chapitre 2 : Architecture Arduino Uno
Structure interne et composants
2.1 Le Microcontroleur ATmega328P
Le coeur de l'Arduino Uno est le microcontroleur ATmega328P, un composant 8 bits de la famille AVR fabrique par Microchip (anciennement Atmel).
Caracteristiques Techniques
Frequence
16 MHz (horloge externe)
Memoire Flash
32 KB (0.5 KB bootloader)
SRAM
2 KB
EEPROM
1 KB
Architecture Interne
┌─────────────────────────────────────────┐ │ ATmega328P │ │ ┌─────────┐ ┌─────────┐ │ │ │ CPU │◄──►│ Flash │ │ │ │ 8-bit │ │ 32KB │ │ │ └────┬────┘ └─────────┘ │ │ │ ┌─────────┐ │ │ ├────────►│ SRAM │ │ │ │ │ 2KB │ │ │ │ └─────────┘ │ │ │ ┌─────────┐ │ │ ├────────►│ EEPROM │ │ │ │ │ 1KB │ │ │ ┌────┴────┐ └─────────┘ │ │ │ I/O │◄──► GPIO, ADC, PWM, UART │ │ │ Ports │ SPI, I2C, Timers │ │ └─────────┘ │ └─────────────────────────────────────────┘
2.2 Schema de la Carte Arduino Uno
ARDUINO UNO R3
┌─────────────────────────────────────────┐
│ ┌───┐ DIGITAL │
│ │USB│ ┌─────────────┐ PWM~ (pins) │
│ └───┘ │ │ 13 ──── LED │
│ │ ATmega328P │ 12 │
│ POWER │ │ ~11 │
│ Vin │ │ ~10 │
│ GND │ │ ~9 │
│ GND └─────────────┘ ~8 │
│ 5V 7 │
│ 3.3V ┌───────┐ ~6 │
│ RESET │ RESET │ ~5 │
│ └───────┘ 4 │
│ ANALOG IN ~3 (INT1) │
│ A0 2 (INT0) │
│ A1 TX 1 │
│ A2 RX 0 │
│ A3 │
│ A4 (SDA) ┌──────────────────┐ │
│ A5 (SCL) │ ICSP Header │ │
│ └──────────────────┘ │
└─────────────────────────────────────────┘
Broches Importantes
- Pin 0-1 : RX/TX pour communication serie
- Pin 2-3 : Interruptions externes (INT0, INT1)
- Pin 3,5,6,9,10,11 : Sorties PWM (~)
- Pin 10-13 : Bus SPI (SS, MOSI, MISO, SCK)
- Pin A4-A5 : Bus I2C (SDA, SCL)
- Pin 13 : LED integree
Chapitre 3 : L'IDE Arduino
Environnement de developpement
3.1 Installation et Configuration
Telechargement
- Aller sur
arduino.cc/en/software - Telecharger la version pour votre systeme (Windows, Mac, Linux)
- Installer en suivant les instructions
- Les drivers USB s'installent automatiquement
Interface de l'IDE
┌─────────────────────────────────────────────────┐
│ Fichier Edition Croquis Outils Aide │
├─────────────────────────────────────────────────┤
│ [✓] Verifier [→] Telecharger │
├─────────────────────────────────────────────────┤
│ │
│ void setup() { │
│ // Code d'initialisation │
│ } │
│ │
│ void loop() { │
│ // Code principal (boucle infinie) │
│ } │
│ │
├─────────────────────────────────────────────────┤
│ Console : Messages de compilation │
└─────────────────────────────────────────────────┘
3.2 Structure d'un Programme Arduino
Un programme Arduino (appele "sketch") a une structure specifique avec deux fonctions obligatoires :
// Declarations globales (variables, constantes) const int LED = 13; // Fonction d'initialisation (executee une seule fois) void setup() { pinMode(LED, OUTPUT); // Configure la pin en sortie Serial.begin(9600); // Initialise la communication serie } // Boucle principale (executee en continu) void loop() { digitalWrite(LED, HIGH); // Allume la LED delay(1000); // Attend 1 seconde digitalWrite(LED, LOW); // Eteint la LED delay(1000); // Attend 1 seconde }
setup() et loop() sont obligatoires. Le compilateur genere une erreur si elles sont absentes.
3.3 Types de Donnees
| Type | Taille | Plage | Exemple |
|---|---|---|---|
boolean |
1 bit | true / false | boolean etat = true; |
byte |
8 bits | 0 a 255 | byte val = 200; |
int |
16 bits | -32768 a 32767 | int compteur = 1000; |
unsigned int |
16 bits | 0 a 65535 | unsigned int pos = 50000; |
long |
32 bits | -2^31 a 2^31-1 | long temps = 1000000; |
float |
32 bits | +/- 3.4028235E+38 | float temp = 25.5; |
char |
8 bits | -128 a 127 | char lettre = 'A'; |
Chapitre 4 : Entrees/Sorties Numeriques (GPIO)
Gestion des signaux digitaux
4.1 Configuration des Broches
Les broches digitales peuvent etre configurees en entree ou en sortie avec la fonction pinMode().
void setup() { pinMode(13, OUTPUT); // Pin 13 en sortie pinMode(2, INPUT); // Pin 2 en entree pinMode(3, INPUT_PULLUP); // Pin 3 en entree avec resistance pull-up }
Modes Disponibles
OUTPUT: Configure la broche en sortie (peut fournir ou absorber du courant)INPUT: Configure la broche en entree (haute impedance)INPUT_PULLUP: Entree avec resistance pull-up interne (~20-50 kOhm)
4.2 Lecture et Ecriture
Ecriture Numerique
digitalWrite(pin, HIGH); // Met la pin a 5V (ou 3.3V) digitalWrite(pin, LOW); // Met la pin a 0V (masse)
Lecture Numerique
int valeur = digitalRead(pin); // Retourne HIGH (1) ou LOW (0)
Exemple : LED avec Bouton
const int BOUTON = 2; const int LED = 13; void setup() { pinMode(BOUTON, INPUT_PULLUP); pinMode(LED, OUTPUT); } void loop() { if (digitalRead(BOUTON) == LOW) { // Bouton appuye (actif bas) digitalWrite(LED, HIGH); // Allume LED } else { digitalWrite(LED, LOW); // Eteint LED } }
Chapitre 5 : Entrees Analogiques
Conversion analogique-numerique (ADC)
5.1 Le Convertisseur ADC
Caracteristiques de l'ADC Arduino Uno
- Resolution : 10 bits (valeurs de 0 a 1023)
- Tension de reference : 5V par defaut
- Precision : 5V / 1024 = 4.88 mV par pas
- Entrees : A0 a A5 (6 canaux)
Formule de Conversion
Valeur ADC = (Tension d'entree / Tension de reference) x 1023
Exemple : Pour 2.5V avec Vref = 5V
Valeur ADC = (2.5 / 5) x 1023 = 511
5.2 Lecture Analogique
int valeur = analogRead(A0); // Lit la valeur sur A0 (0-1023) // Conversion en tension float tension = valeur * (5.0 / 1023.0);
Exemple : Lecture d'un Potentiometre
const int POT = A0; void setup() { Serial.begin(9600); } void loop() { int valeur = analogRead(POT); float tension = valeur * (5.0 / 1023.0); Serial.print("Valeur ADC: "); Serial.print(valeur); Serial.print(" - Tension: "); Serial.print(tension); Serial.println(" V"); delay(500); }
Chapitre 6 : Sorties PWM
Modulation de largeur d'impulsion
6.1 Principe du PWM
Rapport cyclique 25%: ┌─┐ ┌─┐ ┌─┐
──┘ └───┘ └───┘ └─── Vmoy = 1.25V
Rapport cyclique 50%: ┌──┐ ┌──┐ ┌──┐
──┘ └──┘ └──┘ └── Vmoy = 2.5V
Rapport cyclique 75%: ┌───┐ ┌───┐ ┌───┐
──┘ └─┘ └─┘ └─ Vmoy = 3.75V
Broches PWM sur Arduino Uno
Les broches PWM sont marquees avec le symbole ~ : 3, 5, 6, 9, 10, 11
6.2 Fonction analogWrite()
analogWrite(pin, valeur); // valeur de 0 (0%) a 255 (100%)
Exemple : Variation de luminosite LED
const int LED = 9; // Pin PWM void setup() { pinMode(LED, OUTPUT); } void loop() { // Augmente progressivement for (int i = 0; i <= 255; i++) { analogWrite(LED, i); delay(10); } // Diminue progressivement for (int i = 255; i >= 0; i--) { analogWrite(LED, i); delay(10); } }
Exemple : Controle de vitesse moteur DC
const int MOTEUR = 10; const int POT = A0; void setup() { pinMode(MOTEUR, OUTPUT); } void loop() { int lecture = analogRead(POT); // 0-1023 int vitesse = map(lecture, 0, 1023, 0, 255); // Convertit en 0-255 analogWrite(MOTEUR, vitesse); }
Chapitre 7 : Communication Serie
UART, I2C et SPI
7.1 Communication Serie (UART)
Fonctions Principales
Serial.begin(9600); // Initialise a 9600 bauds Serial.print("Texte"); // Envoie sans retour ligne Serial.println("Texte"); // Envoie avec retour ligne Serial.available(); // Nombre d'octets disponibles Serial.read(); // Lit un octet Serial.readString(); // Lit une chaine
Exemple : Echo Serie
void setup() { Serial.begin(9600); Serial.println("Pret!"); } void loop() { if (Serial.available() > 0) { String message = Serial.readString(); Serial.print("Recu: "); Serial.println(message); } }
7.2 Bus I2C
Le bus I2C utilise deux fils : SDA (donnees) et SCL (horloge). Sur Arduino Uno : A4 (SDA) et A5 (SCL).
#include <Wire.h> void setup() { Wire.begin(); // Initialise I2C en mode maitre } void loop() { Wire.beginTransmission(0x27); // Adresse du peripherique Wire.write(0x01); // Envoie un octet Wire.endTransmission(); // Termine la transmission delay(100); }
7.3 Bus SPI
Le bus SPI utilise 4 fils : MOSI (11), MISO (12), SCK (13), SS (10).
#include <SPI.h> const int CS = 10; void setup() { pinMode(CS, OUTPUT); SPI.begin(); } void loop() { digitalWrite(CS, LOW); // Active le peripherique SPI.transfer(0x42); // Envoie/recoit un octet digitalWrite(CS, HIGH); // Desactive delay(100); }
| Protocole | Fils | Vitesse | Multi-peripheriques |
|---|---|---|---|
| UART | 2 (TX, RX) | ~115200 bps | Non (point a point) |
| I2C | 2 (SDA, SCL) | 100-400 kbps | Oui (adressage) |
| SPI | 4 (MOSI, MISO, SCK, SS) | Plusieurs MHz | Oui (selection) |
Chapitre 8 : Capteurs
Interfacage avec differents capteurs
8.1 Capteur de Temperature LM35
const int LM35 = A0; void setup() { Serial.begin(9600); } void loop() { int lecture = analogRead(LM35); float tension = lecture * (5.0 / 1023.0); float temperature = tension * 100; // LM35: 10mV/degre Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" C"); delay(1000); }
8.2 Capteur DHT11 (Temperature + Humidite)
#include <DHT.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { float humidite = dht.readHumidity(); float temperature = dht.readTemperature(); Serial.print("Humidite: "); Serial.print(humidite); Serial.print("% - Temperature: "); Serial.print(temperature); Serial.println("C"); delay(2000); }
8.3 Capteur Ultrason HC-SR04
const int TRIG = 9; const int ECHO = 10; void setup() { Serial.begin(9600); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); } void loop() { // Envoie impulsion digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); // Mesure duree echo long duree = pulseIn(ECHO, HIGH); float distance = duree * 0.034 / 2; // cm Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); }
Chapitre 9 : Actionneurs
Controle de moteurs et servos
9.1 Servomoteur
#include <Servo.h> Servo monServo; void setup() { monServo.attach(9); // Pin de signal } void loop() { monServo.write(0); // Position 0 degres delay(1000); monServo.write(90); // Position 90 degres delay(1000); monServo.write(180); // Position 180 degres delay(1000); }
9.2 Moteur DC avec L298N
const int ENA = 10; // PWM vitesse const int IN1 = 8; // Direction const int IN2 = 9; // Direction void setup() { pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); } void avancer(int vitesse) { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, vitesse); } void reculer(int vitesse) { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, vitesse); } void arreter() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); } void loop() { avancer(200); delay(2000); arreter(); delay(500); reculer(150); delay(2000); arreter(); delay(500); }
9.3 Afficheur LCD I2C
#include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse, colonnes, lignes void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print("Hello World!"); lcd.setCursor(0, 1); lcd.print("Arduino LCD"); } void loop() { }
Chapitre 10 : Projets Pratiques
Applications completes
10.1 Station Meteo
#include <DHT.h> #include <LiquidCrystal_I2C.h> DHT dht(2, DHT11); LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { dht.begin(); lcd.init(); lcd.backlight(); } void loop() { float t = dht.readTemperature(); float h = dht.readHumidity(); lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(t, 1); lcd.print("C"); lcd.setCursor(0, 1); lcd.print("Hum: "); lcd.print(h, 1); lcd.print("%"); delay(2000); }
10.2 Systeme d'Alarme
const int PIR = 2; const int BUZZER = 8; const int LED = 13; void setup() { pinMode(PIR, INPUT); pinMode(BUZZER, OUTPUT); pinMode(LED, OUTPUT); Serial.begin(9600); delay(30000); // Calibration PIR (30s) Serial.println("Systeme pret!"); } void loop() { if (digitalRead(PIR) == HIGH) { Serial.println("Mouvement detecte!"); alarme(); } } void alarme() { for (int i = 0; i < 10; i++) { digitalWrite(LED, HIGH); tone(BUZZER, 1000); delay(200); digitalWrite(LED, LOW); noTone(BUZZER); delay(200); } }