---
# System prepended metadata

title: Room temperature

---

# Room temperature
Victor Lindgren, vl222ky

# Short project overview
The goal of this IoT project is to give you guidance on how to make a Arduino nano rp2040 connect device that could fit on a desktop and measure the temperature.


The way the device works is by using two different sensors. One internal, and one external and then comparing the values of the two.


**Time estimated for this project: 40 hours**

# Objective
***why this project?***

* Affordable.
* This was my very experience with IoT, so I wanted to keep it simple.
* Get a better understanding of hardware.
* Get a better understand of how hardware work together with software.

***What purpose does it serve?***

* Measure temperature.

***What insights will it give?***

* This project can give insight to the basics of IoT.
* This project can give insight to some of the basics of c/c++ and how its used with arduino boards.

# Material
```csvpreview {header="true"}
id,item,quantity,Description,Merchant,Price
1,Arduino nano rp2040 connect,1,Microcontroller,electrokit.com,299SEK
2,MCP9700,1,Temperature sensor,electrokit.com,8SEK
3,Resistor 330 ohm,1,Electric regulator,electrokit.com,N/A
4,USB-cable A-male – micro-male,1,Connects the microcontroller with the computer,electrokit.com,39SEK
5,Breadboard,1,Makes connecting circuits easier,electrokit.com,N/A
6,Jumper wire male-male,4,Connects two points in a circuit,electrokit.com,N/A
```

# Computer setup (Windows 10 OS)
***IDE***
For this project I used the programming language c++ together with Arduino's own IDE and their web editor. Drivers required for this board will be installed when the IDE software is installed automatically.

***Update firmware***
To set up the Arduino nano rp2040 connect we must update the firmware on it.
For this we have two options, either install it through the IDE by clicking tools > WiFiNINA Firmware updater. Alternatively, we can download the fwuploader by entering [this website](https://github.com/arduino/arduino-fwuploader/releases/tag/2.2.0) where we will have a selection of operative systems to choose between. Since i am running a windows 10 64bit system, I picked this one.
![](https://i.imgur.com/9euAmHF.png)
Once it is downloaded, simply open the zip file and click the .exe file.

***Libraries***
For this project, a very small amount of libraries were needed. To start this installing process we first must open the library manager by pressing ctrl+alt+i.

Once the library manager is open, we must look for our first library Arduino_LSM6DSOX, which is required to communicate with our microcontroller's internal sensors. This will be needed since the internal and external temperature sensor is compared to give a good average temperature.

The libraries ArduinoIoTCloud and Arduino_ConnectionHandler will also be required as they will help setup the connection between our microcontroller and the cloud.

# Putting everything together
This is how the hardware is connected on the breadboard.
![](https://i.imgur.com/c7L2FTR.png)

Starting from the top, the first wire is connected to GND.

The second goes to an analog pin, which is required to read the values coming from it.

lastly, we have the wire connected to power. It first goes to a resistor, and to calculate which one we need we first look at the voltage drops $3.3V - 2.3V = 1V$ and then we use that to get the amps $1V / 30mA = 0.033 = 33Ω$. But since I had no $33Ω$ at hand I used a $330Ω$ instead.

# Platform

The IoT platform I selected was the Arduino IoT cloud because it is made for Arduino products and it is also free to use, so it was a natural choice. The Arduino IoT cloud was nice to work with, as it came with a lot of pre-generated code for arduino devices in terms of connecting the device to the cloud.



# The code
The whole structure can be viewed [here](https://github.com/vl222ky/room_temperature/tree/28546f3b13bc19b50fd4e1afdb174e3417736e63/temperature_jun26a).

Here we have the code that calculates the temperature. It is done by using both an internal and external temperature sensor.
```cpp
  #include <Arduino_LSM6DSOX.h>
  
  //resets the temperature value each loop.
  avg_temp = 0;
  
  for (int external_samples = 0; external_samples < 10; external_samples++)
	{
	// reads the external temperature sensor
  int externalSensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = externalSensorValue * (3.3 / 1023.0);
  // Convert the voltage to temperature in degrees Celsius
  float temperature = voltage - 0.5;
  temperature = temperature / 0.01;
  avg_temp = avg_temp + temperature;
  //// reads the internal temperature sensor
  int internalSensorValue;
  IMU.readTemperature(internalSensorValue);
  avg_temp = avg_temp + internalSensorValue;
	}  
  ```

# Transmitting the data / connectivity

The temperature data is sent to the cloud every 10 seconds over wifi using the MQTT protocol.
The choice of wifi was for simplicity reasons, as there is pre-generated code for it from the Arduino IoT cloud. 

Getting the data from the device to the cloud was quite simple as most of it was pre-generated.
To demonstrate how it works, we must first look at the main file. There is a function from our header file that sets up the variable we want to get a read on, along with the delay between each read.
```cpp
initProperties();
```

```cpp
float avg_temp;

void initProperties() {

  ArduinoCloud.addProperty(avg_temp, READ, 10 * SECONDS, NULL);
}
```
In our main file we will also find the line connecting us to the wifi.
The setup to this line, will also be found in our header file.

```cpp
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
```
The SSID and PASS will be defined in another file aswell.

```cpp
const char SSID[]     = SECRET_SSID;    // Network SSID
const char PASS[]     = SECRET_PASS;    // Network password
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
```
Once everything is set up, we can start the loop and gather our data.
The data we want to gather will be sent at the start of each loop, and it uses the information from initProperties() to know what it should send each time.
```cpp
ArduinoCloud.update();
```



# Presenting the data
Because my project was just showing temperature, the dashboards many options, became quite few. So, what I decided to go with was just something to show the current temperature, and one to show the change throughout the day.

![](https://i.imgur.com/mVROLA8.png)

The data is sent from the device to the cloud in 10 second intervals, and will be preserved  for 24 hours.

# Finalizing the design
![](https://i.imgur.com/ZTnXAqD.jpg)

The microcontroller I ended up with was different from the ones that the Teacher assistants of this course could help with. Because of this, I had to solve a lot of my problems alone, and it ended up taking a lot longer than it should have.

There was also the problem that the microcontroller was not being able to run some code that was standardized for most of the other Arduino products, which resulted in a disconnect from the cloud each time my code ran. It took me over a week to solve this problem alone.

But in the end, this project has been very educational and fun. I learned a lot both about IoT, circuits, and sensors and even a bit of a new programming language.