[fpc-pascal] Reading AM2302 temp/humid sensor on RaspberryPi?
Bo Berglund
bo.berglund at gmail.com
Thu Aug 23 09:00:07 CEST 2018
On Thu, 23 Aug 2018 00:33:05 -0500, R0b0t1
<r030t1 at gmail.com> wrote:
>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.
This is what the datasheet says (times here are the nominal ones):
1. It uses a 1-wire connection with 5K pullup and open drain drivers.
2. The master requests data by pulling the wire low for 1 ms
3. The sensor then waits 30 us before setting the wire low for 80 us
followed by high for 80 us.
4. Next follows 40 bits of data each composed of:
- Low level for 50 us
- High level for bit value depenednt time:
Bit = 0 time = 26 us
Bit = 1 time = 70 us
5. After the 40 bit transfer the line is held low for 50 us, then
released.
Data are sent as 4 bytes (16 bit humidity, 16 bit temp) followed by
one parity byte. The bytes are sent MSB first.
The DHT driver for Arduino ESP8266 (in C) seems to measure and stuff
pulse widths into an array during the transfer and after the fact
evaluates the bits depending on the recorded widths.
My problem is that I don't see how I could measure the high level
pulse widths....
Possibly a loop where the input level is checked:
function MeasureHighPulse: cardinal;
var
Ts, Te: cardinal;
const
Tmax = 100; //Max allowed pulse length
begin
Ts := TickCountus(); //Need a tickcount with us resolution...
Te := Ts;
while InputPin(Sensorpin) = 1 do
begin
Te := TickCountus();
if Te - Ts > Tmax then
begin
Te := Ts;
break;
end;
end;
Result := Te - Ts;
end;
I will need a higher resolution GetTickCount for this...
The usleep() function is somethingh I have never seen before, but I
guess it "sleeps" for a certain number of microseconds...
--
Bo Berglund
Developer in Sweden
More information about the fpc-pascal
mailing list