Project 1

Make an LED blink

The first Arduino project: we make an LED blink with pinMode, digitalWrite and delay.

Débutant15 minArduino Uno or Mega

Faire clignoter une LED

Le premier projet Arduino : on fait clignoter une LED avec pinMode, digitalWrite et delay.

Débutant15 minArduino Uno ou Mega

تشغيل وإطفاء مصباح LED بشكل متكرر

أول مشروع مع Arduino: نجعل مصباح LED يومض باستخدام الدوال pinMode وdigitalWrite وdelay.

Débutant15 minArduino Uno أو Mega

The video is coming soon.

The Arduino playlist on YouTube
Arduino project no. 1: make an LED blink
Written by RHIBA Zakaria, Zakpy Academy

This is the first project everyone builds when starting with Arduino. We connect an LED to a digital pin, here pin 8, and make it blink: on for one second, off for one second.

What you will learn

  • Name a pin with #define or with a variable.
  • Set a pin as an output with pinMode().
  • Turn an LED on and off with digitalWrite().
  • Wait with delay().

What you need

  • An Arduino Uno or Mega board
  • An LED
  • A 220 Ω resistor
  • A breadboard and wires

Wiring

From To
Pin 8 220 Ω resistor
Resistor long leg of the LED (anode, +)
Short leg of the LED (cathode, −) GND

The resistor protects the LED and the Arduino pin. Without it, the current is too high.

Naming the pin

There are two ways to give the LED pin a name. Always pick a name that says what it is (led, motor, sensor…):

  • With a variable:
int led = 8;
  • With a #define. This is what the project uses: the compiler replaces LED with 8 before compiling, so it takes no memory.
#define LED 8

The setup() function

We tell the Arduino that the LED pin is an output, because the Arduino sends the signal. If the Arduino had to read a signal (a button for example), we would use INPUT.

void setup() {
  pinMode(LED, OUTPUT);
}

The loop() function

To make the LED blink, we turn it on, wait, turn it off and wait again. Since loop() repeats forever, the LED keeps blinking:

void loop() {
  digitalWrite(LED, HIGH);   // on allume
  delay(1000);               // 1 seconde
  digitalWrite(LED, LOW);    // on éteint
  delay(1000);
}

The full code

The comments in the code are in French, like the .ino file you download.

/*
 * Projet : faire clignoter une LED
 * Zakpy Academy - RHIBA Zakaria
 * Carte : Arduino Uno ou Mega
 *
 * La LED est branchée sur la broche 8, avec une résistance de 220 ohms.
 */

#define LED 8              // la broche de la LED

void setup() {
  pinMode(LED, OUTPUT);    // la broche de la LED est une sortie
}

void loop() {
  digitalWrite(LED, HIGH); // on allume la LED
  delay(1000);             // on attend 1 seconde
  digitalWrite(LED, LOW);  // on éteint la LED
  delay(1000);             // on attend 1 seconde
}

Going further

  • Change the 1000 to blink faster or slower.
  • Add a second LED on another pin and make them blink one after the other.
  • Make the LED blink with millis() instead of delay(), so the program can do something else at the same time.
Projet Arduino n°1 : faire clignoter une LED
Écrit par RHIBA Zakaria, Zakpy Academy

C'est le premier projet qu'on fait quand on commence avec Arduino. On branche une LED sur une broche numérique, ici la broche 8, et on la fait clignoter : allumée une seconde, éteinte une seconde.

Ce qu'on va apprendre

  • Déclarer une broche avec #define ou avec une variable.
  • Mettre une broche en sortie avec pinMode().
  • Allumer et éteindre une LED avec digitalWrite().
  • Attendre avec delay().

Le matériel

  • Une carte Arduino Uno ou Mega
  • Une LED
  • Une résistance de 220 Ω
  • Une breadboard et des fils

Le câblage

De Vers
Broche 8 résistance de 220 Ω
Résistance patte longue de la LED (anode, +)
Patte courte de la LED (cathode, −) GND

La résistance protège la LED et la broche de l'Arduino. Sans elle, le courant est trop fort.

Déclarer la broche

Il y a deux façons de donner un nom à la broche de la LED. On choisit toujours un nom qui dit ce que c'est (led, moteur, capteur…) :

  • Avec une variable :
int led = 8;
  • Avec un #define. C'est la façon qu'on utilise dans le projet : le compilateur remplace LED par 8 avant de compiler, donc ça ne prend pas de place en mémoire.
#define LED 8

La fonction setup()

On dit à l'Arduino que la broche de la LED est une sortie, parce que c'est l'Arduino qui envoie le signal. Si l'Arduino devait lire un signal (un bouton par exemple), on mettrait INPUT.

void setup() {
  pinMode(LED, OUTPUT);
}

La fonction loop()

