Arduino Utilisation du protocope I2C

Utilisation du protocope I2C


Voici un tutoriel un peu plus complet cette fois. Toujours à base du TCN75A, l’idée est ici de tester le protocole I2C. En effet, celui ci permet de mettre en parallèle plusieurs périphériques.

Nous allons donc mettre un écran LCD, le capteur de température et une horloge Tiny RTC, les trois fonctionnant avec le protocole I2C.

1. Pré requis

a. Librairies utilisées :

Wire.h : référence Arduinoexplications en Français

TCN75A.h : Github

RTClib.h : Github

U8glib.h : Wiki de la librairie

b. Connaissance utile :

Le tutoriel n°1 permet d’appréhender l’utilisation du capteur de température Tcn75A via le protocole I2C.

2. Objectif

Afficher sur l’écran les informations des capteurs; Un message qui défile de gauche à droite, date/heure et température relevée. Par défaut l’écran est éteint.

Un clic sur le bouton permettra d’afficher les informations à l’écran. Au bout de 5s l’écran s’éteint. Si avant la fin des 5s on clic, l’écran s’éteint.

On entend par clic la pression et le relâchement du bouton.

Afin de réaliser cette gestion, nous allons mettre en place un petit automate (Les automates évitent bien des problèmes liés à des événements).

3. Matériel utilisé

  • Arduino Uno Rev3
  • Capteur de température TCN75A : Datasheet
  • 0,96 pouces LCD OLED de 128 x 64 série I2C SPI IIC
  • Horloge Tiny RTC
  • Résistance de 10K Ohms et de 220 Ohms
  • 1 bouton poussoir
  • Quelques cables

4. Schéma

5. 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%3CRTClib.h%3E%20%2F%2F%20Librairie%20pour%20l%E2%80%99utilisation%20de%20l%E2%80%99horloge%20temps%20r%C3%A9el.%0A%23include%20%3CU8glib.h%3E%20%2F%2F%20Librairie%20pour%20l%E2%80%99utilisation%20des%20%C3%A9crans%20LCD%0A%23include%20%3CTCN75A.h%3E%20%2F%2F%20Librairie%20pour%20le%20capteur%20de%20temp%C3%A9rature%0A%0A%2F*%20DEFINITION%20DES%20CONSTANTES%20*%2F%0A%23define%20BTN_LIGHTLCD%202%20%2F%2F%20D%C3%A9finition%20du%20bouton%20d%E2%80%99allumage%20de%20l%E2%80%99%C3%A9cran%20LCD%0A%23define%20LIGHTON_DURATION%205000%20%2F%2F%20Dur%C3%A9e%20en%20milliseconde%20de%20l%E2%80%99affichage%20de%20l%E2%80%99%C3%A9cran%0A%0A%2F*%20DECLARATION%20DES%20VARIABLES%20GLOBALES%20*%2F%0A%2F%2F%20D%C3%A9claration%20des%20diff%C3%A9rents%20composants%0ARTC_DS1307%20cp_Timer%3B%20%2F%2F%20D%C3%A9finition%20de%20l%E2%80%99objet%20Composant%20Timer%20Tiny%20RTC%0AU8GLIB_SSD1306_128X64%20cp_Lcd(U8G_I2C_OPT_NONE%20%7C%20U8G_I2C_OPT_DEV_0)%3B%20%2F%2F%20D%C3%A9finition%20de%20l%E2%80%99%C3%A9cran%0ATCN75A%20cp_Temp%3B%20%2F%2F%20D%C3%A9finition%20du%20capteur%20de%20temp%C3%A9rature%0A%0A%2F%2F%20Variables%20d%E2%80%99animations%20%3A%20Scroll%20horizontal%0Aint%20m_xoffset%20%3D%200%3B%20%2F%2F%20Position%20en%20X%20de%20l%E2%80%99affichage%20du%20message.%0Aint%20m_inc%20%3D%202%3B%20%2F%2F%20Pas%20d%E2%80%99incr%C3%A9ment%20pour%20la%20position%20du%20message.%0A%0A%2F%2F%20Variable%20d%E2%80%99%C3%A9tat%20du%20bouton%20et%20de%20mesure%20du%20temps%0Aint%20m_stateLightLcd%3B%20%2F%2F%20Variable%20d%E2%80%99%C3%A9tat%20de%20l%E2%80%99automate%0Aint%20m_btnLightLcdState%3B%20%2F%2F%20Variable%20d%E2%80%99%C3%A9tat%20du%20bouton%20poussoir%0Along%20m_diffTime%3B%20%2F%2F%20Diff%C3%A9rence%20temporelle%20pour%20mesure%20la%20dur%C3%A9e%20d%E2%80%99affichage.%0Abool%20m_isTurnOnLcd%20%3D%20false%3B%20%2F%2F%20Permet%20de%20d%C3%A9finir%20si%20l%E2%80%99%C3%A9cran%20est%20allum%C3%A9. » message= »Partie déclarative » highlight= » » provider= »manual »/]

 

