[PYTHON] Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)

Purpose

Since the microorganisms under study may be alive using trace components in the atmosphere, we will create a culture device that can add them.

Enable UART on the Raspberry Pi side

-UART communication with Raspberrypi 3 (console & general purpose)Measure carbon dioxide concentration with Raspberry Pi 3 Model B + (MH-Z14A) -AWS IoT is also available- Was referred to.

sudo raspi-config ->"5 Interfacing Option" ->"P6 Serial"

You will be asked if you want to log in to the shell serially, so select "No". "Would you like a login shell to be accessible over serial?" -> "No"

You will be asked if you want to enable the serial port, so select "Yes". "Would you like the serial port hardware to be enabled?" -> "Yes"

Select Finish to restart.

continue

$ whoami
pi
$ ls -la /dev/ttyS0
crw-rw---- 1 root dialout 4,64 November 29 10:42 /dev/ttyS0
$ sudo gpasswd -a pi dialout

Module created to use MHZ-14A

Measure carbon dioxide concentration with Raspberry Pi 3 Model B + (MH-Z14A) -AWS IoT is also available- Was referred to.

MH-Z14A NDIR CO2 SENSOR FOR CARBON DIOXIDE DETECTION Since the data sheet was updated, I referred to the latest one. The following five commands are set for this sensor.

  1. Measurement of CO2 concentration
  2. Zero point calibration
  3. Span calibration
  4. On / off of automatic calibration function
  5. Change the measurement range This function is not used this time because a standard gas (or the atmosphere is used as a CO2 concentration of 400 ppm) is required for calibration. I put in commands to measure CO2 concentration, turn off automatic calibration, and change the measurement range. Automatic calibration is a function that automatically performs zero point calibration once every 24 hours, and it is recommended not to use this function when using it in greenhouses, farms, and refrigerators.

