This is my first pi project so I'm a bit new at this. I'm trying to connect a senseair K-30 CO2 sensor to the pi via the UART gpio pins. This is the sensor I am using: https://www.co2meter.com/products/k-30-co2-sensor-module
and I set it up following the app note here: http://www.co2meters.com/Documentation/AppNotes/AN137-K30-sensor-raspberry-pi-uart.pdf
I have done some changes to try to get it to work in python3
However, the co2 comes up with a error message. If I use /dev/ttyS0 I get the error message. Traceback (most recent call last): File "K-30_3.py", line 18, in high = ord(resp[3]) IndexError: index out of range. But if I use /dev/ttyAMA0 it seems to do nothing.
What I've done so far:
- Went into
raspi-configand under interfacing options, changed "Would you like a login shell to be accessible over serial?" to "No" and "Would you like the serial port hardware to be enabled" to "Yes" - Added the line
enable_uart=1anddtoverlay=pi3-disable-btto the/boot/config.txtfile. - Deleted the line
console=/dev/ttyS0,115200in the/boot/cmdline.txtfile. - Did both
systemctl stop [email protected]andsystemctl disable [email protected].
I'm still a little confused and any help would be much appreciated!
Here is what the sensor uses:
#rpi serial connections #Python app to run a K-30 Sensor import serial import time ser = serial.Serial("/dev/ttyS0",baudrate =9600,timeout = .5) print (" AN-137: Raspberryu Pi3 to K-30 Via UART\n") ser.flushInput() time.sleep(1,21) for i in range(1): ser.flushInput() ser.write(b"\xFE\x44\x00\x08\x02\x9F\x25") time.sleep(.5) resp = ser.read(7) high = ord(resp[3]) low = ord(resp[4]) co2 = (high*256) + low print ("i = ",i, " CO2 = " +str(co2)) time.sleep(.1) And the output:
AN- 137: Raspberry Pi3 to K-30 via UART Traceback (most recent call last): File "K-30_3.py", line 18, in <module> high = ord(resp[3]) IndexError: index out of range However, if I change ser = serial.Serial("/dev/ttyS0",baudrate =9600,timeout = .5) to ser = serial.Serial("/dev/ttyAMA0",baudrate =9600,timeout = .5)
The output is this:
AN- 137: Raspberry Pi3 to K-30 via UART It is either doing nothing or is hanging.
I'm using Python 3.7 on Raspberry Pi OS Codename Buster.