Projet code capteur co2

Fermé
daliaza - Modifié le 26 mai 2022 à 22:05
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 - 27 mai 2022 à 22:07
Bonjour, je suis en première année de mesure physique et j'ai un projet a réaliser et rendre avant la fin d'année (début juin). Je dois réaliser un capteur de co2 à l'aide d'un capteur co2 d'une carte micropython d'un écran lcd et de l'application circuit python. Le problème c'est que étant débutante en python je n'arrive pas à construire le programme permettant à l'écran d'afficher le taux de co2 dans une salle en direct. J'ai actuellement toutes les interfaces, il me reste juste à terminer ma boucle while pour finaliser mon projet.

Voici mon programme :

import time
import board
from analogio import AnalogIn
import busio

#entrer analogique de la carte 
analog_in = AnalogIn(board.X11)

#interface lcd rgb
COLUMNS = 16
ROWS    = 2

#Instruction set
CLEARDISPLAY         = 0x01

ENTRYMODESET         = 0x04
ENTRYLEFT            = 0x02
ENTRYRIGHT           = 0x00
ENTRYSHIFTINCREMENT  = 0x01
ENTRYSHIFTDECREMENT  = 0x00

DISPLAYCONTROL       = 0x08
DISPLAYON            = 0x04
DISPLAYOFF           = 0x00
CURSORON             = 0x02
CURSOROFF            = 0x00
BLINKON              = 0x01
BLINKOFF             = 0x00

FUNCTIONSET          = 0x20
_5x10DOTS            = 0x04
_5x8DOTS             = 0x00
_1LINE               = 0x00
_2LINE               = 0x08
_8BITMODE            = 0x10
_4BITMODE            = 0x00

REG_RED              = 0x04
REG_GREEN            = 0x03
REG_BLUE             = 0x02

REG_MODE1            = 0x00
REG_MODE2            = 0x01
REG_OUTPUT           = 0x08

class LCD:
    def __init__(self,i2c):

        self.column = 0
        self.row = 0

        self.address = 62
        self.color = 96


        self.command = bytearray(2)

        self.i2c = i2c

        time.sleep(0.05)

        for i in range(3):
            self._command(FUNCTIONSET | _2LINE )
            time.sleep(0.01)

        self.on()
        self.clear()

        self._command(ENTRYMODESET | ENTRYLEFT | ENTRYSHIFTDECREMENT)

        self.set_cursor(0, 0)

        self.command[0] = REG_MODE1
        self.command[1] = 0
        self.i2c.writeto(self.color, self.command)
        time.sleep(0.01)

        self.command[0] = REG_OUTPUT
        self.command[1] = 0xFF
        self.i2c.writeto(self.color, self.command)
        time.sleep(0.01)

        self.command[0] = REG_MODE2
        self.command[1] = 0x20
        self.i2c.writeto(self.color, self.command)
        time.sleep(0.01)


    def on(self, cursor=False, blink=False):
        if cursor == False and blink == False:
            self._command(DISPLAYCONTROL | DISPLAYON | CURSOROFF | BLINKOFF)
        elif cursor == False and blink == True:
            self._command(DISPLAYCONTROL | DISPLAYON | CURSOROFF | BLINKON)
        elif cursor == True and blink == False:
            self._command(DISPLAYCONTROL | DISPLAYON | CURSORON | BLINKOFF)
        elif cursor == True and blink == True:
            self._command(DISPLAYCONTROL | DISPLAYON | CURSORON | BLINKON)

    def off(self):
        self._command(DISPLAYCONTROL | DISPLAYOFF | CURSOROFF | BLINKOFF)

    def clear(self):
        self._command(CLEARDISPLAY)
        self.set_cursor(0, 0)


    def set_cursor(self, column, row):
        column = column % COLUMNS
        row = row % ROWS
        if row == 0:
            command = column | 0x80
        else:
            command = column | 0xC0
        self.row = row
        self.column = column
        self._command(command)

    def write(self, s):
        for i in range(len(s)):
            time.sleep(0.01)
            self.i2c.writeto(self.address, b'\x40'+s[i])
            self.column = self.column + 1
            if self.column >= COLUMNS:
                self.set_cursor(0, self.row+1)

    def _command(self, value):
        self.command[0] = 0x80
        self.command[1] = value
        self.i2c.writeto(self.address, self.command)
        time.sleep(0.01)

    def setRGB(self,r,g,b):
        self.command[0] = REG_RED
        self.command[1] = r
        self.i2c.writeto(self.color, self.command)
        time.sleep(0.01)

        self.command[0] = REG_GREEN
        self.command[1] = g
        self.i2c.writeto(self.color, self.command)
        time.sleep(0.01)

        self.command[0] = REG_BLUE
        self.command[1] = b
        self.i2c.writeto(self.color, self.command)
        time.sleep(0.01)

    def setWhite(self):
        self.setRGB(255,255,255)


