MATLAB Answers

B
0

Accurate Measurements with DHT11 and Arduino via MATLAB

Asked by B
on 10 May 2013
Latest activity Commented on by 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?

  3 Comments

I have since been able to gather HEX data from the DHT11 by using serial commands. My current code is:
a = serial('COM3');
fopen(a);
a.RecordDetail = 'verbose';
a.RecordName = 'rawdata.txt';
record(a,'on');
for i = 1:2
fread(a);
end
record(a,'off');
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).
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');
set(s,'BaudRate',9600,'InputBufferSize',41);
fopen(s);
fprintf(s,'*IDN?')
idn = fscanf(s);
textdata = char(fread(s));
text = textdata';
fclose(s);
delete(s)
clear s
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.

1 Answer

Answer by B
on 17 May 2013
 Accepted Answer

My final code, for readings once a minute:
s = serial('COM3'); %Connected to COM3
set(s,'BaudRate',9600);
set(s,'Timeout', 65); %sensor will timeout after 65 seconds
fopen(s);
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);
i
end
fclose(s);
% Plot data from arduino
plot3(x,y,z)
title('temperature v. humidity v. time');
xlabel('time');
ylabel('humidity');
zlabel('temperature');

  1 Comment

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.