0

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-config and 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=1 and dtoverlay=pi3-disable-bt to the /boot/config.txt file.
  • Deleted the line console=/dev/ttyS0,115200 in the /boot/cmdline.txt file.
  • Did both systemctl stop [email protected] and systemctl 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.

2
  • It is difficult to understand WHY you would have done this or what OS you are using. Try a fresh install and don't fiddle with boot files.
    – Milliways
    Commented Oct 22, 2021 at 10:31
  • I'm using 'Python 3.7' on Raspberry Pi OS Codename 'Buster'.
    – Marc
    Commented Oct 25, 2021 at 11:03

1 Answer 1

1

Start again with a fresh install.

  1. Use raspi-config to enable serial.
  2. Use /dev/serial0 in your script.
3
  • I have done a fresh install and what you have suggested. However I still get the same results. Thanks for the suggestion
    – Marc
    Commented Oct 25, 2021 at 7:49
  • Please edit your post and show the software listing you are using.
    – joan
    Commented Oct 25, 2021 at 7:50
  • 1
    @Marc All relevant information should be edited into your question.
    – joan
    Commented Oct 25, 2021 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.