DHT Sensor and OLED with MKR Connector Carrier

Learn how to display temperature and Humidity on an OLED display with the MKR Connector Carrier.

This basic example teaches you how to create a circuit that uses two Grove modules and requires no soldering. The MKR board you choose may be anyone of the MKR family because the connection is managed through the MKR Connector Carrier.

Hardware Required

Circuit

The connection to the Carrier board requires two standard Grove cables. The DHT humidity and temperature sensor goes to the D0 connector. The OLED screen is connected to the TWI connector.

The MKR Connector Carrier.
The MKR Connector Carrier.

Grove DHT sensor module.
Grove DHT sensor module.

We did not put a MKR board on the Carrier, but it is required to get the circuit to work, as specified in the bill of materials.

The DHT module uses a specific pin to communicate with the MKR board and it is mapped on D0. This happens because the Grove standard for digital connections follows this rule:

PinFunctrionNotespin1DnPrimary digital i/opin2Dn+1Secondary digital i/opin3VCCPower to module 5V/3.3Vpin4GNDGround

and the module sends SIG on pin 1 that is mapped on the primary digital I/O.

In the picture, the OLED module is shown from component side to let you see the Grove connector. It goes into the TWI male on the MKR Connector Carrier that follows this pin mapping:

PinFunctrionNotespin1SCLI2C Clockpin2SDAI2C Datapin3VCCPower to module 5V/3.3Vpin4GNDGround

Code

To drive the modules you need to load four separate libraries:

1#include <DHT.h>
2#include <DHT_U.h>
3#include <Wire.h>
4#include <SeeedOLED.h>

The DHT module is mapped on D0 when the object dht is instantiated:

1DHT dht(0, DHT22);

The rest of the code is straightforward and keeps reading the

hum
and
temp
values to be printed on the OLED screen.

Here is the complete sketch:

1#include <DHT.h>
2#include <DHT_U.h>
3#include <Wire.h>
4#include <SeeedOLED.h>
5
6DHT dht(0, DHT22);
7
8void setup() {
9
10 Wire.begin(); //initialize I2C in master mode
11
12 SeeedOled.init(); //initialize the OLED
13
14 SeeedOled.clearDisplay(); //clear the screen and set start position to top left corner
15
16 SeeedOled.setNormalDisplay(); //Set display to normal mode (i.e non-inverse mode)
17
18 SeeedOled.setPageMode(); //Set addressing mode to Page Mode
19}
20
21void loop() {
22
23 float temp, hum;
24
25 //Read temperature and humidity
26
27 do {
28
29 hum = dht.readHumidity();
30
31 temp = dht.readTemperature();
32
33 }
34
35 while (isnan(temp) || isnan(hum));
36
37 //Print temperature and humidity values on the OLED display
38
39 SeeedOled.setTextXY(0, 0);
40
41 SeeedOled.putString("Temperature:");
42
43 SeeedOled.setTextXY(1, 0);
44
45 SeeedOled.putString(String(temp).c_str()); //print temperature data converted to a c string
46
47 SeeedOled.putString("C");
48
49 SeeedOled.setTextXY(3, 0);
50
51 SeeedOled.putString("Humidity:");
52
53 SeeedOled.setTextXY(4, 0);
54
55 SeeedOled.putString(String(hum).c_str()); //print humidity data converted to a c string
56
57 SeeedOled.putString("%");
58
59 delay(2000);
60}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.