[pastacode lang= »cpp » manual= »%2F*%20Nom%20%3A%20SetLcdInfos%0ADescription%20%3A%20Permet%20d%E2%80%99afficher%20les%20%C3%A9l%C3%A9ments%20sur%20l%E2%80%99%C3%A9cran%20LCD.%0AParam%C3%A8tre%20%3A%20p_mes%20%3D%3E%20Message%20%C3%A0%20afficher%20et%20qui%20scroll.%0Ap_date%20%3D%3E%20Date%0Ap_temp%20%3D%3E%20temp%C3%A9rature%0ARetour%20%3A%20aucun.%0A*%2F%0Avoid%20SetLcdInfos(String%20p_mes%2C%20DateTime%20p_date%2C%20float%20p_temp)%0A%7B%0A%09%2F%2F%20Boucle%20d%E2%80%99affichage%0A%09cp_Lcd.firstPage()%3B%0A%09do%20%7B%0A%09%09cp_Lcd.setPrintPos(m_xoffset%2C%2010)%3B%0A%09%09cp_Lcd.print(p_mes)%3B%20%2F%2F%20Affichage%20du%20message%20(Anim%C3%A9).%0A%09%09cp_Lcd.setPrintPos(0%2C%2025)%3B%0A%09%09cp_Lcd.print(FormatDate(p_date%2C%201))%3B%2F%2F%20Affichage%20de%20la%20date%0A%09%09cp_Lcd.setPrintPos(0%2C%2040)%3B%0A%09%09cp_Lcd.print(%C2%AB%20Il%20est%20%20%C2%BB%20%2B%20FormatDate(p_date%2C%202))%3B%2F%2F%20Affichage%20de%20l%E2%80%99heure%0A%09%09cp_Lcd.setPrintPos(0%2C%2055)%3B%0A%09%09cp_Lcd.print(%C2%AB%20Il%20fait%20%20%C2%BB%20%2B%20FormatTemp(p_temp))%3B%20%2F%2F%20Affichage%20de%20la%20temp%C3%A9rature%0A%09%7D%20while%20(cp_Lcd.nextPage())%3B%0A%0A%09%2F%2F%20Calcul%20de%20l%E2%80%99offset%20pour%20le%20scroll%0A%09m_xoffset%20%3D%20m_xoffset%20%2B%20m_inc%3B%0A%09if%20(m_xoffset%20%3C%3D%20-128%20%7C%7C%20m_xoffset%20%3E%3D%20128)%0A%09m_inc%20%3D%20-m_inc%3B%0A%7D%0A%0A%2F*%20Nom%20%3A%20SetLcdInfos%0ADescription%20%3A%20Automate%20permettant%20la%20gestion%20de%20l%E2%80%99affichage%20%C3%A0%20partir%20du%20bouton.%0AUn%20clic%20%3D%20Appuyer%20et%20relacher.%0AClic%20%3D%3E%20Allumage%20%C3%A9cran%20pour%20LIGHTON_DURATION%20secondes%0AClic%20pendant%20l%E2%80%99allumage%20%3D%3E%20Eteindre%20l%E2%80%99%C3%A9cran%0ALes%20%C3%A9tats.%0A0%20%3A%20Etat%20de%20base%20%C3%A9cran%20%C3%A9teint%0A1%20%3A%20Etat%20appuy%C3%A9%20sur%20bouton%0A2%20%3A%20Etat%20relache%20du%20bouton%0AParam%C3%A8tre%20%3A%20aucun.%0ARetour%20%3A%20aucun.%0A*%2F%0Avoid%20CheckBtn()%0A%7B%0A%09long%20v_dif%3B%0A%09m_btnLightLcdState%20%3D%20digitalRead(BTN_LIGHTLCD)%3B%20%2F%2FRappel%20%3A%20bouton%20%3D%202%0A%0A%09if%20(m_btnLightLcdState%20%3D%3D%20HIGH)%0A%09%7B%0A%09%09if(m_stateLightLcd%20%3D%3D%200)%0A%09%09%09m_stateLightLcd%20%3D%201%3B%0A%09%7D%0A%09else%0A%09%7B%0A%09%09if(m_stateLightLcd%20%3D%3D%201)%0A%09%09%09m_stateLightLcd%20%3D%202%3B%0A%09%7D%0A%0A%09if%20(m_stateLightLcd%20%3D%3D%202)%0A%09%7B%0A%09%09m_isTurnOnLcd%20%3D%20!m_isTurnOnLcd%3B%0A%09%09m_stateLightLcd%20%3D%200%3B%0A%09%09v_dif%20%3D%20millis()%20%E2%80%93%20m_diffTime%3B%0A%09%09m_diffTime%20%3D%20millis()%3B%0A%09%7D%0A%09if(m_stateLightLcd%20%3D%3D%200)%0A%09%09v_dif%20%3D%20millis()%20%E2%80%93%20m_diffTime%3B%0A%0A%09if%20(m_isTurnOnLcd)%0A%09%09if%20(v_dif%20%3E%3D%20LIGHTON_DURATION)%0A%09%09%09m_isTurnOnLcd%20%3D%20false%3B%0A%09if%20(m_isTurnOnLcd)%0A%09%09Serial.print(%C2%AB%20Allume%20%C2%AB%20)%3B%0A%09else%0A%09%09Serial.print(%C2%AB%20Etient%20%C2%AB%20)%3B%0A%09Serial.print(%C2%AB%20Depuis%20%C2%AB%20)%3B%0A%09Serial.print(v_dif%20%2F%201000)%3B%0A%09Serial.println(%C2%AB%20s%20%C2%BB)%3B%0A%7D » message= »Quelques méthodes » highlight= » » provider= »manual »/]

