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 “Smart City”. 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.
The K30 is a low-cost, low-maintenance CO2 sensor capable of interfacing with a Raspberry Pi. It is designed to accurately measure ambient CO2 levels over long periods of time. The K30 detects CO2 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 and NO2 can cause cross sensitivity with CO2.
Used in conjunction with a Raspberry Pi, the K30 is perfect for remote monitoring of CO2 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.
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:
$ sudo nano /boot/config.txt
And appending the file with:
enable_uart=1
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:
$ sudo nano /boot/config.txt
The allocation will look something like:
“console=ttyAMA0,115200”
Alternatively, this can be disabled through the interface options in the raspberry configuration menu:
sudo raspi-config
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’s GPIOs.
To test the K30 sensor, a prototype as developed, using a Raspberry Pi, the sensor, and some LEDs. CO2 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 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 levels for an office, and the sensor clearly respond to an increase in CO2 levels.
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.
More information on the K30: https://www.co2meter.com/products/k-30-co2-sensor-module
Full Python code:
#rpi serial connections #Python app to run a K-30 Sensor import serial import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) GPIO.setup(16, GPIO.OUT) ser = serial.Serial("/dev/ttyS0", baudrate=9600, timeout = 1) #serial port may vary from pi to pi print ("AN-137: Raspberry Pi3 to K-30 Via UART\n") ser.flushInput() time.sleep(1) cutoff_ppm = 1000 #readings above this will cause the led to turn red for i in range(1,101): ser.flushInput() ser.write("\xFE\x44\x00\x08\x02\x9F\x25") time.sleep(.5) resp = ser.read(7) #print (type(resp)) #used for testing and debugging #print (len(resp)) #print (resp) #print ("this is resp" + resp) high = ord(resp[3]) low = ord(resp[4]) print(high) print(low) co2 = (high*256) + low print ("i = ",i, " CO2 = " +str(co2)) GPIO.output(12, GPIO.LOW) GPIO.output(16, GPIO.LOW) if co2 > cutoff_ppm: GPIO.output(12, GPIO.HIGH) else: GPIO.output(16, GPIO.HIGH) time.sleep(.1)
Fantastic! Thanks a lot, sir!
Perfect! Great job!
but I don’t know how to convert this code for Python3 (unicode is not supported)
Hi Antares,
I’m afraid I don’t have access to a sensor to test but I think it’s probably line 17 that is causing you grief.
I think all you need to do to fix this line is change it to
ser.write(b”\xFE\x44\x00\x08\x02\x9F\x25″)
Lines 24 & 25 may also need changing, but without the hardware I can’t test.