Skip to content

Instantly share code, notes, and snippets.

@eddiewebb
Created July 7, 2022 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiewebb/3a347c82c5942a49ceefe6e5bdcec3dc to your computer and use it in GitHub Desktop.
Save eddiewebb/3a347c82c5942a49ceefe6e5bdcec3dc to your computer and use it in GitHub Desktop.
"Fix" DHT Sensor reliability on Raspberry Pi

What

A 'fix' for the notoriously poor reading issues of DHT hygrometer sensors from Python / Raspberry Pis.

Why

I've used the cheap DHT sensors on a few projects and always find the "yeah they're unreliable, just keep trying" and that sucks. How do opther devices work using these sensors? Priority.

So these process changes have, for me, worked to give my python process the realtime CPU & IO access to meet the strict timing requirements of a DHT sensor.

Super useful: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/monitoring_and_managing_system_status_and_performance/tuning-scheduling-policy_monitoring-and-managing-system-status-and-performance

How

Just add these cpu & io priority settings to your systemd file.
You may be able to lower priority but since I use dedicated zeros I want all the priority possible.


[Service]
...
#give cpu scheduling priority to help timing issues reading DHT sensor
CPUSchedulingPolicy=fifo # fifo or rr are 2 realtime options
CPUSchedulingPriority=99 #highest
Nice=-20 # highest
IOSchedulingClass=realtime # only realtime option
IOSchedulingPriority=0 #highest

[Install]
WantedBy=multi-user.target

Even when using read_retries for the DHT Adafruit library I saw success rate jump from ~60% to 90%+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment