Skip to content

Instantly share code, notes, and snippets.

@UedaTakeyuki
Created February 4, 2021 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UedaTakeyuki/fdfebac33a3264220f1dd779657b32c7 to your computer and use it in GitHub Desktop.
Save UedaTakeyuki/fdfebac33a3264220f1dd779657b32c7 to your computer and use it in GitHub Desktop.
Read MH_Z19 PWM with RPi gpio 12.
# 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