When reading the referenced code ・ Definition of class and meaning of init and self ・ How to use pyserial -Making a module and meaning ʻif name =='main': ` I didn't know much about such things, so I made something that works for the time being, but I'm not confident in the accuracy of the code.

I saved the created mhz14a.py in /usr/lib/python2.7/dist-packages/. Reference; 3rd edition p195

mhz14a.py


import serial
import time

class MHZ14A():
    PACKET = [0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79]
    RANGE1 = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x07, 0xd0, 0x8F]
    RANGE2 = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB]
    RANGE3 = [0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x27, 0x10, 0x2F]
    AUTOCALON = [0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6]
    AUTOCALOFF = [0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86]

    def __init__(self, ser):
        self.serial = serial.Serial(ser, 9600, timeout=1)
        time.sleep(2)

    def get(self):
        self.serial.write(bytearray(MHZ14A.PACKET))
        res = self.serial.read(size=9)
        res = bytearray(res)
        checksum = 0xff & (~(res[1] + res[2] + res[3] + res[4] + res[5] + res[6] + res[7]) + 1)
        if res[8] == checksum:
            return (res[2] << 8|res[3])
        else:
            raise Exception("checksum: " + hex(checksum))

    def close(self):
        self.serial.close()

#Acquires and returns the CO2 concentration from the sensor
def main():
    sensor = MHZ14A("/dev/ttyS0")
    try:
        return (int(sensor.get()))
    except:
        pass
    sensor.close()

#Self-On / off of calibration function(No return value)
def autocal(x):
    sensor = MHZ14A("/dev/ttyS0")
    if x == 0:
        sensor.serial.write(bytearray(MHZ14A.AUTOCALOFF))
    elif x == 1:
        sensor.serial.write(bytearray(MHZ14A.AUTOCALON))
    sensor.close()

#Send a command to change the measurement range(No return value)
def range(y):
    sensor = MHZ14A("/dev/ttyS0")
    if y == 1:
        sensor.serial.write(bytearray(MHZ14A.RANGE1))
    elif y == 2:
        sensor.serial.write(bytearray(MHZ14A.RANGE2))
    elif y == 3:
        sensor.serial.write(bytearray(MHZ14A.RANGE3))
    sensor.close

if __name__ == '__main__':
    main()

・ Main () returns the sensor value ・ Autocal (x) is the automatic calibration function on (1) off (0) ・ Range (y) has 3 ways of changing the measurement range, 0 ~ 2000, 0 ~ 5000, 0 ~ 10000, and the initial setting of the machine is 5000.

Acquires CO2 and turns the solenoid valve on and off

I wrote it with reference to the code of L Chika. The automatic calibration function is turned off and the measurement range is set to 0 to 10000ppm.

import RPi.GPIO as GPIO
from time import sleep
import datetime
import mhz14a as MHZ

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

MHZ.autocal(0) #self-Turn off calibration function
MHZ.range(3) #Measurement range is 0~Set to 10000ppm

try:
    while True:
        ppm = MHZ.main() #Get the value of the sensor
        if ppm < 9500: #CO2 concentration that opens the solenoid valve
            GPIO.output(18, True)
            sleep(0.2) #Time to open solenoid valve(Seconds)
            GPIO.output(18, False)
            sleep(0.8)
        else:
            sleep(1)
        print(ppm, datetime.datetime.now()) #Display CO2 concentration and date / time
        sleep(60) #Measurement interval(Seconds)
except KeyboardInterrupt:
    pass

GPIO.cleanup()
print("stop")

Solenoid valve on / off

・ Solenoid valve JPMV22NC (bought at Amazon) ・ 12V power supply attached to solenoid valve ・ DC-DC converter SUS1R50505 ・ OMRON G5V-2 (If it is not this size, it will not stick to the breadboard) ・ Transistor S8050 (I bought it in a pack with an assortment of Amazon) ・ 1000Ω resistor (bought with Amazon assortment pack) ・ Diode (bought in Amazon's assortment pack) When I tried to draw a circuit diagram, Firitzing was charged, so I took a direct shot on the breadboard. At first, the solenoid valve didn't work and I couldn't understand everything, so I connected the 5V GND to the Raspberry Pi's GND and it worked. It's difficult ... IMG_3483_2.png

I tried to move it for the time being

IMG_3485.png As shown in the photo, the sensor, CO2 outlet and fan are packed in a tapper, lightly covered (not completely sealed), and moved overnight. The result is the graph below.

co2.png Since the upper limit of the sensor is 10000ppm, I'm shaking it off, but I think I can maintain around 10000ppm. Speaking of greed, I want a measurement range up to about 10%, but there is nothing affordable.

Future tasks

・ Save log -Added hydrogen, methane, and carbon monoxide sensors (I bought the sensor itself, but AD conversion is required) → Trace gas incubator

Recommended Posts

Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
Create a color sensor using a Raspberry Pi and a camera
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Using a webcam with Raspberry Pi
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a Kanji display compass with Raspberry Pi and Sense Hat
[Raspberry Pi] Add a thermometer and a hygrometer
Make Raspberry Pi speak Japanese using OpenJtalk
Make a wash-drying timer with a Raspberry Pi
Try using a QR code on a Raspberry Pi
Create a visitor notification system using Raspberry Pi
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Let's make a simple game with Python 3 and iPhone
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Make a Sato Yohei discriminator using OpenCV and TensorFlow
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
Make an autonomous driving robot car with Raspberry Pi3 B + and ultrasonic distance sensor HC-SR04
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
Create a web surveillance camera with Raspberry Pi and OpenCV
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Create a partition and then install the Raspberry Pi OS
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to automate [a certain task] using Raspberry Pi
Get the weather using the API and let the Raspberry Pi speak!
I tried to make a simple text editor using PyQt
Indoor monitoring using Raspberry Pi
Finally ... Make a radio control using python on Raspberry Pi 3! (When the magnet is brought closer, the motor moves and stops automatically)
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
"2/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
Control the motor with a motor driver using python on Raspberry Pi 3!
Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Make a simple OMR (mark sheet reader) with Python and OpenCV
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Weighing instrument using raspberry pi and hx711 (GUI display on Tkinter)
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Finally ... Make a radio control using python on Raspberry Pi 3! (The motor moves while the button is pressed)
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
Learn Zundokokiyoshi using a simple RNN
Make a face recognizer using TensorFlow
Creating a simple table using prettytable
Try using ArUco on Raspberry Pi
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Initial settings for using GrovePi + starter kit and camera on Raspberry Pi
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
I want to make a web application using React and Python flask
Finally ... Make a radio control using python on Raspberry Pi 3! (The motor moves while the magnet is brought closer)