CuriousOne
- 22nd December 2024, 20:38
Hello.
I have this sensor and I want to read CO2 reading from it using PBP.
According to manual, it can be done either via PWM or serial interface.
PWM works, I checked it with scope and pulse width changes according to CO2 value.
I tried to capture it with PULSIN statement, and sometimes it works a bit, sometimes - not.
first, reading loop looks like this:
32768
actual value
32768
actual value
32768
actual value
and so on
I would "filter" out 32768 quite easily, but sometimes, strange values being measured - like say, average reading is around 6000 at the moment, but there might be some measurements that are below 500 or above 40000 - for 1-2 loop iterations.
These are not sensor issues, since I checked with scope and everything works properly, there are no out of spec pulses.
I'm using PIC18F45K80 @64 mhz and here's the code:
#config
CONFIG RETEN = OFF
CONFIG INTOSCSEL = HIGH
CONFIG SOSCSEL = DIG
CONFIG XINST = OFF ;Enabled
CONFIG FOSC = INTIO1
CONFIG PLLCFG = OFF
CONFIG FCMEN = OFF ;Disabled
CONFIG PWRTEN = OFF ;Disabled
CONFIG BOREN = OFF ;Disabled in hardware, SBOREN disabled
CONFIG WDTEN = OFF ;WDT disabled in hardware; SWDTEN bit disabled
CONFIG CANMX = PORTB
CONFIG MCLRE = OFF
#endconfig
OSCTUNE.6 = 1 ; Enable 4x PLL
OSCCON = %01110000
ANCON1=0 'DISABLE ADC D3-D2-D1-D0-
ANCON0=0
ADCON0=0
TRISC=%10100001 'set PORTC
TRISD=%00000000 'set PORTD
TRISB=%00000000 'set PORTB
TRISA=%01000100 'set PORTA
TRISE=%0000000 'set PORTE
define OSC 64
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 5
DEFINE LCD_EREG PORTA
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 850
DEFINE LCD_DATAUS 30
'PORTS
PWIN VAR PORTC.0 'PWM INPUT
'VARIABLES
X VAR word 'TEMP VARIABLE 1
co2check:
PULSIN PWIN,1, X
LCDOUT $FE,$2,"CO2=", DEC X
PAUSE 100
GOTO CO2CHECK
This device also supports serial TX/RX, I tried to read serial data from it, but it does not responds to the serial statement - there are no pulses coming back from the sensor. I'm using this to query the sensor, according to the sensor manual:
https://www.winsen-sensor.com/d/files/MH-Z16.pdf
serout2 PORTA.7, 84,[$FF,$01,$86,0,0,0,0,0,$79]
Of course, I'd prefer serial output to be used, but I'm ok with PULSIN, if there's no other solutions available, but why it behaves so weirdly? connecting wires are short, and sensor outputs clean signal, but PBP detects it wrongly. Maybe this is because PWM is being read irregularly? but how can I change that?, ok, in that code I'll reduce PAUSE to say 1. But in actual code, I have other things to do...
I have this sensor and I want to read CO2 reading from it using PBP.
According to manual, it can be done either via PWM or serial interface.
PWM works, I checked it with scope and pulse width changes according to CO2 value.
I tried to capture it with PULSIN statement, and sometimes it works a bit, sometimes - not.
first, reading loop looks like this:
32768
actual value
32768
actual value
32768
actual value
and so on
I would "filter" out 32768 quite easily, but sometimes, strange values being measured - like say, average reading is around 6000 at the moment, but there might be some measurements that are below 500 or above 40000 - for 1-2 loop iterations.
These are not sensor issues, since I checked with scope and everything works properly, there are no out of spec pulses.
I'm using PIC18F45K80 @64 mhz and here's the code:
#config
CONFIG RETEN = OFF
CONFIG INTOSCSEL = HIGH
CONFIG SOSCSEL = DIG
CONFIG XINST = OFF ;Enabled
CONFIG FOSC = INTIO1
CONFIG PLLCFG = OFF
CONFIG FCMEN = OFF ;Disabled
CONFIG PWRTEN = OFF ;Disabled
CONFIG BOREN = OFF ;Disabled in hardware, SBOREN disabled
CONFIG WDTEN = OFF ;WDT disabled in hardware; SWDTEN bit disabled
CONFIG CANMX = PORTB
CONFIG MCLRE = OFF
#endconfig
OSCTUNE.6 = 1 ; Enable 4x PLL
OSCCON = %01110000
ANCON1=0 'DISABLE ADC D3-D2-D1-D0-
ANCON0=0
ADCON0=0
TRISC=%10100001 'set PORTC
TRISD=%00000000 'set PORTD
TRISB=%00000000 'set PORTB
TRISA=%01000100 'set PORTA
TRISE=%0000000 'set PORTE
define OSC 64
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 5
DEFINE LCD_EREG PORTA
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 850
DEFINE LCD_DATAUS 30
'PORTS
PWIN VAR PORTC.0 'PWM INPUT
'VARIABLES
X VAR word 'TEMP VARIABLE 1
co2check:
PULSIN PWIN,1, X
LCDOUT $FE,$2,"CO2=", DEC X
PAUSE 100
GOTO CO2CHECK
This device also supports serial TX/RX, I tried to read serial data from it, but it does not responds to the serial statement - there are no pulses coming back from the sensor. I'm using this to query the sensor, according to the sensor manual:
https://www.winsen-sensor.com/d/files/MH-Z16.pdf
serout2 PORTA.7, 84,[$FF,$01,$86,0,0,0,0,0,$79]
Of course, I'd prefer serial output to be used, but I'm ok with PULSIN, if there's no other solutions available, but why it behaves so weirdly? connecting wires are short, and sensor outputs clean signal, but PBP detects it wrongly. Maybe this is because PWM is being read irregularly? but how can I change that?, ok, in that code I'll reduce PAUSE to say 1. But in actual code, I have other things to do...