MATLAB Answers

Accurate Measurements with DHT11 and Arduino via MATLAB

90 views (last 30 days)
B on 10 May 2013
Commented: Philip Nahmias on 24 Jul 2018
I have a DHT11 sensor connected to an Arduino UNO. I can get consistent (and expected) data values through the Arduino software by following this tutorial. However, I'd like to replicate the same data collection with MATLAB.
I'm stuck on how I can take the DHT11's digital signal and "break it down" into its component bits so that they can be identified and used properly. From my understanding, the DHT11's signal is 40 bits, and is set up so that the first 16 relate to the humidity detected, the following 16 are for temperature, and the last 8 are the checksum.
Is it possible to separate a digital signal into a collection of bits? How can I use MATLAB to get measurements from this sensor?


B on 14 May 2013
I have since been able to gather HEX data from the DHT11 by using serial commands. My current code is:
a = serial('COM3');
a.RecordDetail = 'verbose';
a.RecordName = 'rawdata.txt';
for i = 1:2
When I open 'rawdata.txt' I have entries listed as
< 512 uchar values.
44 48 54 78 78 20 74 65 73 74 21 0d 0a 48 75 6d
69 64 69 74 ...
The second entry starts with the 21st value of the first entry (in the above set, the second entry starts where the ... is). The third entry (if it were collected, follows the same pattern).
B on 16 May 2013
As an update, I've managed to get some code to work and have had some very positive results. Using MATLAB documentation for serial port devices I realized that I'd have to adjust the input buffer size properly, and found that in this case 41 works nicely. Also, in using the char(fread(s)) command I was able to convert the decimal data to text, and have it displayed as "Humidity: 35.00 % Temperature: 24.00 *C".
s = serial('COM3');
idn = fscanf(s);
textdata = char(fread(s));
text = textdata';
clear s
Ishaan Chandratreya
Ishaan Chandratreya on 24 Sep 2017
I am wishing to read DHT11 using a raspberry pi on MATLAB. Could anyone help me with that? I have already tried reading GPIO pin as sensor transmits data, and have tried connecting rx to data, and using serialdev of raspberry pi package

Sign in to comment.

Accepted Answer

B on 17 May 2013
My final code, for readings once a minute:
s = serial('COM3'); %Connected to COM3
set(s,'Timeout', 65); %sensor will timeout after 65 seconds
x = 1:1:60; %readings over the span of one hour
for i=1:length(x)
y(i) = fscanf(s, '%f', 6);
z(i) = fscanf(s, '%f', 6);
% Plot data from arduino
title('temperature v. humidity v. time');

  1 Comment

Philip Nahmias
Philip Nahmias on 24 Jul 2018
I am faced with a similar situation like you and I am a bit confused on how you were able to resolve your situation. First of all did you have an arduino package on matlab or did you just define the connection as serial? Cause I was under the impression I had to download some arduino package and I haven't managed to make it work.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by