i2c = busio.I2C(board.SCL,board.SDA)

while not i2c.try_lock():
    pass

finally:
    i2c.unlock()

#boucle finale 
while True:
    print(("CO2:",((get_voltage(analog_in) * 5000 / 5.2,"ppm"))))
    time.sleep(0.1)
    #initialisation du lcd 
    myLCD = LCD(i2c)
    myLCD.on()
    myLCD.setWhite()
    myLCD.set_cursor(0,0)
    myLCD.write(("CO2: ",((get_voltage(analog_in) * 5000 / 5.2,"ppm"))))
    time.sleep(1)
    
    #condition taux de co2 
    if PPM > 1000 :
        myLCD.setRGB(255,0,0)     # ROUGE
    elif  PPM >= 800 and PPM <= 1000 :        
        myLCD.setRGB(255,165,0)     # ORANGE 
    elif PPM >= 660  and PPM < 800 :                          
        myLCD.setRGB(0,255,0)     # VERT
    elif PPM < 660 :            
        myLCD.setRGB(255,255,0)     # JAUNE




Cordialement
ZD.
EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI

Merci d'y penser dans tes prochains messages.
A voir également:

2 réponses

yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477
27 mai 2022 à 14:36
bonjour,
ton programme fonctionne bien, sans erreur?
0
Oui j’ai mis tous ce qui est initialisation avant la boucle la seule chose qu’il manque c’est que l’écran affiche le taux.
0
Bonjour,
Je viens de le modifier légèrement j’ai mis tout ce qui est initialisation de l’écran avant la boucle.
Le programme fonctionne il y a le taux de ppm qui s’affiche sur circuit python, le seul problème c’est que je n’arrive ps à faire afficher le taux de co2 sur l’écran.
0
brucine Messages postés 14331 Date d'inscription lundi 22 février 2021 Statut Membre Dernière intervention 27 avril 2024 1 816
27 mai 2022 à 15:10
Bonjour,

Juste en passant pour t'éviter de te faire incendier par ton prof: on ne parle pas davantage de taux de pm que de taux de glycémie, c'est redondant puisque par définition ppm est déjà un taux.

On peut parler du nombre de ppm ou du taux de CO2 exprimé en ppm quand une ambiguïté existe quant à une autre unité de mesure (par exemple molarité).

C'est de plus, quand le contexte n'en est pas évident, une unité très vicieuse parce que ne décrivant que 10^-6 mais on ne sait pas de quoi: nombre de molécules (comme ici), mg/kg (c'est la mode pour le fluor des dentifrices), ml/m3?
0
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477
27 mai 2022 à 16:11
print(("CO2:",((get_voltage(analog_in) * 5000 / 5.2,"ppm"))))
myLCD.write(("CO2: ",((get_voltage(analog_in) * 5000 / 5.2,"ppm"))))


Veux-tu dire que la première instruction fonctionne bien, la seconde pas?

As-tu essayé
myLCD.write("bonjour")
?

Peut-être:
message= "CO2: " + str(get_voltage(analog_in) * 5000 / 5.2 + " ppm")
print(message)
myLCD.write(message)
0
Daliaza > yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024
27 mai 2022 à 19:45
J’ai essayé myLCD.write(“bonjours”) ça m”affiche bonjours sur l’écran mais concernant les voltages ça me met toujours erreur je pense que c’est Parcque la fonction myLCD.write n’accepte que des chaînes de caractères et pas des valeurs.
0
Daliaza > brucine Messages postés 14331 Date d'inscription lundi 22 février 2021 Statut Membre Dernière intervention 27 avril 2024
27 mai 2022 à 19:45
Ahh je ne savais pas merci !
0
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477 > Daliaza
27 mai 2022 à 20:21
as-tu testé ma suggestion avec "message"?
0