[pastacode lang= »cpp » manual= »void%20setup(void)%20%7B%0A%09%2F%2F%20On%20init%20le%20port%20s%C3%A9rie%20pour%20avoir%20une%20console.%0A%09Serial.begin(9600)%3B%0A%0A%09%2F%2F%20Init%20du%20port%20I2C%0A%09Wire.begin()%3B%0A%0A%09%2F%2F%20Init%20Tiny%20RTC%20pour%20obtenir%20l%E2%80%99horloge%0A%09cp_Timer.begin()%3B%0A%09if%20(!cp_Timer.isrunning())%20%7B%0A%09%09Serial.println(%C2%AB%20RTC%20is%20NOT%20running!%20%C2%BB)%3B%0A%09%09%2F%2F%20Cette%20ligne%20permet%20de%20d%C3%A9finir%20la%20date%20et%20l%E2%80%99heure%20du%20Tin%20RTC%20si%20celle%20ci%20n%E2%80%99a%20jamais%20%C3%A9t%C3%A9%20initialis%C3%A9.%0A%09%09%2F%2F%20Attention%20%C3%A0%20ne%20pas%20le%20faire%20fr%C3%A9quemment%2C%20en%20effet%20le%20nombre%20d%E2%80%99%C3%A9criture%20est%20limit%C3%A9%20(esp%C3%A9rance%20de%20vie%20du%20composant).%0A%09%09cp_Timer.adjust(DateTime(__DATE__%2C%20__TIME__))%3B%0A%09%7D%0A%0A%09%2F%2F%20Init%20du%20capteur%20de%20temp%C3%A9rature%0A%09cp_Temp.begin()%3B%0A%09cp_Temp.set_address(0)%3B%0A%09cp_Temp.set_resolution(3)%3B%0A%0A%09%2F%2F%20Init%20de%20l%E2%80%99%C3%A9cran%20LCD%0A%09cp_Lcd.sleepOff()%3B%0A%09cp_Lcd.setFont(u8g_font_unifont)%3B%20%2F%2F%20D%C3%A9finition%20de%20la%20fonte%20par%20d%C3%A9faut.%0A%09if%20(cp_Lcd.getMode()%20%3D%3D%20U8G_MODE_R3G3B2)%20%7B%0A%09%09cp_Lcd.setColorIndex(255)%3B%20%2F%2F%20Blanc%0A%09%7D%0A%09else%20if%20(cp_Lcd.getMode()%20%3D%3D%20U8G_MODE_GRAY2BIT)%20%7B%0A%09%09cp_Lcd.setColorIndex(3)%3B%20%2F%2F%20intensit%C3%A9%20maximum%0A%09%7D%0A%09else%20if%20(cp_Lcd.getMode()%20%3D%3D%20U8G_MODE_BW)%20%7B%0A%09%09cp_Lcd.setColorIndex(1)%3B%20%2F%2F%20pixel%20on%0A%09%7D%0A%09else%20if%20(cp_Lcd.getMode()%20%3D%3D%20U8G_MODE_HICOLOR)%20%7B%0A%09%09cp_Lcd.setHiColorByRGB(255%2C%20255%2C%20255)%3B%0A%09%7D%0A%0A%09%2F%2F%20Mise%20en%20place%20du%20bouton%20d%E2%80%99allumage%20de%20l%E2%80%99%C3%A9cran%0A%09pinMode(BTN_LIGHTLCD%2C%20INPUT)%3B%0A%0A%09m_stateLightLcd%20%3D%200%3B%0A%09m_diffTime%20%3D%20millis()%3B%0A%7D » message= »Le setup » highlight= » » provider= »manual »/]

