{"id":50,"date":"2017-08-18T15:52:42","date_gmt":"2017-08-18T14:52:42","guid":{"rendered":"https:\/\/computenodes.net\/?p=50"},"modified":"2025-01-17T16:03:36","modified_gmt":"2025-01-17T16:03:36","slug":"__trashed-4","status":"publish","type":"post","link":"https:\/\/computenodes.net\/2017\/08\/18\/__trashed-4\/","title":{"rendered":"Interfacing a K30 CO2 sensor with a Raspberry Pi for Remote Air Quality Sensing"},"content":{"rendered":"
Climate change, and the declining air quality in major urban areas is becoming an increasingly prevalent and critical issue, with hundreds of thousands of premature deaths due to air pollution in Europe alone (http:\/\/www.bbc.co.uk\/news\/world-europe-38078488<\/a>). One approach to resolving this problem is a data-driven \u201cSmart City\u201d. So-called Smart Cities employ a huge network of sensors all connected to the internet- known as the Internet of Things (IoT). These sensors are not just restricted to gas sensors, but may detect, noise, traffic, temperature and even rubbish levels in bins. In order to use these devices remotely, networking them cheaply and effectively is critical.<\/p>\n The K30 is a low-cost, low-maintenance CO2<\/sub> sensor capable of interfacing with a Raspberry Pi. It is designed to accurately measure ambient CO2<\/sub> levels over long periods of time. The K30 detects CO2<\/sub> using an infrared source, and an infrared detector. Since different molecules absorb different wavelengths of infrared light, the amount of absorption at certain wavelengths indicates the levels of those molecules. This leads to measurement issues when other molecules absorb light at similar wavelengths. In situations of low concentrations, SO2<\/sub> and NO2<\/sub> can cause cross sensitivity with CO2<\/sub>.<\/p>\n Used in conjunction with a Raspberry Pi, the K30 is perfect for remote monitoring of CO2<\/sub> levels. The Raspberry Pi has many unique qualities that make it perfect for interfacing with the K30 and other sensors. Its respectable compute power, versatile General Purpose Input Output (GPIO) pins, and full network capabilities allow it work with almost any sensor. Combined with its low cost and compact size, it is an excellent choice for most applications. The Raspberry Pi 3 has Wi-Fi and Bluetooth built in which makes sending the data to other devices relatively easy. In our deployment of the K30, remote data communication will achieved using LoRaWAN, a long range, low power network. This will be used to send data to a gateway connected to the internet.<\/p>\n Some setup is required to best utilise the Raspberry Pi to communicate with the K30 Sensor. The K30 communicates over serial, which required the Raspberry Pi to have UART (Universal Asynchronous Receiver Transmitter) enabled. This can be done by editing the boot configuration:<\/p>\n And appending the file with:<\/p>\n By default, the console uses the serial output to output data, which can be used as a way to control the computer headlessly. This should be disabled to allow the serial output to be used by the K30 for data transmission. This can be done by deleting the console serial port allocation in the boot file:<\/p>\n The allocation will look something like:<\/p>\n Alternatively, this can be disabled through the interface options in the raspberry configuration menu:<\/p>\n Once the Pi is prepared to receive serial data, the K30 must be connected. This is a relatively simple case of connecting the K30 to 5v (a separate power supply was used), connecting the Pi and K30 to ground, and then cross-connecting the serial connections. A simple LED circuit was also established from the Pi\u2019s GPIOs.<\/p>\n To test the K30 sensor, a prototype as developed, using a Raspberry Pi, the sensor, and some LEDs. CO2<\/sub> readings were read from the K30 using the serial library in Python, which then controlled LEDS via the GPIOs. To visually demonstrate the functionality of the K30, a red LED was turned on when values over 1000 ppm of CO2<\/sub> were detected. Values within the room were typically around 600 ppm, which could easily be raised over the 1000 ppm threshold by exhaling on the sensor. The ambient readings are in line with the expected CO2<\/sub> levels for an office, and the sensor clearly respond to an increase in CO2<\/sub> levels.<\/p>\n The full Python code used is below. Note that the serial port is TTYSO which is standard for the Raspberry Pi model 3. In earlier versions of the Raspberry Pi this may be different.<\/p>\n More information on the K30: https:\/\/www.co2meter.com\/products\/k-30-co2-sensor-module<\/a><\/p>\n Full Python code:<\/p>\n Climate change, and the declining air quality in major urban areas is becoming an increasingly prevalent and critical issue, with hundreds of thousands of premature deaths due to air pollution in Europe alone (http:\/\/www.bbc.co.uk\/news\/world-europe-38078488). One approach to resolving this problem is a data-driven \u201cSmart City\u201d. So-called Smart Cities employ a huge network of sensors all$ sudo nano \/boot\/config.txt<\/h6>\n
enable_uart=1<\/h6>\n
$ sudo nano \/boot\/config.txt<\/h6>\n
\u201cconsole=ttyAMA0,115200\u201d<\/h6>\n
sudo raspi-config<\/h6>\n
\n#rpi serial connections\n#Python app to run a K-30 Sensor\nimport serial\nimport time\nimport RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BOARD)\nGPIO.setup(12, GPIO.OUT)\nGPIO.setup(16, GPIO.OUT)\nser = serial.Serial("\/dev\/ttyS0", baudrate=9600, timeout = 1) #serial port may vary from pi to pi\nprint ("AN-137: Raspberry Pi3 to K-30 Via UART\\n")\nser.flushInput()\ntime.sleep(1)\ncutoff_ppm = 1000 #readings above this will cause the led to turn red\n\nfor i in range(1,101):\n ser.flushInput()\n ser.write("\\xFE\\x44\\x00\\x08\\x02\\x9F\\x25")\n time.sleep(.5)\n resp = ser.read(7)\n #print (type(resp)) #used for testing and debugging\n #print (len(resp))\n #print (resp)\n #print ("this is resp" + resp)\n high = ord(resp[3])\n low = ord(resp[4])\n print(high)\n print(low)\n co2 = (high*256) + low\n print ("i = ",i, " CO2 = " +str(co2))\n GPIO.output(12, GPIO.LOW)\n GPIO.output(16, GPIO.LOW)\n\n if co2 > cutoff_ppm:\n GPIO.output(12, GPIO.HIGH)\n else:\n GPIO.output(16, GPIO.HIGH)\n time.sleep(.1)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"
Continue reading Interfacing a K30 CO2 sensor with a Raspberry Pi for Remote Air Quality Sensing<\/span> <\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,8,6],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-interns","category-raspberry-pi","category-sensors","col-1"],"_links":{"self":[{"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":15,"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/posts\/50\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computenodes.net\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}