1. Pré requis
a. Librairies utilisées :
Wire.h : référence Arduino, explications en Français
TCN75A.h : Github
LiquidCrystal.h : référence Arduino
b. Connaissance utile :
Avoir réalisé quelques tutoriels de base (Allumage d’une led, …). Avoir pris connaissance du protocole I2C.
2. Objectif
Mesurer avec l’Arduino la température provenant du capteur de température et l’afficher sur l’écran LCD.
3. Matériel utilisé
- Arduino Uno Rev3
- Capteur de température TCN75A
- Ecran LCD 16×2.
4. Schéma
Programme
a. Extrait de code
[pastacode lang= »cpp » manual= »%23include%20%3CWire.h%3E%20%2F%2F%20librairie%20utilisant%20le%20protocole%20I2C%20%2F%20TWI%20(communication%20s%C3%A9rie%20sur%202%20fils)%0A%23include%20%3CLiquidCrystal.h%3E%20%2F%2F%20librairie%20pour%20l’%C3%A9cran%20LCD%0A%0A%2F*%20DEFINITION%20DES%20CONSTANTES%20*%2F%0A%23define%20tcn75address%200×48%20%2F%2F%20Adresse%20I2C%20du%20capteur%20de%20temp%C3%A9rature%0A%23define%20SERIAL_BAUDRATE%209600%20%2F%2F%20Vitesse%20de%20communication%20du%20port%20s%C3%A9rie%0A%0Aconst%20byte%20RSPIN%20%3D%2012%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0Aconst%20byte%20RWPIN%20%3D%2011%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0Aconst%20byte%20EPIN%20%3D%2013%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0A%0Aconst%20byte%20DB4%20%3D%204%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0Aconst%20byte%20DB5%20%3D%205%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0Aconst%20byte%20DB6%20%3D%206%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0Aconst%20byte%20DB7%20%3D%207%3B%20%2F%2F%20Port%20num%C3%A9rique%20utilis%C3%A9%20pour%20l’%C3%A9cran%20LCD%0A%0A%2F*%20DECLARATION%20DES%20VARIABLES%20GLOBALES%20*%2F%0ALiquidCrystal%20lcd(RSPIN%2C%20RWPIN%2C%20EPIN%2C%20DB4%2C%20DB5%2C%20DB6%2C%20DB7)%3B%20%2F%2Fliaison%204%20bits%20de%20donn%C3%A9es%0A%0Abyte%20m_seconds%3B%20%2F%2F%20D%C3%A9claration%20des%20variables%20du%20Timer%2C%0Abyte%20m_minutes%3B%20%2F%2F%20notez%20le%20m_%20pour%20d%C3%A9finir%20qu’il%20s’agit%0Abyte%20m_hours%3B%20%2F%2F%20d’une%20variable%20membre%0A%0Afloat%20m_Temp%3B%20%2F%2F%20Variable%20contenant%20la%20temp%C3%A9rature.%0Achar*%20m_time%20%3D%20(char*)malloc(13)%3B%20%2F%2F%20Stockage%20de%20la%20dur%C3%A9e%20de%20fonctionnement%20DDDDD%20HH%3AMM%3ASS.III%0A%0A%2F%2F%20Definition%20du%20caractere%20%C2%B0%20qui%20n’existe%20pas%20de%20base%0Abyte%20degrees%5B8%5D%20%3D%20%7B%0AB00000%2C%0AB01000%2C%0AB10100%2C%0AB01000%2C%0AB00000%2C%0AB00000%2C%0AB00000%2C%0AB00000%2C%0A%7D%3B » message= »Partie déclaration » highlight= » » provider= »manual »/]
[pastacode lang= »cpp » manual= »%2F*%20Nom%20%3A%20GetTemp%0ADescription%20%3A%20Permet%20d’obtenir%20la%20temp%C3%A9rature.%0ARetour%20%3A%20Float%20%3D%3E%20Temp%C3%A9rature%20mesur%C3%A9e.%0A*%2F%0Afloat%20GetTemp()%0A%7B%0A%09int%20tempreg%20%3D%200%3B%0A%09float%20temperature%20%3D%200%3B%0A%09Wire.requestFrom(tcn75address%2C%202)%3B%0A%09tempreg%20%3D%20Wire.read()%3B%20%2F%2F%20R%C3%A9cup%C3%A9ration%20de%20l’information%0A%09tempreg%20%3D%20tempreg%20%3C%3C%208%3B%20%2F%2F%20Conversion%20et%20calcul%20de%20la%20valeur%20en%20%C2%B0C%0A%09tempreg%20%7C%3D%20Wire.read()%3B%0A%09tempreg%20%3D%20tempreg%20%3E%3E%204%3B%0A%09temperature%20%3D%20(float)tempreg%20%2F%2016%3B%0A%09return%20temperature%3B%0A%7D%0A%0A%2F*%20Nom%20%3A%20GetTime%0ADescription%20%3A%20Permet%20d’obtenir%20le%20d%C3%A9lai%20depuis%20lequel%20l’arduino%20fonctionne.%0ARetour%20%3A%20char*%20%3D%3E%20Chaine%20de%20caract%C3%A8re%20contenant%20le%20d%C3%A9lai%20au%20format%20HH%3AMM%3ASS.III%0A*%2F%0Achar*%20GetTime()%0A%7B%0A%09%2F%2F%20Note%20c’est%20possible%20de%20d%C3%A9tecter%20le%20passage%20%C3%A0%20z%C3%A9ro%20de%20milli.%20Suffit%20d’avoir%20not%C3%A9%20que%20sa%20valeur%20est%20inf.%20%C3%A0%20des%20valeurs%20stock%C3%A9es.%0A%09%2F%2F%20D%C3%A9claration%20des%20variables%20locales%0A%09const%20byte%20ASCII_NUM%20%3D%2048%3B%0A%09unsigned%20long%20v_delay%3B%20%2F%2F%20Nombre%20de%20millisecondes.%20Notez%20que%20les%20variables%20locales%20sont%20pr%C3%A9fix%C3%A9s%20v_%0A%09byte%20v_seconds%3B%20%2F%2F%20Nombre%20de%20secondes%20calcul%C3%A9es%0A%09byte%20v_minutes%3B%20%2F%2F%20Nombre%20de%20minutes%20calcul%C3%A9es%0A%09byte%20v_hours%3B%20%2F%2F%20Nombre%20d’heures%20calcul%C3%A9es%0A%09unsigned%20int%20v_days%3B%20%2F%2F%20Nombre%20de%20jours%20calcul%C3%A9s%0A%09unsigned%20int%20v_millis%3B%0A%0A%09%2F%2F%20Allons%20chercher%20l’info%20en%20milliseconde%2C%20puis%20calculons%20les%20variables%0A%09v_delay%20%3D%20millis()%3B%0A%09v_millis%20%3D%20v_delay%20%25%201000%3B%20%2F%2F%20Test%20de%20remise%20%C3%A0%20z%C3%A9ro%0A%09v_seconds%20%3D%20(v_delay%20%2F%201000)%20%25%2060%3B%0A%09v_minutes%20%3D%20((v_delay%20%2F%201000)%20%2F%2060)%20%25%2060%3B%0A%09v_hours%20%3D%20(((v_delay%20%2F%201000)%20%2F%2060)%20%2F%2060)%20%25%2060%3B%0A%09v_days%20%3D%20(((v_delay%20%2F%201000)%20%2F%2060)%20%2F%2060)%20%2F%2024%3B%20%2F%2F%20Si%20vous%20voulez%20rajouter%20le%20nombre%20de%20jours.%0A%0A%09%2F%2F%20Formatage%20du%20d%C3%A9lai%0A%09m_time%5B0%5D%20%3D%20ASCII_NUM%20%2B%20v_hours%20%2F%2010%3B%0A%09m_time%5B1%5D%20%3D%20ASCII_NUM%20%2B%20v_hours%20%25%2010%3B%0A%09m_time%5B2%5D%20%3D%20’%3A’%3B%0A%09m_time%5B3%5D%20%3D%20ASCII_NUM%20%2B%20v_minutes%20%2F%2010%3B%0A%09m_time%5B4%5D%20%3D%20ASCII_NUM%20%2B%20v_minutes%20%25%2010%3B%0A%09m_time%5B5%5D%20%3D%20’%3A’%3B%0A%09m_time%5B6%5D%20%3D%20ASCII_NUM%20%2B%20v_seconds%20%2F%2010%3B%0A%09m_time%5B7%5D%20%3D%20ASCII_NUM%20%2B%20v_seconds%20%25%2010%3B%0A%09m_time%5B8%5D%20%3D%20′.’%3B%0A%09m_time%5B9%5D%20%3D%20ASCII_NUM%20%2B%20v_millis%20%2F%20100%3B%0A%09m_time%5B10%5D%20%3D%20ASCII_NUM%20%2B%20(v_millis%20%25%20100)%20%2F%2010%3B%0A%09m_time%5B11%5D%20%3D%20ASCII_NUM%20%2B%20v_millis%20%25%2010%3B%0A%09m_time%5B12%5D%20%3D%20’%5C0’%3B%0A%09return%20m_time%3B%0A%7D » message= »Méthodes spécifiques » highlight= » » provider= »manual »/]
[pastacode lang= »cpp » manual= »void%20setup()%0A%7B%0A%09Wire.begin()%3B%20%2F%2F%20Initialisation%20de%20la%20com%20I2C%0A%09Wire.beginTransmission(tcn75address)%3B%20%2F%2F%20Initialisation%20de%20la%20mesure%20de%20temp%C3%A9rature%0A%09Wire.write(0x01)%3B%20%2F%2F%20D%C3%A9finition%20de%20la%20r%C3%A9solution%0A%09Wire.write(0x60)%3B%0A%09Wire.endTransmission()%3B%20%2F%2F%20Fin%20de%20Commande%0A%0A%09Wire.beginTransmission(tcn75address)%3B%20%2F%2F%20On%20%C3%A9tabli%20la%20communication%0A%09Wire.write(0x00)%3B%0A%09Wire.endTransmission()%3B%20%2F%2F%20Fin%20de%20Commande%0A%0A%09lcd.begin(16%2C%202)%3B%20%2F%2F%20Init%20de%20l’%C3%A9cran%2016%20colonnes%20et%202%20lignes%0A%09lcd.clear()%3B%20%2F%2F%20Vidage%20de%20l’%C3%A9cran.%0A%09lcd.createChar(0%2C%20degrees)%3B%20%2F%2F%20initialiser%20le%20caract%C3%A8re%20%C2%B0%20dans%20le%20LCD%0A%0A%09m_hours%20%3D%200%3B%20%2F%2F%20Init%20des%20variables%20temporelles%0A%09m_minutes%20%3D%200%3B%0A%09m_seconds%20%3D%200%3B%0A%0A%09Serial.begin(SERIAL_BAUDRATE)%3B%20%2F%2F%20Initialisation%20de%20la%20communication%20s%C3%A9rie%0A%7D » message= »Le Setup » highlight= » » provider= »manual »/]
[pastacode lang= »cpp » manual= »void%20loop()%0A%7B%0A%09lcd.setCursor(0%2C%200)%3B%0A%09lcd.print(GetTime())%3B%20%2F%2F%20Affichage%20du%20temps%20depuis%20lequel%20l’arduino%20fonctionne%0A%09lcd.setCursor(0%2C%201)%3B%20%2F%2F%20Positionnement%20%C3%A0%20la%20seconde%20ligne%0A%09m_Temp%20%3D%20GetTemp()%3B%20%2F%2F%20Acquisition%20de%20la%20temp%C3%A9rature%0A%09lcd.print(m_Temp)%3B%20lcd.write(byte(0))%3B%20lcd.print(%22C%22)%3B%20%2F%2F%20Affichage%20de%20la%20temp%C3%A9rature.%0A%0A%09delay(500)%3B%20%2F%2F%20On%20renouvelera%20l’op%C3%A9ration%20dans%20500%20ms.%0A%7D » message= »La boucle Loop » highlight= » » provider= »manual »/]
b. Source à télécharger
6. Résultat
En vidéo