[pastacode lang= »cpp » manual= »void%20loop(void)%20%7B%0A%09%2F%2F%20On%20r%C3%A9cup%C3%A8re%20la%20date%20et%20l%E2%80%99heure%0A%09DateTime%20v_now%20%3D%20cp_Timer.now()%3B%0A%0A%09%2F%2F%20R%C3%A9cup%C3%A9ration%20de%20la%20temp%C3%A9rature%0A%09float%20v_t%20%3D%20cp_Temp.read()%3B%0A%0A%09%2F%2F%20Ecriture%20des%20informations%20sur%20le%20port%20S%C3%A9rie%0A%09SetSerialInfos(v_now%2C%20v_t)%3B%0A%0A%09%2F%2F%20Ecriture%20des%20informations%20sur%20l%E2%80%99%C3%A9cran%20Lcd%0A%09if%20(m_isTurnOnLcd)%0A%09%7B%0A%09%09cp_Lcd.sleepOff()%3B%0A%09%09SetLcdInfos(%C2%AB%20Bonjour%20Seb%20%C2%BB%2C%20v_now%2C%20v_t)%3B%0A%09%7D%0A%09else%0A%09%09cp_Lcd.sleepOn()%3B%0A%0A%09%2F%2F%20Automate%20de%20gestion%20du%20bouton.%0A%09CheckBtn()%3B%0A%7D » message= »Le loop » highlight= » » provider= »manual »/]

b. Source à télécharger

Tutoriel n°2 le .ino

6. Résultat

En Image

En vidéo

Étiquettes : , , , ,

Leave a Reply

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.

Related Post