Main Content

Create DHT11 Sensor Block to Measure Relative Humidity and Temperature Using IO Device Builder App

This example shows how to use the IO Device Builder app to create DHT11 sensor for measuring relative humidity and temperature. In this example, the DHT11 sensor communicates with a microcontroller through a single-wire interface. The communication protocol is a proprietary one-wire protocol that uses a timing-based communication scheme. It detects water vapor by measuring the electrical resistance between two electrodes, and it measures temperature with a surface-mounted NTC temperature sensor (thermistor) built into the unit.

Note: By default, communication libraries such as SPI, I2C, and Servo are included. For information on adding other libraries, see Working with Arduino Libraries in IO Device Builder.

Prerequisites

Circuit Connections

To run this example, make these circuit connections.

  • For 5.0V LOGIC boards: Connect 5V on the Arduino® to VIN on the DHT11

  • For 3.3V LOGIC boards: Connect 3.3V on the Arduino to VIN on the DHT11.

  • Connect GND on the Arduino to GND on the DHT11.

  • Connect the signal pin on the DHT11 to the digital pin 10 on the Arduino.

Open the IO Device Builder App

To open the IO Device Builder app, perform these steps.

  1. Start MATLAB® and then open / create a Simulink® model.

  2. On the Hardware tab of the Simulink toolstrip, in Prepare section, under Design, choose IO Device Builder.

Select Working Directory and Add Third-Party Source Files

Once the Source files location page loads, select the working directory and add third-party source files.

On the Source files location page:

  1. Click Select and select the working directory. In the working directory, the generated system object, along with the corresponding cpp and h files, and the model are located.

  2. Click Add folder and add the DHT-sensor-library and Adafruit_Sensor-master folders you downloaded. Only the files present directly within the selected folder are included, and any files present within subfolders are excluded.

  3. Click Next to continue.

Select Source Files

On the Source files page, select the required source files and then click Next to continue.

Specify Block Parameters

On the Block parameters page:

  1. Specify the block name and add block description.

  2. Add parameter dataDelay with Data type uint32 and Dimension [1,1].

  3. Click Next to continue.

Select Outputs for the Block

On the Outputs page:

  1. Add Humidity and Temperature as Outputs with Data type as single and Dimension [1,1].

  2. Click Next to continue.

Select Inputs for the Block

On the Inputs page:

  1. Remove inputs for the block, if any already added.

  2. Click Next to continue.

Preview Block

On the Block image page, view the preview of the block with the inputs and outputs you added. Click Next to continue.

Generate System Object Files

On the Generate page, the file generation location is displayed. Click Generate to generate the System object™ files and then click Finish in the Next Steps screen.

Next Steps

On the Next Steps page, the files generated are shown and the next steps to be performed are displayed.

Perform these steps.

1. The generated .cpp file opens automatically. Modify the generated .cpp file as shown below.

#include "C:\user\dht11\dht11Sensor.h"  //App generated header. Do not modify.
#include "Adafruit_Sensor.h"
#include "DHT.h"
#include "DHT_U.h"
#include Arduino.h

#define DHTPIN 10
#define DHTTYPE    DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
void setupFunctiondht11Sensor(uint32_T * dataDelay,int size_vector__1){
    Serial.begin(9600);
    dht.begin();
    delayMS = dataDelay;
}

void stepFunctiondht11Sensor(float * Humidity,int size_vector_1,float * Temperature,int size_vector_2){
    delay(delayMS);
    sensors_event_t event;
    dht.temperature().getEvent(&event);
    if (isnan(event.temperature)) {
    }
    else {
        *Temperature=event.temperature;

    }
    dht.humidity().getEvent(&event);
    if (isnan(event.relative_humidity)) {
    }
    else {
        *Humidity=event.relative_humidity;
    }
}

2. Assign the output to the selected output port.

5. Add the #include "Arduino.h" header and other required headers for the driver, as mentioned in the example with the absolute address.

6. Open the generated Simulink model.

7. Add a Display to the output ports.

8. In the Simulink model, navigate to Modeling > Model Settings and in the Configuration Parameters dialog box, click Hardware Implementation and then select an Arduino board.

A sample Simulink model is shown here.