kaluma-project / kaluma

A tiny JavaScript runtime for RP2040 (Raspberry Pi Pico)
https://kalumajs.org
Apache License 2.0
647 stars 37 forks source link

pulseRead does not work with DHT #630

Closed ZIMkaRU closed 10 months ago

ZIMkaRU commented 11 months ago

Looks like pulseRead does not work with DHT22


used the last release https://github.com/kaluma-project/kaluma/releases/tag/1.1.0-beta.4 checked with this lib https://github.com/niklauslee/dht made simple example:

const { DHT } = require('dht')

const pin = 15
const dht = new DHT(pin, DHT.DHT22)

setInterval(() => {
  const res = dht.read()
  console.log('[DHT res]:', res)
  console.log(`Temp: ${dht.temperature} C`)
  console.log(`Humidity: ${dht.humidity} %`)
}, 5000)

and pulseRead always returns null: https://github.com/niklauslee/dht/blob/main/index.js#L74 I connected an oscilloscope to the signal pin of the sensor to make sure it works trigger pulses are processed successfully and the sensor returns a data packet but it looks like reading is not happening it seems the reading pin of the board is INPUT_PULLUP mode and can't be reconfigured to INPUT_PULLDOWN

Screenshot from 2023-12-21 09-19-56

Screenshot from 2023-12-21 09-20-48

Screenshot from 2023-12-21 09-21-36

Screenshot from 2023-12-21 09-23-23


I think I'm not the only one having this problem https://github.com/niklauslee/dht/issues/2

ZIMkaRU commented 11 months ago

Update: the issue is in the trigger section of the pulseRead when the last trigger impulse is set to down then input is pulled up I was able to fetch bits when added a third trigger value of 20

var bits = pulseRead(this.pin, 100, {
  timeout: 25000,
  startState: LOW,
  mode: INPUT,
  trigger: {
    startState: HIGH,
    interval: [10000, 18000, 20],
  },
});

https://github.com/niklauslee/dht/blob/main/index.js#L80

ZIMkaRU commented 11 months ago

@communix several thoughts: maybe when we set the INPUT mode after the trigger https://github.com/kaluma-project/kaluma/blob/c3071d6868a000607349aa29126f5a32c02c0f19/src/global.c#L276 we should set false for gpio_set_dir(pin, false); before setting gpio_set_input_enabled(pin, true); https://github.com/kaluma-project/kaluma/blob/c3071d6868a000607349aa29126f5a32c02c0f19/targets/rp2/src/gpio.c#L73 in order to disable OUTPUT mode before setting INPUT one https://github.com/kaluma-project/kaluma/blob/c3071d6868a000607349aa29126f5a32c02c0f19/targets/rp2/src/gpio.c#L66

ZIMkaRU commented 11 months ago

I made this fix: https://github.com/kaluma-project/kaluma/pull/631 and built a beta release on my own: kaluma-rp2-pico-1.1.0-beta.4.zip works good

communix commented 11 months ago

@ZIMkaRU Thank you for your contribution. Your code looks good and it's merged. Thanks to your code, I have a chance to review GPIO input/output code and add more code to fix this issue. Please try it with the latest code in the master branch.

ZIMkaRU commented 11 months ago

@communix I've built and tested many versions of the combinations for gpio_set_dir/gpio_set_input_enabled and it works only if remove gpio_set_input_enabled(pin, false) which is placed after gpio_set_dir(pin, GPIO_OUT) for KM_GPIO_IO_MODE_OUTPUT mode https://github.com/kaluma-project/kaluma/blob/master/targets/rp2/src/gpio.c#L67

communix commented 11 months ago

@ZIMkaRU Thank you. Let me revert my change. Please check #633 and close this issue.

ZIMkaRU commented 10 months ago

Sure, thanks

ZIMkaRU commented 10 months ago

checked, works well