How to do real time Plot with delaying measurement from MQ7 sensor?

Hi all,

I am using Arduino Uno and Python' matplotlib to do real time plotting. It works on BMP280 sensor.

But now there is a problem, I am trying MQ7 sensor that needs delaying time to measure the CO.
Combining with MQ135 that does not need delaying time.

I just want to ask whether my .ino code or .py code is correct? Because when I run the python code, the pyplot GUI does not show up. What to do with sensor that has delaying time to measure data?

 int R0 = 6692;
 int R2 = 1000;
 int sensorPin = A0;  
 int MQ7sensorValue = 0;
 int sensorValue = 0; 

 float PPM_CO2;
 float PPM_CO;
 float RS;
 float MQ7_CO;

void setup() {  
  // initialize the serial port  
  Serial.begin(115200);  
   
  }

void loop() {  
int sensorValue135 = analogRead(A2);  
  float volts = sensorValue135 * 5;
  volts = volts / 1023;
  RS = R2 * (1-volts);
  RS = RS/volts; 
  
  PPM_CO2 = 470.627063 - 330.033*(RS/R0);
  PPM_CO = 155.1505 - 129.2524*(RS/R0);
  Serial.print(PPM_CO2);
  Serial.print(",");
  analogWrite(LED_BUILTIN, 255);  // turn the heater fully on  
  delay(60000);            
 // now reduce the heating power  
  analogWrite(LED_BUILTIN, 72);  // turn the heater to approx 1,4V  
  delay(90000);            

  digitalWrite(LED_BUILTIN, HIGH);  
  delay (50);   
  
  MQ7_CO = analogRead(sensorPin);  
  Serial.print(MQ7_CO);
 }

The python code:

# https://toptechboy.com/python-with-arduino-lesson-11-plotting-and-graphing-live-data-from-arduino-with-matplotlib/
# https://python-forum.io/thread-32161.html

import serial # import Serial Library
import numpy  # Import numpy
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *

PPM_CO2=[]
MQ7_CO= []

arduinoData = serial.Serial('/dev/ttyACM0', 115200) #Creating our serial object named arduinoData
plt.ion() #Tell matplotlib you want interactive mode to plot live data
cnt=0

def makeFig(): #Create a function that makes our desired plot
    plt.ylim(23,25)                              
    plt.title('MQ7 and MQ135 Live Streaming Sensor Data')     
    plt.grid(True)                               
    plt.ylabel('CO2')                            
    plt.plot(PPM_CO2, 'ro-', label='CO2 (from MQ135)')    
    plt.legend(loc='upper left')                    
    plt2=plt.twinx()                                
    plt.ylim(600,800)                          
    plt2.plot(MQ7_CO, 'b^-', label='CO (from MQ7)')
    plt2.set_ylabel('CO')                    
    plt2.ticklabel_format(useOffset=False)         
    plt2.legend(loc='upper right')                 

while True: # While loop that loops forever
    while (arduinoData.inWaiting()==0): 
        pass 
    arduinoString = arduinoData.readline().decode('ascii')
    # print(arduinoString)	 	 
    dataArray = arduinoString.split(',')
    co2 = float(dataArray[0])            
    co = float(dataArray[1])  
    PPM_CO2.append(co2)            
    MQ7_CO.append(co)                
    drawnow(makeFig)                      
    plt.pause(.000001)                    
    cnt=cnt+1
    
if(cnt>50):
	PPM_CO2.pop(0)
	MQ7_CO.pop(0)
	

It's okay, I have found the solution.

The naming should not contain underline _

thus change all PPM_CO2 into PPMCO2 and MQ7_CO into MQ7CO.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.