Components and supplies
Capacitor 10 µF
Jumper wires (generic)
Pushbutton switch 12mm
Resistor 1k ohm
Resistor 10k ohm
Breadboard (generic)
Arduino UNO
DHT22 Temperature Sensor
Apps and platforms
Arduino IDE
MATLAB
Project description
Code
humCorr.m
matlab
MATLAB function used to correct the humidity readings from the value of one sensor. Not called if lines 66-68 of the main script DHT22_5sensor.m are commented. Ignore or edit this function according to your needs.
1function corrFact = humCorr(rh5,sensNum) 2% Humidity correction factor from measurement of sensors 1,2,3,4,5 3% rh5 is the relative humidity of sensor number 5 4% sensNum is the sensor number (1 to 5) 5% 6% Data to interpolate: 7% RH5 30.843 1.008 1.007 1.065 1.011 1.000 8% RH5 53.775 1.077 1.150 1.136 1.059 1.000 9 10data = [ 36.992 1.033 1.056 1.114 1.040 1.000;... 11 62.333 1.060 1.115 1.170 1.066 1.000]; 12 13% check on input 14if sensNum ~= 1 && sensNum ~= 2 && sensNum ~= 3 && sensNum ~= 4 && sensNum ~= 5 15 error('sensNum must be 1 or 2 or 3 or 4 or 5') 16end 17 18deltaX = data(2,1) - data(1,1); % scalar 19deltaY = data(2,2:end) - data(1,2:end); % array 1D 20m = deltaY/deltaX; % slope of the linear interpolant 21 22corrFact = data(1,sensNum+1) + m(sensNum) * (rh5 - data(1,1)); 23
DHT22_5sensor.ino
arduino
Arduino sketch to read sensors data and pass them to the computer by the serial port.
1// Libraries 2#include <DHT.h> 3 4 5// Initialize DHT sensor for normal 16mhz Arduino 6DHT dht1(2, DHT22); 7DHT dht2(4, DHT22); 8DHT dht3(7, DHT22); 9DHT dht4(8, DHT22); 10DHT dht5(12, DHT22); 11 12int const stopPin = 6; 13int stopFlag = 0; 14 15 16// Variables 17float hum; //Stores humidity value 18float temp; //Stores temperature value 19 20void setup() 21{ 22 pinMode(stopPin, INPUT); 23 Serial.begin(9600); 24 dht1.begin(); 25 dht2.begin(); 26 dht3.begin(); 27 dht4.begin(); 28 dht5.begin(); 29} 30 31void loop() 32{ 33 delay(2000); 34 35 // Read humidity from the 5 sensors and display it on a single line 36 hum = dht1.readHumidity(); 37 Serial.print(hum); 38 Serial.print(":"); 39 hum = dht2.readHumidity(); 40 Serial.print(hum); 41 Serial.print(":"); 42 hum = dht3.readHumidity(); 43 Serial.print(hum); 44 Serial.print(":"); 45 hum = dht4.readHumidity(); 46 Serial.print(hum); 47 Serial.print(":"); 48 hum = dht5.readHumidity(); 49 Serial.print(hum); 50 Serial.print(":"); 51 52 // Read temperature from the 5 sensors and display it on a single line 53 temp = dht1.readTemperature(); 54 Serial.print(temp); 55 Serial.print(":"); 56 temp = dht2.readTemperature(); 57 Serial.print(temp); 58 Serial.print(":"); 59 temp = dht3.readTemperature(); 60 Serial.print(temp); 61 Serial.print(":"); 62 temp = dht4.readTemperature(); 63 Serial.print(temp); 64 Serial.print(":"); 65 temp = dht5.readTemperature(); 66 67 // Check stop condition 68 stopFlag = digitalRead(stopPin); 69 if (stopFlag == LOW){ 70 Serial.println(temp); 71 } 72 else{ 73 Serial.print(temp); 74 Serial.println(":999"); 75 } 76 77} 78
humCorr.m
matlab
MATLAB function used to correct the humidity readings from the value of one sensor. Not called if lines 66-68 of the main script DHT22_5sensor.m are commented. Ignore or edit this function according to your needs.
1function corrFact = humCorr(rh5,sensNum) 2% Humidity correction factor from measurement of sensors 1,2,3,4,5 3% rh5 is the relative humidity of sensor number 5 4% sensNum is the sensor number (1 to 5) 5% 6% Data to interpolate: 7% RH5 30.843 1.008 1.007 1.065 1.011 1.000 8% RH5 53.775 1.077 1.150 1.136 1.059 1.000 9 10data = [ 36.992 1.033 1.056 1.114 1.040 1.000;... 11 62.333 1.060 1.115 1.170 1.066 1.000]; 12 13% check on input 14if sensNum ~= 1 && sensNum ~= 2 && sensNum ~= 3 && sensNum ~= 4 && sensNum ~= 5 15 error('sensNum must be 1 or 2 or 3 or 4 or 5') 16end 17 18deltaX = data(2,1) - data(1,1); % scalar 19deltaY = data(2,2:end) - data(1,2:end); % array 1D 20m = deltaY/deltaX; % slope of the linear interpolant 21 22corrFact = data(1,sensNum+1) + m(sensNum) * (rh5 - data(1,1)); 23
DHT22_5sensor.ino
arduino
Arduino sketch to read sensors data and pass them to the computer by the serial port.
1// Libraries 2#include <DHT.h> 3 4 5// Initialize DHT sensor for 6 normal 16mhz Arduino 7DHT dht1(2, DHT22); 8DHT dht2(4, DHT22); 9DHT dht3(7, 10 DHT22); 11DHT dht4(8, DHT22); 12DHT dht5(12, DHT22); 13 14int const stopPin 15 = 6; 16int stopFlag = 0; 17 18 19// Variables 20float hum; //Stores humidity 21 value 22float temp; //Stores temperature value 23 24void setup() 25{ 26 pinMode(stopPin, 27 INPUT); 28 Serial.begin(9600); 29 dht1.begin(); 30 dht2.begin(); 31 dht3.begin(); 32 33 dht4.begin(); 34 dht5.begin(); 35} 36 37void loop() 38{ 39 delay(2000); 40 41 42 // Read humidity from the 5 sensors and display it on a single line 43 hum 44 = dht1.readHumidity(); 45 Serial.print(hum); 46 Serial.print(":"); 47 48 hum = dht2.readHumidity(); 49 Serial.print(hum); 50 Serial.print(":"); 51 52 hum = dht3.readHumidity(); 53 Serial.print(hum); 54 Serial.print(":"); 55 56 hum = dht4.readHumidity(); 57 Serial.print(hum); 58 Serial.print(":"); 59 60 hum = dht5.readHumidity(); 61 Serial.print(hum); 62 Serial.print(":"); 63 64 65 // Read temperature from the 5 sensors and display it on a single line 66 temp 67 = dht1.readTemperature(); 68 Serial.print(temp); 69 Serial.print(":"); 70 71 temp = dht2.readTemperature(); 72 Serial.print(temp); 73 Serial.print(":"); 74 75 temp = dht3.readTemperature(); 76 Serial.print(temp); 77 Serial.print(":"); 78 79 temp = dht4.readTemperature(); 80 Serial.print(temp); 81 Serial.print(":"); 82 83 temp = dht5.readTemperature(); 84 85 // Check stop condition 86 stopFlag 87 = digitalRead(stopPin); 88 if (stopFlag == LOW){ 89 Serial.println(temp); 90 91 } 92 else{ 93 Serial.print(temp); 94 Serial.println(":999"); 95 96 } 97 98} 99
DHT22_5sensor.m
matlab
MATLAB code to read serial data from the Arduino. Please check your port number on line 18. First acquisition should be without correction. For the first time leave lines 66 to 68 commented. I have used an external tool to verify temperature and humidity readings and it appeared that sensor number 5 read both correctly in several condition. I have written the humCorr.m function to fix the other sensors readings from the value of sensor number 5. If interested in correction, manipulate humCorr.m function and/or lines 66-68 according to your needs.
1% Script to test a bundle of 5 DHT22 sensors with an Arduino (Uno) 2% microcontroller. Acquire data from the sensors until a stop button is 3% pressed on the board or a time limit is reached. Plot last 10 minutes of 4% live data during acquisition, the entire data set after the acquisition, 5% and save these data on a spreadsheet. 6 7close all 8instrreset 9clear 10clc 11 12% Acquisition time (min). Insert inf to disable time limit. 13waitTime = 10; 14 15%% Acquire and display live data 16 17% Open serial communication 18s = serial('/dev/cu.usbmodem411','BAUD',9600); 19 20figure 21color = ['b', 'r', 'g', 'm', 'c', 'b', 'r', 'g', 'm', 'c']; 22for i = 1:5 23 h(i) = animatedline('Color',color(i),'LineWidth',2); 24end 25axh = gca; 26axh.YGrid = 'on'; 27axh.YLim = [30 80]; 28xlabel('Time') 29ylabel('Humidity (%)') 30legend('Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5',... 31 'Location','NorthWest') 32 33figure 34for i = 6:10 35 h(i) = animatedline('Color',color(i),'LineWidth',2); 36end 37axt = gca; 38axt.YGrid = 'on'; 39axt.YLim = [10 40]; 40xlabel('Time') 41ylabel('Temperature (\\circC)') 42legend('Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5',... 43 'Location','NorthWest') 44 45stop = false; 46waitTime = duration(0,waitTime,0); 47startTime = datetime('now'); 48t = datetime('now') - startTime; 49 50while ~stop && t < waitTime 51 52 % Read data from serial port 53 fopen(s); 54 idn = fscanf(s); 55 fclose(s); 56 57 % Separate data 58 C = strsplit(idn,':'); 59 60 % Display data in MATLAB command window 61 serialData = str2double(C); 62 63 % Humidity correction factor from measurement of sensor 5 64 corrData = serialData; 65 % First acquisition should be without correction. For the first time leave lines 66 to 68 commented. I have used an external tool to verify temperature and humidity readings and it appeared that sensor number 5 read both correctly in several condition. I have written the humCorr.m function to fix the other sensors readings from the value of sensor 5. If interested in correction, manipulate humCorr.m function and/or the following lines according to your needs. 66 %for i = 1:4 67 % corrData(i) = serialData(i) * humCorr(serialData(5),i); 68 %end 69 70 disp(corrData) 71 72 % Get current time 73 t = datetime('now') - startTime; 74 75 % Add points to animation (humidity data) 76 for i = 1:5 77 addpoints(h(i),datenum(t),corrData(i)) 78 end 79 80 % Update axes 81 axh.XLim = datenum([t-seconds(600) t]); 82 datetick('x','keeplimits') 83 drawnow 84 85 % Add points to animation (temperature data) 86 for i = 6:10 87 addpoints(h(i),datenum(t),corrData(i)) 88 end 89 90 % Update axes 91 axt.XLim = datenum([t-seconds(600) t]); 92 datetick('x','keeplimits') 93 drawnow 94 95 % Check stop condition from serial monitor 96 if str2double(C{end}) == 999 97 stop = true; 98 end 99end 100 101% Output message 102if stop 103 disp('Data acquisition ended because the STOP button has been pressed') 104else 105 disp('Data acquisition ended because the TIME limit has been reached') 106end 107 108%% Plot the recorded data 109 110for i = 1:5 111 [~,humLogs(i,:)] = getpoints(h(i)); 112 [timeLogs,tempLogs(i,:)] = getpoints(h(i+5)); 113end 114timeSecs = (timeLogs-timeLogs(1))*24*3600; 115 116figure 117subplot(1,2,1) 118plot(timeSecs,humLogs,'LineWidth',2) 119grid on 120ax = gca; 121ylim([round(ax.YLim(1)-2), round(ax.YLim(2)+2)]) 122xlabel('Elapsed time (s)') 123ylabel('Humidity (%)') 124 125subplot(1,2,2) 126timeSecs = (timeLogs-timeLogs(1))*24*3600; 127plot(timeSecs,tempLogs,'LineWidth',2) 128hold off, grid on 129ax = gca; 130ylim([round(ax.YLim(1)-2), round(ax.YLim(2)+2)]) 131xlabel('Elapsed time (s)') 132ylabel('Temperature (\\circC)') 133legend('Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5',... 134 'Location','Best') 135 136%% Save results to a file 137 138T = table(timeSecs',humLogs',tempLogs','VariableNames',... 139 {'Time_s','Relative_Humidity','Temperature_C'}); 140filename = 'Humidity_and_Temperature_Data.xls'; 141 142% Delete previous file, if exists, to avoid append of data 143if exist(filename,'file') 144 delete(filename) 145end 146 147% Write table to file 148writetable(T,filename) 149% Print confirmation to command line 150fprintf('Results table with %g humidity and temperature measurements saved to file %s\ 151',... 152 length(timeSecs),filename) 153 154%% Summary charts with original data, averaged data, and uncertainty 155 156for sensor = 1:5 157 158 % Smooth out readings with moving average filter 159 smoothHum = smooth(humLogs(sensor,:),25); 160 smoothTemp = smooth(tempLogs(sensor,:),25); 161 162 % Typical accuracy of the humidity sensor 163 humMax = 1.02 * smoothHum; 164 humMin = 0.98 * smoothHum; 165 166 % Worst accuracy of the humidity sensor 167 humMaxW = 1.05 * smoothHum; 168 humMinW = 0.95 * smoothHum; 169 170 % Accuracy of the temperature sensor 171 tempMax = smoothTemp + 0.5; 172 tempMin = smoothTemp - 0.5; 173 174 figure 175 subplot(1,2,1), hold on 176 plot(timeSecs,humLogs(sensor,:),'b','LineWidth',2) 177 plot(timeSecs,smoothHum,'r','LineWidth',1) 178 plot(timeSecs,humMin,'r--','LineWidth',2) 179 plot(timeSecs,humMax,'r--','LineWidth',2) 180 plot(timeSecs,humMinW,'m--','LineWidth',1) 181 plot(timeSecs,humMaxW,'m--','LineWidth',1) 182 hold off, grid on, ylim([round(min(humMinW))-2, round(max(humMaxW))+2]) 183 xlabel('Elapsed time (s)') 184 ylabel('Humidity (%)') 185 title(['Humidity data, average, and uncertainty for sensor ',num2str(sensor)]) 186 187 subplot(1,2,2), hold on 188 plot(timeSecs,tempLogs(sensor,:),'b','LineWidth',2) 189 plot(timeSecs,smoothTemp,'r','LineWidth',1) 190 plot(timeSecs,tempMin,'r--','LineWidth',2) 191 plot(timeSecs,tempMax,'r--','LineWidth',2) 192 hold off, grid on, ylim([round(min(tempMin))-2, round(max(tempMax))+2]) 193 xlabel('Elapsed time (s)') 194 ylabel('Temperature (\\circC)') 195 title(['Temperature data, average, and uncertainty for sensor ',num2str(sensor)]) 196 197end
Downloadable files
Connections
Breadboard view
Connections
Connections
Breadboard view
Connections
Comments
Only logged in users can leave comments