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:
intled=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.
voidsetup(){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:
voidloop(){digitalWrite(LED,HIGH);// on allumedelay(1000);// 1 secondedigitalWrite(LED,LOW);// on éteintdelay(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 LEDvoidsetup(){pinMode(LED,OUTPUT);// la broche de la LED est une sortie}voidloop(){digitalWrite(LED,HIGH);// on allume la LEDdelay(1000);// on attend 1 secondedigitalWrite(LED,LOW);// on éteint la LEDdelay(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 :
intled=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.
voidsetup(){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 :
voidloop(){digitalWrite(LED,HIGH);// on allumedelay(1000);// 1 secondedigitalWrite(LED,LOW);// on éteintdelay(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 LEDvoidsetup(){pinMode(LED,OUTPUT);// la broche de la LED est une sortie}voidloop(){digitalWrite(LED,HIGH);// on allume la LEDdelay(1000);// on attend 1 secondedigitalWrite(LED,LOW);// on éteint la LEDdelay(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. بدونها يكون التيار قويًا جدًا.
تسمية المنفذ
هناك طريقتان لإعطاء اسم لمنفذ المصباح. نختار دائمًا اسمًا يدل على العنصر (مصباح، محرك، حساس…):
باستخدام متغير:
intled=8;
باستخدام #define، وهي الطريقة المعتمدة في المشروع: يستبدل المترجم LED بالرقم 8 قبل الترجمة، فلا تأخذ أي مساحة من الذاكرة.
#define LED 8
الدالة setup()
نخبر لوحة Arduino أن منفذ المصباح مخرج، لأن اللوحة هي التي ترسل الإشارة. ولو كان على اللوحة أن تقرأ إشارة (من زر مثلًا) لاستعملنا INPUT.
voidsetup(){pinMode(LED,OUTPUT);}
الدالة loop()
لكي يومض المصباح نشغله، ثم ننتظر، ثم نطفئه، ثم ننتظر من جديد. وبما أن loop() تتكرر دون توقف، يستمر المصباح في الوميض:
voidloop(){digitalWrite(LED,HIGH);// on allumedelay(1000);// 1 secondedigitalWrite(LED,LOW);// on éteintdelay(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 LEDvoidsetup(){pinMode(LED,OUTPUT);// la broche de la LED est une sortie}voidloop(){digitalWrite(LED,HIGH);// on allume la LEDdelay(1000);// on attend 1 secondedigitalWrite(LED,LOW);// on éteint la LEDdelay(1000);// on attend 1 seconde}
للتعمق أكثر
غيّر القيمة 1000 ليصبح الوميض أسرع أو أبطأ.
أضف مصباحًا ثانيًا على منفذ آخر واجعلهما يومضان بالتناوب.
اجعل المصباح يومض باستخدام millis() بدل delay()، حتى يتمكن البرنامج من تنفيذ مهام أخرى في الوقت نفسه.