A thermometer accurate to a tenth of a degree with an LM35 and a 7-segment display, and an adjustable alarm that makes an LED blink. We use the ADC, multiplexing, registers and two timers.
Intermédiaire3 hArduino Mega 2560Video
Thermomètre numérique avec alarme réglable
Un thermomètre au dixième de degré avec un LM35 et un afficheur 7 segments, et une alarme réglable qui fait clignoter une LED. On utilise l'ADC, le multiplexage, les registres et deux timers.
Intermédiaire3 hArduino Mega 2560Video
ميزان حرارة رقمي مع منبه قابل للضبط
ميزان حرارة بدقة عُشر درجة باستخدام الحساس LM35 وشاشة عرض ذات سبع قطع، مع منبه قابل للضبط يجعل مصباح LED يومض. نستخدم المحول التناظري الرقمي، والمسح المتعدد، والسجلات، ومؤقتين.
Arduino project no. 5: LM35 and 7-segment display
Written by RHIBA Zakaria, Zakpy Academy
In this project we measure the temperature with an LM35 sensor and show it on a 7-segment display to 0.1 °C (for example 24.8). A potentiometer sets an alarm temperature between 0 and 40 °C. When the temperature goes above the alarm, an LED starts blinking.
The project looks simple, but it covers important ideas: the ADC, multiplexing a display, the microcontroller registers, timers and interrupts.
Read a voltage with the ADC and pick the right reference (1.1 V).
Encode the digits of a 7-segment display in one byte.
Write 8 pins at once with the PORTA register.
Show several digits with multiplexing.
Set up a timer and use an interrupt.
What you need
An Arduino Mega 2560 board
An LM35 temperature sensor
A 4-digit 7-segment display (12 pins, for example a common cathode 5641AS)
A 1 kΩ potentiometer and a 3.3 kΩ resistor
An LED and a 220 Ω resistor
8 1 kΩ resistors (one per segment)
Wiring
The project schematic in Proteus: Arduino Mega 2560, LM35, 7-segment display, potentiometer and LED (here a common anode display with 4049 inverters)
Part
Arduino pin
LM35 (Vout output)
A0
Potentiometer (wiper)
A1
Segments A, B, C, D, E, F, G, DP
22 to 29 (PORTA)
Tens, units, tenths digits
52, 51, 50
Alarm LED
32
Pins 22 to 29 form PORTA of the ATmega2560. So we can write the 8 segments of a digit in one line: PORTA = 0x5B; shows a 2.
Measuring the temperature
The LM35 gives 10 mV per degree. We use the internal 1.1 V reference of the ADC: one step is then 1.1 V / 1024 = 1.07 mV, about 0.1 °C.
analogReference(INTERNAL1V1);
The calculation N × 1100 / 1024 gives millivolts, and since 1 mV = 0.1 °C, it is directly the temperature in tenths of a degree (248 = 24.8 °C). We use a long because 1023 × 1100 is too big for an int:
The segments are shared between the digits, so we light only one digit at a time, 5 ms each: this is multiplexing. Timer 1 handles it in an interrupt every 5 ms (prescaler 64, OCR1A = 1249):
ISR(TIMER1_COMPA_vect){digitalWrite(DIG_D2,HIGH);// on éteint toutdigitalWrite(DIG_D3,HIGH);digitalWrite(DIG_D4,HIGH);PORTA=affichage[digitCourant];// les segments du chiffredigitalWrite(pinDigits[digitCourant],LOW);// on allume ce chiffredigitCourant++;if(digitCourant>=3)digitCourant=0;}
The alarm
The potentiometer becomes a temperature between 0 and 40.0 °C:
Timer 3 makes the LED blink every 0.5 s (prescaler 256, OCR3A = 31249) when the temperature is above the alarm. The loop() only measures and decides:
alarme=(temp>temp_trigger);
The project steps
The code of each step is in the steps folder, with the same numbers as the project sheet:
Etape1_Lire_Temperature: read the LM35 and test the display with 88.8
Etape2_Affichage_Timer1: show the temperature with Timer 1
Step 3 is the wiring on the real board, with the same code as step 2
Etape4_Alarme_Potentiometre: set the alarm with the potentiometer, the LED turns on
Etape5_Alarme_Clignotante_Timer3: the LED blinks with Timer 3
Testing without hardware on Wokwi
Create a new Arduino Mega project on wokwi.com, paste wokwi/sketch.ino in the code tab and wokwi/diagram.json in the diagram tab, then start the simulation. Wokwi has no LM35, so a potentiometer on A0 plays the temperature.
On the real board, if your display is common anode (for example a 5641BS), you must invert the segments (PORTA = ~affichage[...]) and the digits (HIGH to select, LOW to turn off).
The full code
The comments and messages in the code are in French, like the .ino file you download.
/********************************************************************* * Thermomètre numérique avec alarme - Arduino Mega 2560 (16 MHz) * LM35 + afficheur 7 segments (Timer 1) + alarme réglable (Timer 3) * Written by RHIBA Zakaria - Zakpy Academy *********************************************************************/#define LM35_pin A0 // capteur LM35#define POT A1 // potentiomètre du seuil#define LED 32 // LED d'alarme#define DIG_D2 52 // afficheur des dizaines#define DIG_D3 51 // afficheur des unités (+ point)#define DIG_D4 50 // afficheur des dixièmes// Codes 0..9, 1 = segment allumé (cathode commune, ou anode commune + 4049)bytetabDigit[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};constbytepinDigits[3]={DIG_D2,DIG_D3,DIG_D4};volatilebyteaffichage[3];// les 3 codes à affichervolatilebytedigitCourant=0;// afficheur allumé en ce momentvolatileboolalarme=false;// décidée par loop(), lue par Timer 3inttemp;// température en dixièmes (248 = 24,8 °C)inttemp_trigger;// seuil en dixièmes (0..400)voidprepareAffichage(intnumber);intmesureTemp();intmesureTrigger();voidinitTimer1();voidinitTimer3();voidsetup(){Serial.begin(9600);pinMode(DIG_D2,OUTPUT);digitalWrite(DIG_D2,HIGH);// HIGH = éteintpinMode(DIG_D3,OUTPUT);digitalWrite(DIG_D3,HIGH);pinMode(DIG_D4,OUTPUT);digitalWrite(DIG_D4,HIGH);pinMode(LED,OUTPUT);digitalWrite(LED,LOW);analogReference(INTERNAL1V1);// 1 pas = 1,07 mV ≈ 0,1 °CanalogRead(LM35_pin);// 1re lecture ignoréeDDRA=0xFF;// broches 22..29 en sortiePORTA=0x00;initTimer1();// multiplexage toutes les 5 msinitTimer3();// clignotement toutes les 0,5 sprepareAffichage(888);// test des segments : 88.8delay(3000);}voidloop(){temp=mesureTemp();temp_trigger=mesureTrigger();alarme=(temp>temp_trigger);// loop() décide, les timers agissentprepareAffichage(temp);Serial.print("Temp = ");Serial.print(temp/10);Serial.print(".");Serial.print(temp%10);Serial.print(" C | Seuil = ");Serial.print(temp_trigger/10);Serial.print(".");Serial.print(temp_trigger%10);Serial.println(alarme?" C -> ALARME":" C");delay(500);}// Température en dixièmes : N x 1100 / 1024 = mV = dixièmes de °CintmesureTemp(){longN=analogRead(LM35_pin);// long : 1023 x 1100 dépasse un intreturn(int)((N*1100L)/1024L);}// Seuil en dixièmes : 0..1023 -> 0..400 (0,0 à 40,0 °C)intmesureTrigger(){returnmap(analogRead(POT),0,1023,0,400);}// Découpe la valeur en 3 chiffres et prépare les codesvoidprepareAffichage(intnumber){if(number>999)number=999;// 3 digits : 99,9 maxif(number<0)number=0;intdizaine=number/100;intunite=(number/10)%10;intdixieme=number%10;if(dizaine==0)affichage[0]=0x00;// pas de zéro inutileelseaffichage[0]=tabDigit[dizaine];affichage[1]=tabDigit[unite]|0x80;// point décimal (bit 7)affichage[2]=tabDigit[dixieme];}// Timer 1 : CTC, prescaler 64, 1250 ticks de 4 us = 5 msvoidinitTimer1(){noInterrupts();TCCR1A=0;TCCR1B=0;TCNT1=0;OCR1A=1249;TCCR1B|=(1<<WGM12);// mode CTCTCCR1B|=(1<<CS11)|(1<<CS10);// prescaler 64TIMSK1|=(1<<OCIE1A);// autorise l'interruptioninterrupts();}// Timer 3 : CTC, prescaler 256, 31250 ticks de 16 us = 0,5 svoidinitTimer3(){noInterrupts();TCCR3A=0;TCCR3B=0;TCNT3=0;OCR3A=31249;TCCR3B|=(1<<WGM32);// mode CTCTCCR3B|=(1<<CS32);// prescaler 256TIMSK3|=(1<<OCIE3A);interrupts();}// Multiplexage : un afficheur à chaque appel (toutes les 5 ms)ISR(TIMER1_COMPA_vect){digitalWrite(DIG_D2,HIGH);// 1) tout éteindredigitalWrite(DIG_D3,HIGH);digitalWrite(DIG_D4,HIGH);PORTA=affichage[digitCourant];// 2) segments du chiffredigitalWrite(pinDigits[digitCourant],LOW);// 3) allumer cet afficheurdigitCourant++;// 4) le suivant au prochain appelif(digitCourant>=3)digitCourant=0;}// Clignotement : bascule la LED toutes les 0,5 s si alarmeISR(TIMER3_COMPA_vect){if(alarme)digitalWrite(LED,!digitalRead(LED));elsedigitalWrite(LED,LOW);}
Going further
Replace the LED with a buzzer for a real sound alarm.
Add hysteresis so the alarm does not keep switching on and off around the limit.
Keep the limit in memory (EEPROM) even after a power cut.
Send the temperature to your phone, a first brick for the King House smart home project.
Projet Arduino n°5 : LM35 et afficheur 7 segments
Écrit par RHIBA Zakaria, Zakpy Academy
Dans ce projet on mesure la température avec un capteur LM35, et on l'affiche sur un afficheur 7 segments avec une précision de 0,1 °C (par exemple 24.8). Un potentiomètre permet de régler une température d'alarme entre 0 et 40 °C. Quand la température dépasse cette alarme, une LED se met à clignoter.
Le projet a l'air simple, mais il permet de voir des notions importantes : l'ADC, le multiplexage d'un afficheur, les registres du microcontrôleur, les timers et les interruptions.
Lire une tension avec l'ADC et choisir la bonne référence (1,1 V).
Coder les chiffres d'un afficheur 7 segments sur un octet.
Écrire 8 broches d'un coup avec le registre PORTA.
Afficher plusieurs chiffres avec le multiplexage.
Configurer un timer et utiliser une interruption.
Le matériel
Une carte Arduino Mega 2560
Un capteur de température LM35
Un afficheur 7 segments 4 chiffres (12 broches, par exemple 5641AS à cathode commune)
Un potentiomètre de 1 kΩ et une résistance de 3,3 kΩ
Une LED et une résistance de 220 Ω
8 résistances de 1 kΩ (une par segment)
Le câblage
Le schéma du projet sur Proteus : Arduino Mega 2560, LM35, afficheur 7 segments, potentiomètre et LED (ici un afficheur à anode commune avec des inverseurs 4049)
Élément
Broche Arduino
LM35 (sortie Vout)
A0
Potentiomètre (curseur)
A1
Segments A, B, C, D, E, F, G, DP
22 à 29 (PORTA)
Chiffres dizaines, unités, dixièmes
52, 51, 50
LED d'alarme
32
Les broches 22 à 29 forment le PORTA de l'ATmega2560. On peut donc écrire les 8 segments d'un chiffre en une ligne : PORTA = 0x5B; affiche un 2.
Mesurer la température
Le LM35 donne 10 mV par degré. On utilise la référence interne de 1,1 V de l'ADC : un pas fait alors 1,1 V / 1024 = 1,07 mV, soit environ 0,1 °C.
analogReference(INTERNAL1V1);
Le calcul N × 1100 / 1024 donne des mV, et comme 1 mV = 0,1 °C, c'est directement la température en dixièmes de degré (248 = 24,8 °C). On utilise un long parce que 1023 × 1100 dépasse la taille d'un int :
Les segments sont partagés entre les chiffres, donc on allume un seul chiffre à la fois, 5 ms chacun : c'est le multiplexage. Le Timer 1 s'en occupe dans une interruption toutes les 5 ms (prescaler 64, OCR1A = 1249) :
ISR(TIMER1_COMPA_vect){digitalWrite(DIG_D2,HIGH);// on éteint toutdigitalWrite(DIG_D3,HIGH);digitalWrite(DIG_D4,HIGH);PORTA=affichage[digitCourant];// les segments du chiffredigitalWrite(pinDigits[digitCourant],LOW);// on allume ce chiffredigitCourant++;if(digitCourant>=3)digitCourant=0;}
L'alarme
Le potentiomètre est transformé en une température entre 0 et 40,0 °C :
Le Timer 3 fait clignoter la LED toutes les 0,5 s (prescaler 256, OCR3A = 31249) quand la température dépasse l'alarme. La boucle loop() se contente de mesurer et de décider :
alarme=(temp>temp_trigger);
Les étapes du projet
Le code de chaque étape est dans le dossier steps, avec les mêmes numéros que la fiche projet :
Etape1_Lire_Temperature : lire le LM35 et tester l'afficheur avec 88.8
Etape2_Affichage_Timer1 : afficher la température avec le Timer 1
L'étape 3 est le câblage sur la vraie carte, avec le même code que l'étape 2
Etape4_Alarme_Potentiometre : régler l'alarme avec le potentiomètre, la LED s'allume
Etape5_Alarme_Clignotante_Timer3 : la LED clignote avec le Timer 3
Tester sans matériel sur Wokwi
Crée un nouveau projet Arduino Mega sur wokwi.com, colle wokwi/sketch.ino dans l'onglet du code et wokwi/diagram.json dans l'onglet du schéma, puis lance la simulation. Wokwi n'a pas de LM35, donc un potentiomètre sur A0 joue le rôle de la température.
Sur la vraie carte, si ton afficheur est à anode commune (par exemple 5641BS), il faut inverser les segments (PORTA = ~affichage[...]) et les chiffres (HIGH pour sélectionner, LOW pour éteindre).
Le code complet
/********************************************************************* * Thermomètre numérique avec alarme - Arduino Mega 2560 (16 MHz) * LM35 + afficheur 7 segments (Timer 1) + alarme réglable (Timer 3) * Written by RHIBA Zakaria - Zakpy Academy *********************************************************************/#define LM35_pin A0 // capteur LM35#define POT A1 // potentiomètre du seuil#define LED 32 // LED d'alarme#define DIG_D2 52 // afficheur des dizaines#define DIG_D3 51 // afficheur des unités (+ point)#define DIG_D4 50 // afficheur des dixièmes// Codes 0..9, 1 = segment allumé (cathode commune, ou anode commune + 4049)bytetabDigit[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};constbytepinDigits[3]={DIG_D2,DIG_D3,DIG_D4};volatilebyteaffichage[3];// les 3 codes à affichervolatilebytedigitCourant=0;// afficheur allumé en ce momentvolatileboolalarme=false;// décidée par loop(), lue par Timer 3inttemp;// température en dixièmes (248 = 24,8 °C)inttemp_trigger;// seuil en dixièmes (0..400)voidprepareAffichage(intnumber);intmesureTemp();intmesureTrigger();voidinitTimer1();voidinitTimer3();voidsetup(){Serial.begin(9600);pinMode(DIG_D2,OUTPUT);digitalWrite(DIG_D2,HIGH);// HIGH = éteintpinMode(DIG_D3,OUTPUT);digitalWrite(DIG_D3,HIGH);pinMode(DIG_D4,OUTPUT);digitalWrite(DIG_D4,HIGH);pinMode(LED,OUTPUT);digitalWrite(LED,LOW);analogReference(INTERNAL1V1);// 1 pas = 1,07 mV ≈ 0,1 °CanalogRead(LM35_pin);// 1re lecture ignoréeDDRA=0xFF;// broches 22..29 en sortiePORTA=0x00;initTimer1();// multiplexage toutes les 5 msinitTimer3();// clignotement toutes les 0,5 sprepareAffichage(888);// test des segments : 88.8delay(3000);}voidloop(){temp=mesureTemp();temp_trigger=mesureTrigger();alarme=(temp>temp_trigger);// loop() décide, les timers agissentprepareAffichage(temp);Serial.print("Temp = ");Serial.print(temp/10);Serial.print(".");Serial.print(temp%10);Serial.print(" C | Seuil = ");Serial.print(temp_trigger/10);Serial.print(".");Serial.print(temp_trigger%10);Serial.println(alarme?" C -> ALARME":" C");delay(500);}// Température en dixièmes : N x 1100 / 1024 = mV = dixièmes de °CintmesureTemp(){longN=analogRead(LM35_pin);// long : 1023 x 1100 dépasse un intreturn(int)((N*1100L)/1024L);}// Seuil en dixièmes : 0..1023 -> 0..400 (0,0 à 40,0 °C)intmesureTrigger(){returnmap(analogRead(POT),0,1023,0,400);}// Découpe la valeur en 3 chiffres et prépare les codesvoidprepareAffichage(intnumber){if(number>999)number=999;// 3 digits : 99,9 maxif(number<0)number=0;intdizaine=number/100;intunite=(number/10)%10;intdixieme=number%10;if(dizaine==0)affichage[0]=0x00;// pas de zéro inutileelseaffichage[0]=tabDigit[dizaine];affichage[1]=tabDigit[unite]|0x80;// point décimal (bit 7)affichage[2]=tabDigit[dixieme];}// Timer 1 : CTC, prescaler 64, 1250 ticks de 4 us = 5 msvoidinitTimer1(){noInterrupts();TCCR1A=0;TCCR1B=0;TCNT1=0;OCR1A=1249;TCCR1B|=(1<<WGM12);// mode CTCTCCR1B|=(1<<CS11)|(1<<CS10);// prescaler 64TIMSK1|=(1<<OCIE1A);// autorise l'interruptioninterrupts();}// Timer 3 : CTC, prescaler 256, 31250 ticks de 16 us = 0,5 svoidinitTimer3(){noInterrupts();TCCR3A=0;TCCR3B=0;TCNT3=0;OCR3A=31249;TCCR3B|=(1<<WGM32);// mode CTCTCCR3B|=(1<<CS32);// prescaler 256TIMSK3|=(1<<OCIE3A);interrupts();}// Multiplexage : un afficheur à chaque appel (toutes les 5 ms)ISR(TIMER1_COMPA_vect){digitalWrite(DIG_D2,HIGH);// 1) tout éteindredigitalWrite(DIG_D3,HIGH);digitalWrite(DIG_D4,HIGH);PORTA=affichage[digitCourant];// 2) segments du chiffredigitalWrite(pinDigits[digitCourant],LOW);// 3) allumer cet afficheurdigitCourant++;// 4) le suivant au prochain appelif(digitCourant>=3)digitCourant=0;}// Clignotement : bascule la LED toutes les 0,5 s si alarmeISR(TIMER3_COMPA_vect){if(alarme)digitalWrite(LED,!digitalRead(LED));elsedigitalWrite(LED,LOW);}
Pour aller plus loin
Remplace la LED par un buzzer pour avoir une vraie alarme sonore.
Ajoute une hystérésis pour que l'alarme ne s'allume et ne s'éteigne pas sans arrêt autour de la limite.
Garde la limite en mémoire (EEPROM) même après une coupure de courant.
Envoie la température sur le téléphone, une première brique pour le projet de maison connectée King House.
مشروع Arduino رقم 5: LM35 وشاشة عرض ذات سبع قطع
إعداد: RHIBA Zakaria، Zakpy Academy
في هذا المشروع نقيس درجة الحرارة بالحساس LM35، ونعرضها على شاشة عرض ذات سبع قطع بدقة 0.1 درجة مئوية (مثلًا 24.8). تسمح مقاومة متغيرة بضبط درجة حرارة المنبه بين 0 و 40 درجة. عندما تتجاوز الحرارة هذه القيمة، يبدأ مصباح LED في الوميض.
يبدو المشروع بسيطًا، لكنه يسمح بالتعرف على مفاهيم مهمة: المحول التناظري الرقمي، والمسح المتعدد للشاشة، وسجلات المتحكم الدقيق، والمؤقتات، والمقاطعات.
ترميز أرقام شاشة العرض ذات القطع السبع في بايت واحد.
كتابة 8 منافذ دفعة واحدة باستخدام السجل PORTA.
عرض عدة أرقام بطريقة المسح المتعدد.
ضبط مؤقت واستخدام مقاطعة.
المكونات
لوحة Arduino Mega 2560
حساس الحرارة LM35
شاشة عرض ذات سبع قطع بأربعة أرقام (12 طرفًا، مثل 5641AS بمهبط مشترك)
مقاومة متغيرة 1 كيلو أوم ومقاومة 3.3 كيلو أوم
مصباح LED ومقاومة 220 أوم
8 مقاومات 1 كيلو أوم (واحدة لكل قطعة)
التوصيل
مخطط المشروع على Proteus: لوحة Arduino Mega 2560، الحساس LM35، شاشة العرض ذات القطع السبع، المقاومة المتغيرة ومصباح LED (هنا شاشة بمصعد مشترك مع عواكس 4049)
العنصر
منفذ Arduino
الحساس LM35 (المخرج Vout)
A0
المقاومة المتغيرة (الطرف الأوسط)
A1
القطع A و B و C و D و E و F و G و DP
من 22 إلى 29 (PORTA)
أرقام العشرات والوحدات والأعشار
52 و 51 و 50
مصباح المنبه
32
المنافذ من 22 إلى 29 تشكل السجل PORTA في المتحكم ATmega2560. لذلك يمكننا كتابة القطع الثماني لرقم ما في سطر واحد: PORTA = 0x5B; يعرض الرقم 2.
قياس درجة الحرارة
يعطي الحساس LM35 جهدًا قدره 10 ملي فولت لكل درجة. نستخدم الجهد المرجعي الداخلي 1.1 فولت للمحول التناظري الرقمي، فتصبح الخطوة 1.1 / 1024 = 1.07 ملي فولت، أي حوالي 0.1 درجة.
analogReference(INTERNAL1V1);
العملية N × 1100 / 1024 تعطي الجهد بالملي فولت، وبما أن 1 ملي فولت يساوي 0.1 درجة، فالنتيجة هي درجة الحرارة بالأعشار مباشرة (248 تعني 24.8 درجة). نستخدم النوع long لأن 1023 × 1100 أكبر من سعة النوع int:
القطع مشتركة بين الأرقام، لذلك نشغل رقمًا واحدًا في كل مرة، لمدة 5 ملي ثانية لكل رقم: هذا هو المسح المتعدد. يتكفل المؤقت 1 بذلك داخل مقاطعة كل 5 ملي ثانية (مقسم التردد 64، و OCR1A = 1249):
ISR(TIMER1_COMPA_vect){digitalWrite(DIG_D2,HIGH);// on éteint toutdigitalWrite(DIG_D3,HIGH);digitalWrite(DIG_D4,HIGH);PORTA=affichage[digitCourant];// les segments du chiffredigitalWrite(pinDigits[digitCourant],LOW);// on allume ce chiffredigitCourant++;if(digitCourant>=3)digitCourant=0;}
المنبه
تتحول قيمة المقاومة المتغيرة إلى درجة حرارة بين 0 و 40.0 درجة:
يجعل المؤقت 3 المصباح يومض كل 0.5 ثانية (مقسم التردد 256، و OCR3A = 31249) عندما تتجاوز الحرارة قيمة المنبه. أما الدالة loop() فتكتفي بالقياس واتخاذ القرار:
alarme=(temp>temp_trigger);
مراحل المشروع
شيفرة كل مرحلة موجودة في المجلد steps، بنفس أرقام بطاقة المشروع:
Etape1_Lire_Temperature: قراءة الحساس LM35 واختبار الشاشة بعرض 88.8
Etape2_Affichage_Timer1: عرض درجة الحرارة باستخدام المؤقت 1
المرحلة 3 هي التوصيل على اللوحة الحقيقية، بنفس شيفرة المرحلة 2
Etape5_Alarme_Clignotante_Timer3: وميض المصباح باستخدام المؤقت 3
الاختبار دون مكونات على Wokwi
أنشئ مشروع Arduino Mega جديدًا على wokwi.com، والصق wokwi/sketch.ino في تبويب الشيفرة و wokwi/diagram.json في تبويب المخطط، ثم شغّل المحاكاة. لا يوجد الحساس LM35 في Wokwi، لذلك تؤدي مقاومة متغيرة على المنفذ A0 دور الحرارة.
على اللوحة الحقيقية، إذا كانت شاشتك بمصعد مشترك (مثل 5641BS)، يجب عكس القطع (PORTA = ~affichage[...]) وعكس الأرقام (HIGH للاختيار و LOW للإطفاء).
الشيفرة الكاملة
التعليقات والرسائل داخل الشيفرة باللغة الفرنسية، كما في ملف .ino الذي يمكنك تحميله.
/********************************************************************* * Thermomètre numérique avec alarme - Arduino Mega 2560 (16 MHz) * LM35 + afficheur 7 segments (Timer 1) + alarme réglable (Timer 3) * Written by RHIBA Zakaria - Zakpy Academy *********************************************************************/#define LM35_pin A0 // capteur LM35#define POT A1 // potentiomètre du seuil#define LED 32 // LED d'alarme#define DIG_D2 52 // afficheur des dizaines#define DIG_D3 51 // afficheur des unités (+ point)#define DIG_D4 50 // afficheur des dixièmes// Codes 0..9, 1 = segment allumé (cathode commune, ou anode commune + 4049)bytetabDigit[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};constbytepinDigits[3]={DIG_D2,DIG_D3,DIG_D4};volatilebyteaffichage[3];// les 3 codes à affichervolatilebytedigitCourant=0;// afficheur allumé en ce momentvolatileboolalarme=false;// décidée par loop(), lue par Timer 3inttemp;// température en dixièmes (248 = 24,8 °C)inttemp_trigger;// seuil en dixièmes (0..400)voidprepareAffichage(intnumber);intmesureTemp();intmesureTrigger();voidinitTimer1();voidinitTimer3();voidsetup(){Serial.begin(9600);pinMode(DIG_D2,OUTPUT);digitalWrite(DIG_D2,HIGH);// HIGH = éteintpinMode(DIG_D3,OUTPUT);digitalWrite(DIG_D3,HIGH);pinMode(DIG_D4,OUTPUT);digitalWrite(DIG_D4,HIGH);pinMode(LED,OUTPUT);digitalWrite(LED,LOW);analogReference(INTERNAL1V1);// 1 pas = 1,07 mV ≈ 0,1 °CanalogRead(LM35_pin);// 1re lecture ignoréeDDRA=0xFF;// broches 22..29 en sortiePORTA=0x00;initTimer1();// multiplexage toutes les 5 msinitTimer3();// clignotement toutes les 0,5 sprepareAffichage(888);// test des segments : 88.8delay(3000);}voidloop(){temp=mesureTemp();temp_trigger=mesureTrigger();alarme=(temp>temp_trigger);// loop() décide, les timers agissentprepareAffichage(temp);Serial.print("Temp = ");Serial.print(temp/10);Serial.print(".");Serial.print(temp%10);Serial.print(" C | Seuil = ");Serial.print(temp_trigger/10);Serial.print(".");Serial.print(temp_trigger%10);Serial.println(alarme?" C -> ALARME":" C");delay(500);}// Température en dixièmes : N x 1100 / 1024 = mV = dixièmes de °CintmesureTemp(){longN=analogRead(LM35_pin);// long : 1023 x 1100 dépasse un intreturn(int)((N*1100L)/1024L);}// Seuil en dixièmes : 0..1023 -> 0..400 (0,0 à 40,0 °C)intmesureTrigger(){returnmap(analogRead(POT),0,1023,0,400);}// Découpe la valeur en 3 chiffres et prépare les codesvoidprepareAffichage(intnumber){if(number>999)number=999;// 3 digits : 99,9 maxif(number<0)number=0;intdizaine=number/100;intunite=(number/10)%10;intdixieme=number%10;if(dizaine==0)affichage[0]=0x00;// pas de zéro inutileelseaffichage[0]=tabDigit[dizaine];affichage[1]=tabDigit[unite]|0x80;// point décimal (bit 7)affichage[2]=tabDigit[dixieme];}// Timer 1 : CTC, prescaler 64, 1250 ticks de 4 us = 5 msvoidinitTimer1(){noInterrupts();TCCR1A=0;TCCR1B=0;TCNT1=0;OCR1A=1249;TCCR1B|=(1<<WGM12);// mode CTCTCCR1B|=(1<<CS11)|(1<<CS10);// prescaler 64TIMSK1|=(1<<OCIE1A);// autorise l'interruptioninterrupts();}// Timer 3 : CTC, prescaler 256, 31250 ticks de 16 us = 0,5 svoidinitTimer3(){noInterrupts();TCCR3A=0;TCCR3B=0;TCNT3=0;OCR3A=31249;TCCR3B|=(1<<WGM32);// mode CTCTCCR3B|=(1<<CS32);// prescaler 256TIMSK3|=(1<<OCIE3A);interrupts();}// Multiplexage : un afficheur à chaque appel (toutes les 5 ms)ISR(TIMER1_COMPA_vect){digitalWrite(DIG_D2,HIGH);// 1) tout éteindredigitalWrite(DIG_D3,HIGH);digitalWrite(DIG_D4,HIGH);PORTA=affichage[digitCourant];// 2) segments du chiffredigitalWrite(pinDigits[digitCourant],LOW);// 3) allumer cet afficheurdigitCourant++;// 4) le suivant au prochain appelif(digitCourant>=3)digitCourant=0;}// Clignotement : bascule la LED toutes les 0,5 s si alarmeISR(TIMER3_COMPA_vect){if(alarme)digitalWrite(LED,!digitalRead(LED));elsedigitalWrite(LED,LOW);}
للتعمق أكثر
استبدل المصباح بجرس صوتي للحصول على منبه مسموع.
أضف هامشًا (تخلفًا) حتى لا يشتغل المنبه وينطفئ باستمرار قرب القيمة المحددة.
احفظ قيمة المنبه في الذاكرة الدائمة (EEPROM) حتى بعد انقطاع الكهرباء.
أرسل درجة الحرارة إلى الهاتف، كأول لبنة في مشروع المنزل الذكي King House.