[fpc-pascal] Reading AM2302 temp/humid sensor on RaspberryPi?

R0b0t1 r030t1 at gmail.com
Thu Aug 23 07:33:05 CEST 2018


On Wed, Aug 22, 2018 at 4:19 PM, Bo Berglund <bo.berglund at gmail.com> wrote:
> I wonder if there are any demo projects around for reading the AM2302
> DHT sensor on a RaspberryPi through FreePascal?
> Any links to sample code would be appreciated.
> I have used it on an ESP-07 WiFi module (C language and Arduino
> environment) but now I would like to get it going on an RPi using
> FPC/Lazarus.
>

Can you briefly describe its protocol? Does it look anything like
Dallas/Maxim Semi 1-wire or I2C, or is it something else? If it is
either of those the I2C or SPI peripheral likely could do it. Even if
not the SPI peripheral may work.

It looks like pulse length modulation as used in IR receivers. Some
SPI peripherals have a mode that will read this; in other cases, you
can set the clock speed to the GCD of all signalling times and receive
a bitstream that maps to high/low intervals.


If you need to go DIY I think you will be able to easily, without
kernel modifications or writing kernelspace code. You should be able
to sleep for microseconds using nanosleep(2) (which is used by
usleep(3)). I think I read that for small values nanosleep(2) should
busy loop instead of yielding to the scheduler.  Or perhaps not.

I usually like to stick a small ARM chip somewhere (used to be an AVR)
to handle these things as the hardware is usually more configurable.

Cheers,
    R0b0t1



More information about the fpc-pascal mailing list