Created
February 4, 2021 12:24
-
-
Save UedaTakeyuki/fdfebac33a3264220f1dd779657b32c7 to your computer and use it in GitHub Desktop.
Read MH_Z19 PWM with RPi gpio 12.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf8 | |
import RPi.GPIO as GPIO | |
import time | |
class GPIO_Edge_Timeout(Exception): | |
pass | |
CYCLE_START_HIGHT_TIME = 2 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(12,GPIO.IN) | |
channel = GPIO.wait_for_edge(12, GPIO.FALLING, timeout=2000) | |
if channel is None: | |
raise GPIO_Edge_Timeout("gpio 12 edge timeout") | |
channel = GPIO.wait_for_edge(12,GPIO.RISING, timeout=2000) | |
if channel is None: | |
raise GPIO_Edge_Timeout("gpio 12 edge timeout") | |
else: | |
rising = int(time.time() * 1000) | |
channel = GPIO.wait_for_edge(12, GPIO.FALLING, timeout=2000) | |
if channel is None: | |
raise GPIO_Edge_Timeout("gpio 12 edge timeout") | |
else: | |
falling = int(time.time() * 1000) | |
print(rising) | |
co2 = (falling -rising - CYCLE_START_HIGHT_TIME) / 2 | |
print ("co2", co2) | |
print ("range2000", co2*(2000/500)) | |
print ("range5000", co2*(5000/500)) | |
print ("range10000", co2*(10000/500)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment