2

This is my first pi project so im a bit new at this. Im 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

However, the co2 measurements show up as 0ppm even if I breathe on the sensor membrane so my thoughts are that the Tx and Rx pins are not reading anything from the sensor.

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 to the /boot/config.txt file.
- Deleted the line console=/dev/ttyS0,115200 in the /boot/cmdline.txt file.
- Did both systemctl stop serial-getty@ttyS0.service and systemctl disable serial-getty@ttyS0.service
- I also had to change some permissions in order for me to be able to use ttyS0, I added the user to the groups dialout, tty and uucp.
- I tried checking the pins by making a feedback loop and using minicom or screen to check if they are reading properly but im unsure of how to use either of them so it did not help

Any pointers on how to short the Tx and Rx pins together and use minicom to see if they're working? Are there other permissions I need to change in order to use the serial pins to communicate with the sensor? It stopped giving me the error saying permissions denied but possibly it can read but not write or execute? Im a little confused and any help would be much appreciated!

Here is the code that 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: Raspberry Pi3 to K-30 Via UART\n"
ser.flushInput()
time.sleep(1)

for i in range(1,21):

 ser.flushInput()
 ser.write("\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
i =   1  CO2 = 0
i =   2  CO2 = 0
i =   3  CO2 = 0
i =   4  CO2 = 0
i =   5  CO2 = 0
i =   6  CO2 = 0
i =   7  CO2 = 0
i =   8  CO2 = 0
i =   9  CO2 = 0
i =   10  CO2 = 0
i =   11  CO2 = 0
i =   12  CO2 = 0
i =   13  CO2 = 0
i =   14  CO2 = 0
i =   15  CO2 = 0
i =   16  CO2 = 0
i =   17  CO2 = 0
i =   18  CO2 = 0
i =   19  CO2 = 0
i =   20  CO2 = 0
0

the command being sent to the sensor is not correct.

this command:

ser.write("\xFE\x44\x00\x08\x02\x9F\x25")

says:

  1. use the 'any device' address (broadcast address) of 0xFE
  2. use function code 0x44 (which does not exist, perhaps you meant 0x04
  3. it says to start with the 9th register (register numbers start with 0, 0x00 0x08)
  4. it says to read 2 registers (of 16 bits each) (this field is two bytes, not just one byte, I.E. 0x00 0x02)
  5. then the CRC (which I have not verified, and is probably wrong) is 0x25 0x9F

When communicating on a ModBus,

  1. the first thing to do with the received data is to check the command code to assure it matches the sent command.
  2. the second thing to do with the received data is to check the function code (mostly for error indications, I.E. the returned function code will have 0x80 bit set)
  3. if an error condition is indicated, read the next byte(s) for which error occurred.
  4. the third thing to do is read the data byte count (that tells how many data bytes were actually returned)
  5. the forth thing to do is verify the CRC
  6. the fifth thing to do is extract the register value(s) Which are 2 bytes per register.

You need to read the document that contains the 'modbus' implementation (and restrictions) for the sensor, which is linked to from your first external reference .

the referenced code example is not correct

  • the correct code would also be setting 1 start bit, no parity, 1 stop bit – user3629249 Aug 19 '17 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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