# IoT temperature and humidity sensor with Arduino RP2040 By Alexander Pettersson (ap222ju at Linneaus unniversity) --- **Overview of the project** This project is about how to make an IoT device that senses humidity and temperatarure using a DHT11 and the built in rgb-led and temperature sensor and also an external RGB-led to control just for fun with an arduino RP2040 connected to the arduino IoT cloud. Approximated time: Depends on your level of experience, for me it took several hours but for someone with more experience it could take maybe just 2-3 hours. --- # Objective My first thought of this project was to create a device for a greenhouse to read temperature and humidity. My first sketch of this device it included a soil moisture sensor to check when to water the plants, but since it is a time limited project I didnt have time to add that sensor. So on how the device came out I can still check the temperature and the humidity in my greenhouse. The project should give insights in basic concepts of IoT, sensors and programming. The device will create awareness of the environment in a greenhouse. ___ # Material | Hardware | Usage | Price | | -------- | -------- | -------- | | Arduino RP2040 ![](https://i.imgur.com/XbefTmc.jpg) | Microcontroller | [299 SEK](https://www.electrokit.com/produkt/arduino-nano-rp2040-connect-with-headers/) | | Battery holder 3xAAA with switch| Powering for device | [19 SEK](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare/) | | USB cable | For powering device and to upload code when commisioning | [39 SEK](https://www.electrokit.com/en/product/usb-cable-a-male-micro-male-5-pin-1m/) | | Jumper wires 30cm female/male | Connecting sensors | [29 SEK](https://www.electrokit.com/en/product/jumper-wires-20-pin-30cm-female-male/) | | Jumper wires 30cm male/male | Connecting sensors | [29 SEK](https://www.electrokit.com/en/product/jumper-wires-20-pin-30cm-male-male/) | | Resistor metal film 0.6W 1% 330ohm (330R) | Resistor for connecting RGB-led module | [3 SEK](https://www.electrokit.com/en/product/resistor-metal-film-0-6w-1-330ohm-330r/) | | DHT11 Temperature and humidity sensor ![](https://i.imgur.com/4dluJGT.jpg) | Sensor to read temperature and humidity | [100 SEK](https://www.kjell.com/se/produkter/el-verktyg/utvecklingskit/arduino/moduler/temperatur-och-luftfuktighetssensor-for-arduino-p87086) | | RGB-led module ![](https://i.imgur.com/0Cu3KET.png)| RGB-led | [ca. 19 SEK (Included in sensor package) ](https://www.kjell.com/se/produkter/el-verktyg/arduino/moduler/playknowlogy-stora-modul-paketet-for-arduino-p87291) | In the first sketch of this project I had choosen an heltec microcontroller for this project but due to the electonic components situation in the world the supplier were unable to provide this devices so I went with the arduino instead wich were one of the recomended microcontrollers. ___ # Computer setup **IDE** To write and upload your code you need an IDE. Arduino got thier own IDE simply called Arduino IDE. To install the IDE and to connect your device they have a really nice tutorial that you can follow [here](https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-01-technical-reference#installation). If you are going to use the IoT cloud and dont want to test the sensor skip to **Setting up Arduino cloud**. To test ot your DHT11 you need to install a couple of libraries for DHT and Unified sensors by following these steps: 1. Open the library handler. ![](https://i.imgur.com/6PDqLEe.jpg) 2. Install the DHT sensor library ![](https://i.imgur.com/nmgQxlK.jpg) 3. Install the Adafruit Unified Sensor library ![](https://i.imgur.com/aXy1Dih.jpg) 4.Open up the example code for DHT sensors. (DHTtester) ![](https://i.imgur.com/v2Ef4HY.jpg) 5. In the first box you need to change to wich pin yor sensor i connected (in my example it is connected to pin 5). And you need to uncomment the DHT11 and delete/comment the DHT22 in the second box. ![](https://i.imgur.com/zHPHz9g.jpg) 6. Upload your code and open the seriell monitor under tools or ctrl+shift+m. ![](https://i.imgur.com/XLuZ9KT.jpg) 7. The result should be something like this. ![](https://i.imgur.com/wXMX1Az.jpg) **Setting up Arduino IoT cloud** I started out using the IDE 1.8.19 to connect my device and to test out my sensors. But because I wanted to use the Arduino IoT cloud I needed to connect my device to the cloud, which were very easy to do by just following this [tutorial](https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-iot-cloud). Unless you want to be able to read/write to more then five variables you can use the free version of the cloud. To make the same setup as I did you need to setup your variables as following picture: ![](https://i.imgur.com/g4nJA8R.jpg) And under network (down to the right) you need to put in your ssid and password for your Wifi. Now under Sketch you will have som code already written for you and you just need to add what you want to do with your varaibles. If you are using the same set up as me you can just copy my code. And then edit your dashboard as expained in the tutorial for [Arduino IoT cloud](https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-iot-cloud). --- <br></br> # **Putting everthing together** The sensors has been connected to a breadboard with jumperwires to the microcontroller as the figure below: ![](https://i.imgur.com/9h1yhZi.jpg) # **Platform** I wanted to go with a cloud based platform for my application so the Arduino IoT cloud was a good option for me. It uses on-change or periodically based triggers. It uses MQTT as communication protocol. It is easy to set up the variables and you can write and upload the code directly from the cloud. Its limited as a free user so if you want more functions you need to subscribe to get more functions. I use the free plan, which works fine for me and my application. # The code ``` #include "thingProperties.h" #include "thingProperties.h" #include <Arduino_LSM6DSOX.h> #include "DHT.h" #define DHTPIN 5 #define DHTTYPE DHT11 DHT dht(DHTPIN,DHTTYPE); void setup() { // Initialize serial and wait for port to open: pinMode(LEDR, OUTPUT); pinMode(LEDG, OUTPUT); pinMode(LEDB, OUTPUT); pinMode(2,OUTPUT); //Green pinMode(3,OUTPUT); //BLUE pinMode(4,OUTPUT); //RED Serial.begin(9600); dht.begin(); while(!Serial); // Prevents sketch from running until Serial Monitor opens. if (!IMU.begin()) { Serial.println("Failed to initialize IMU!"); while (1); } delay(1500); // Defined in thingProperties.h initProperties(); // Connect to Arduino IoT Cloud ArduinoCloud.begin(ArduinoIoTPreferredConnection); /* The following function allows you to obtain more information related to the state of network and IoT Cloud connection and errors the higher number the more granular information you’ll get. The default is 0 (only errors). Maximum is 4 */ setDebugMessageLevel(2); ArduinoCloud.printDebugInfo(); } void loop() { ArduinoCloud.update(); humidity = dht.readHumidity(); temperature = dht.readTemperature(); if (IMU.temperatureAvailable()) { IMU.readTemperature(temp_chip); } } /* Since Green is READ_WRITE variable, onGreenChange() is executed every time a new value is received from IoT Cloud. */ void onGreenChange() { if(green){ digitalWrite(LEDG, HIGH); //turn on GREEN digitalWrite(2, HIGH); } else{ digitalWrite(LEDG, LOW); //turn off GREEN digitalWrite(2, LOW); } } /* Since Blue is READ_WRITE variable, onBlueChange() is executed every time a new value is received from IoT Cloud. */ void onBlueChange() { if(blue){ digitalWrite(LEDB, HIGH); //turn on GREEN digitalWrite(3, HIGH); } else{ digitalWrite(LEDB, LOW); //turn off GREEN digitalWrite(3, LOW); } } ``` # Transmitting the data / Connectivity The data is sent periodically from the humidity and the temparature sensors in an inteval of 5 sec, this is a time I will raise later on but I used 5 secs for testing my device, the RGB-leds is sent on a trigger. I use WiFi for this application. MQTT is the communication protocol. # Presenting the data The data will be saved for 1 day as a free user of Arduino IoT Cloud. Heres a picture of the dashboard: ![](https://i.imgur.com/kjiJEOU.jpg) # Finalizing the design I had a hard time getting the sensors to work out. I started to try and program i Thonny and microPython but I had a hard time with the libraries. Then after a while I decided to go with the arduino IDE and arduino language (c++?). I think it would have been a bit easier if I had choosen to go with a pycom instead. But I think the arduino environment and its all tutorials were really good and it help me alot in this project though I would have liked to program in micrPython. After all it worked out pretty nice and heres some pictures of my device. ![](https://i.imgur.com/EXGKRzZ.jpg) ![](https://i.imgur.com/5BsV7zZ.jpg)