Pour faire clignoter la LED, on l'allume, on attend, on l'éteint et on attend encore. Comme loop() se répète à l'infini, la LED clignote sans arrêt :

void loop() {
  digitalWrite(LED, HIGH);   // on allume
  delay(1000);               // 1 seconde
  digitalWrite(LED, LOW);    // on éteint
  delay(1000);
}

Le code complet

/*
 * Projet : faire clignoter une LED
 * Zakpy Academy - RHIBA Zakaria
 * Carte : Arduino Uno ou Mega
 *
 * La LED est branchée sur la broche 8, avec une résistance de 220 ohms.
 */

#define LED 8              // la broche de la LED

void setup() {
  pinMode(LED, OUTPUT);    // la broche de la LED est une sortie
}

void loop() {
  digitalWrite(LED, HIGH); // on allume la LED
  delay(1000);             // on attend 1 seconde
  digitalWrite(LED, LOW);  // on éteint la LED
  delay(1000);             // on attend 1 seconde
}

Pour aller plus loin

  • Change le 1000 pour faire clignoter plus vite ou plus lentement.
  • Ajoute une deuxième LED sur une autre broche et fais-les clignoter chacune leur tour.
  • Fais clignoter la LED avec millis() à la place de delay(), pour que le programme puisse faire autre chose en même temps.
مشروع Arduino رقم 1: وميض مصباح LED
إعداد: RHIBA Zakaria، Zakpy Academy

هذا هو المشروع الأول الذي ينجزه كل من يبدأ مع Arduino. نوصل مصباح LED بمنفذ رقمي، وهو هنا المنفذ 8، ونجعله يومض: يشتغل لمدة ثانية ثم ينطفئ لمدة ثانية.

ما الذي سنتعلمه

  • تسمية منفذ باستخدام #define أو باستخدام متغير.
  • جعل المنفذ مخرجًا باستخدام pinMode().
  • تشغيل المصباح وإطفاؤه باستخدام digitalWrite().
  • الانتظار باستخدام delay().

المكونات

  • لوحة Arduino Uno أو Mega
  • مصباح LED واحد
  • مقاومة 220 أوم
  • لوحة تجارب وأسلاك توصيل

التوصيل

من إلى
المنفذ 8 المقاومة 220 أوم
المقاومة الساق الطويلة للمصباح (المصعد، +)
الساق القصيرة للمصباح (المهبط، −) GND

المقاومة تحمي المصباح وتحمي منفذ لوحة Arduino. بدونها يكون التيار قويًا جدًا.

تسمية المنفذ

هناك طريقتان لإعطاء اسم لمنفذ المصباح. نختار دائمًا اسمًا يدل على العنصر (مصباح، محرك، حساس…):

  • باستخدام متغير:
int led = 8;
  • باستخدام #define، وهي الطريقة المعتمدة في المشروع: يستبدل المترجم LED بالرقم 8 قبل الترجمة، فلا تأخذ أي مساحة من الذاكرة.
#define LED 8

الدالة setup()

نخبر لوحة Arduino أن منفذ المصباح مخرج، لأن اللوحة هي التي ترسل الإشارة. ولو كان على اللوحة أن تقرأ إشارة (من زر مثلًا) لاستعملنا INPUT.

void setup() {
  pinMode(LED, OUTPUT);
}

الدالة loop()

لكي يومض المصباح نشغله، ثم ننتظر، ثم نطفئه، ثم ننتظر من جديد. وبما أن loop() تتكرر دون توقف، يستمر المصباح في الوميض:

void loop() {
  digitalWrite(LED, HIGH);   // on allume
  delay(1000);               // 1 seconde
  digitalWrite(LED, LOW);    // on éteint
  delay(1000);
}

الشيفرة الكاملة

التعليقات داخل الشيفرة باللغة الفرنسية، كما في ملف .ino الذي يمكنك تحميله.

/*
 * Projet : faire clignoter une LED
 * Zakpy Academy - RHIBA Zakaria
 * Carte : Arduino Uno ou Mega
 *
 * La LED est branchée sur la broche 8, avec une résistance de 220 ohms.
 */

#define LED 8              // la broche de la LED

void setup() {
  pinMode(LED, OUTPUT);    // la broche de la LED est une sortie
}

void loop() {
  digitalWrite(LED, HIGH); // on allume la LED
  delay(1000);             // on attend 1 seconde
  digitalWrite(LED, LOW);  // on éteint la LED
  delay(1000);             // on attend 1 seconde
}

للتعمق أكثر

  • غيّر القيمة 1000 ليصبح الوميض أسرع أو أبطأ.
  • أضف مصباحًا ثانيًا على منفذ آخر واجعلهما يومضان بالتناوب.
  • اجعل المصباح يومض باستخدام millis() بدل delay()، حتى يتمكن البرنامج من تنفيذ مهام أخرى في الوقت نفسه.