Since the microorganisms under study may be alive using trace components in the atmosphere, we will create a culture device that can add them.
-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
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.
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.
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 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 ...
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.
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.
・ Save log -Added hydrogen, methane, and carbon monoxide sensors (I bought the sensor itself, but AD conversion is required) → Trace gas incubator