# :electric_plug: Temperature and Humidity sensor with Arduino Nano RP2040 connect ::: info **Linnéuniversitetet** **1DT305 - Introduction to Applied IoT** **Carl-Adam Spanlang cs224gt** ::: Table of content: [ToC] ## :page_facing_up: Introduktion to the project This tutorial explains how to monitor temperature and humidity in an indoor environment using a sensor (DHT11) and an Arduino RP2040 connect and later view the data on a dashboard in the Arduino IoT Cloud. If the guide provided in this paper is followed and the same tools are used, the project shouldn't take more than 3–4 hours to complete. Since none of the supervisors had any prior experience with the Arduino RP2040 connect and there wasn't much information online, in my case, it took a lot longer to complete this project. Because of this, I had to invest a lot of time in my own research and troubleshooting. On average, completing the project took between 30 and 40 hours. ## Objective The project was selected as a part of Linnaeus University's "Introduction to Applied IoT" course. Due to limited prior knowledge of the Internet of Things, the project's objectives were to learn as much as possible while simultaneously developing a product that can read temperature and humidity. Later, the read data will be transferred to a cloud platform where it will be visually displayed. In hindsight, the solution itself was not all that difficult, but it served as a springboard for future projects. The project's insights will be learning how to develop, test, and deploy IoT solutions. ## Material The breadboard and jumper wires were bought as a bundle from Elektrokit for 129 SEK. The Arduino RP2040 Nano Connect was also purchased from Elektrokit for 299SEK, and the DHT11 temperature and humidity sensor was purchased from Kjell & Company for 99SEK. The table below includes images of every component, a short description, and a hyperlink to where it was purchased. | Image | Item | Description | Link & price | |:--------------------------------------------------:|:---------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------:| ------------- | | ![nano](https://i.imgur.com/AdO31Rm.png =200x100) | Arduino RP2040 Nano Connect | Arduino's microcontroller, the Nano RP2040 Connect, is based on the Raspberry Pi's microcontroller. | [299SEK](https://www.electrokit.com/produkt/arduino-nano-rp2040-connect-with-headers/) | | ![dht11](https://i.imgur.com/sxifOlD.png =200x100) | Temperature and Humidity sensor (DHT11) | Measuring temperature and humidity with Arduino | [99SEK](https://www.kjell.com/se/produkter/el-verktyg/arduino/arduino-tillbehor/temperatur-och-luftfuktighetssensor-for-arduino-p87086?gclid=CjwKCAjwk_WVBhBZEiwAUHQCmZG8aK3ZSz1MeuWmcjSOttbnaXc162GZyeo4G5B0xLE4RghGi5PRiRoCCUgQAvD_BwE&gclsrc=aw.ds) | | ![BD](https://i.imgur.com/8euS2JU.png =200x100) | Breadbord | The breadboard is used for building temporary circuits for simple wiring and sensor connections. | [129SEK](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-sensors-only-bundle/) | | ![JW](https://i.imgur.com/AMmx7cm.jpg =200x100) | Jumper Wire | Jumper wires make it possible to connect a sensor to the breadboard rather than directly to the Arduino RP2040 and transfer power between the two components | [Included in bundle](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-sensors-only-bundle/) | ## Computer Setup I'll give an overview of the project's progress in this step. What procedures were followed in order to activate the microcontroller, among other things. Arduino IDE was the IDE of choice for this project. A text editor for writing code, a message area, and many other features are available in the Arduino IDE and can be used with the Arduino hardware. Initially, the temperature and humidity sensors, as well as the Arduino hardware, were tested locally using the Arduino IDE. The IoT Arduino Cloud came into focus after the workability was confirmed. Both procedures will be demonstrated in this chapter. ### Locally installed Arduino IDE to test functionallity. For the purpose of testing the functionality of the Arduino RP2040 and the DHT11 sensor, this step is only performed locally on your computer. You are welcome to omit this step and focus solely on the cloud service. The chapter "Platform" will go into more detail regarding the Arduino IoT Cloud. **1.** First, go to [Arduino Software](https://www.arduino.cc/en/software) to download the most recent version of the Arduino IDE. Install the software on your computer. After the installation is finished, launch the Arduino IDE. ![](https://i.imgur.com/6CYaGAG.png) **2.** Once Arduino IDE is open, the Arduino Mbed OS Nano Boards can be downloaded. After completing the download, connect your Arduino RP2040 to your computer using a USB cable and confirm that the computer recognizes the device and that the file storage opens properly. **3.** Inside Arduino IDE select **Tools -> Board -> Board manager**. As demonstrated below, search for **Arduino Mbed OS Nano Boards** and install the latest version. ![](https://i.imgur.com/QcEOzDG.png) **4.** Once the installation is complete head to **Tools -> Board -> Arduino -> Arduino Mbed OS Nano Boards** and select **Arduino Nano RP2040 Connect** as shown below. ![](https://i.imgur.com/8DNkm81.png) **5.** After all of this, the correct core has been installed. Furthermore, the Arduino hardware's port is also selected by navigating to **Tools -> Port** and setting the port used. When the correct port is selected, and everything is functional, it's time to wire the circuit with the Arduino RP2040 and the DHT11 temperature and humidity sensor. **6.** The image below demonstrates how I chose to connect the DHT11 temperature and humidity sensor to the Arduino RP2040. ![](https://i.imgur.com/fSD38NN.png) Connect the jumper wires to the DHT11 sensor as shown above. Each of the DHT11 sensor's pins is marked. Make sure to connect the **+** to the Arduino 3V pin, the **-** to the ground pin, and the **s** (signal pin) to the D2 pin. The DHT11 sensor's instruction manual can be used in this situation if there are any uncertainties.: [DHT11 User Manual](https://www.kjell.com/globalassets/mediaassets/745018_87086_manual_en.pdf?ref=A5605F6320) Once the wiring is complete, the DHT11 sensor needs to be tested with some code. ### Testing the DHT11 Open up the Arduino IDE libary manager by going to **Sketch -> Include Libary ->** and press **"Manage Libraries"**. This will open up the Library Manager. In the search field, search for "DHT11" and install the DHT sensor library by Adafruit like shown below. ![](https://i.imgur.com/MvqoObb.png) Once the installation is completed, open up the DHT tester by going to **Examples -> DHT sensor library -> DHTTester.** The sketch contains helpful information to get the sensor up and running, but you will need to modify the sensor and pin placement in the code. The code used for this project will be linked here: [GitHub Gist for DHT11 code](https://gist.github.com/cs224gt/f4d9367d63fc5f6ea8c732b47250b86d). Remember that this code is only compatible with the setup utilized in this project. Make sure to adapt the code if you use a different setup to suit your needs. Run the code in Arduino IDE and open the **"Serial Monitor"** by either pressing **Ctrl+Shift+M** or **Tools -> Serial Monitor**.If everything is working as indented, the output should look like this: ![](https://i.imgur.com/LN51I38.png) The next step will explain how to set up everything in the cloud using Arduino IoT Cloud. ## Platform The platform used for this project is the [Arduino IoT Cloud](https://create.arduino.cc/iot/things). Go to the link and create an account. **Step 1**. Go to the "Devices" tab and add a device. ![](https://i.imgur.com/iSwXLFr.png) Add your Arduino device, make sure that it is connected, and follow the steps. Download the **Arduino Create Agent**, and this is done in order to upload sketches from the Arduino Cloud platform to your device. **Step 2**. Once the device is added, go to **"Things"** and press **create**. Once we have created the device, there will be three parameters that we will work with, as shown below.**(1) Device** is used to connect the Arduino RP2040 that was created earlier. **(2) Add Variable**, which is used to define the variables that will be used during the code, and **(3) Network**, which is used to add the network credentials. ![](https://i.imgur.com/vnSqhX7.png) Our initial step will be to add the device. Once the device are added, the next step is to add the variables. You should add two variables: **temperature** and **humidity**. Follow the instructions on the picture below for both variables and press **"Add variable"** ![](https://i.imgur.com/uIK67Fx.png =400x) After that, we'll mount the network and configure it. Add your network credentials after pressing **"Configure network"**. ![](https://i.imgur.com/bB2AAoD.png =800x) **Step 3**. Go to the **dashboard** tab and build a new Dashboard. Inside the Dashboard, create two new **widgets**. Use **Gauge** for temperature and **Percentage** for humidity. This is optional, as you can choose whatever widget you find appropriate. Start by adding the widget Gauge and name it **"Temperature"** and link the variable created earlier. Once the widget is added, they will have standard numbers as the code is not yet executed. ![](https://i.imgur.com/rWGd44P.png) **Step 4**. In the next step, we go to the tab "Things" and head to the sketch where the code has been generated from the variables we configured earlier. The code itself is pretty straightforward: all we have to do is read the value of the sensors and attach them to the variables we previously linked to the widgets. In order to use the DHT11 temperature and humidity sensor with the Arduino Cloud IoT, a library is required, similar to what was done with Arduino IDE. We incorporate the "EduIntro" library into the code to accomplish this in the cloud. You can read more about "EduIntro" by visiting this page: [EduIntro](https://github.com/arduino/EduIntro). Head to **Things -> Sketch** and put in the following code: ```cpp=18 #include <EduIntro.h> DHT11 dht11(D2); ``` Reading the data that is received from the DHT11 sensor is the only task left before we can run the code and measure temperature and humidity. Add the following lines to accomplish this: ```cpp=48 dht11.update(); temperature = dht11.readCelsius(); humidity = dht11.readHumidity(); delay(60000); // Updates Humidity and Temperature every 60 seconds ``` When the sketch has been successfully uploaded, you should receive the following confirmation: ![](https://i.imgur.com/4t30IFc.png) Head to the **Dashboard** to read the temperature and humidity in real-time. The delay of updates is set to every 60 seconds. ![](https://i.imgur.com/YAMrA8z.png) By downloading the Arduino IoT Cloud Remote app, you can also view the updates directly from your phone. ![](https://i.imgur.com/3FpF7aq.png =x400) Link to the entire code: [Arduino IoT Cloud Sketch](https://gist.github.com/cs224gt/3c9aa2ad6272db6e554a0a56d6f77f90) ## The code The earlier chapters, "Computer Setup" and "Platform," explain the code used for this project. Since the code is already covered in these steps, this chapter will only briefly go over the code's features. The project has two "main" scripts since it is split into two sections: one for locally testing the sensor and the other for moving to the Arduino IoT Cloud. The "DHT sensor library" provided the code that was used to test the DHT11 sensor, which may be downloaded from the library manager in the Arduino IDE. Generally, the code is written and modified to fit my chosen settings. The code in this link contains comments describing every change: [Code for locally testing the sensor](https://gist.github.com/cs224gt/f4d9367d63fc5f6ea8c732b47250b86d) Additionally, a different piece of code was used to link the Arduino RP2040 and DHT11 sensor to the Arduino IoT Cloud. This step differs slightly from the previous one in that variables that change on the page are automatically generated and updated as code in "sketches." Thus, any modifications to the "Thing" instantly turn into code. This simple way of writing and generating code left less room for error. In addition to the generated code, these lines had to be added to import the DHT11 library and connect the signal pin. ```cpp=18 #include <EduIntro.h> DHT11 dht11(D2); ``` These four lines had to be added to the previous two lines to read the sensor's data and delay the readings: ```cpp=48 dht11.update(); temperature = dht11.readCelsius(); humidity = dht11.readHumidity(); delay(60000); // Updates Humidity and Temperature every 60 seconds ``` Here is a link to the Arduino IoT Cloud's complete source code: [Arduino IoT Cloud Sketch](https://gist.github.com/cs224gt/3c9aa2ad6272db6e554a0a56d6f77f90) ## Transmitting the data / connectivity The U-Blox Nina W102 module, which offers native Bluetooth and WiFi connectivity, enables the data to be transmitted over WiFi. WiFi is a great option for sending data because of its speed, but it consumes a lot of power. There were no issues with the device accessing WiFi or power consumption since I was using it at home. 1. Arduino takes sensor readings every minute. During the testing phase, readings were taken much more frequently so that data could be read quickly for functionality checks. 2. The wireless protocol used for this project is WiFi. 3. For transportation of data MQTT was used. ## Presenting the data Since I worked with Arduino hardware, the Arduino IoT Cloud provided a very flexible platform to build the dashboard. ![](https://i.imgur.com/K083QXp.png) How to create a dashboard is described in and explained in the [platform](https://hackmd.io/nfMEqYBYR2SvFXudAbgtmQ?both#Platform) section. According to the illustration above, the final dashboard with additional charts is constructed. A **gauge** widget on the dashboard displays the temperature, and a **percentage** widget displays the humidity readings. Additionally, each measurement has a separate chart that makes it simple to follow changes in temperature and humidity over time. You can see the current temperature and what it looked like in the previous hour, the last day, the last seven days, and finally, a summary of the previous 15 days. Therefore, the data is kept for a maximum of 15 days. Data is sent every minute to the database, which is also how often the Arduino IoT Cloud updates the data. You can use your smartphone to access the dashboard from anywhere at any time by downloading the [Arduino IoT Cloud Remote application](https://apps.apple.com/us/app/id1514358431) The mobile device view has been modified to make it simpler to read the content on a smartphone. Below is a screenshot of the dashboard from the view of a smartphone ![](https://i.imgur.com/hcDzL0V.png =300x) ## Finalizing the design The project has been enjoyable and very educative. It hasn't always been a bed of roses; numerous bugs and issues have cropped up throughout the project, which has been challenging and enjoyable once solved. The project has provided a better understanding of the Internet of Things, how to program devices and the countless DIY projects that can be created using sensors. One area where improvements could have been made is to invest in the upgraded and more expensive DHT22 sensor, which could have provided more accurate readings. For future projects, it would have been interesting to use a soil sensor to measure soil moisture in plants. This could be used combined with a water pump to automatically water plants when the soil becomes too dry. The picture below shows what the final product looked like in reality. ![](https://i.imgur.com/Xh3yHJJ.jpg =430x)