Connecting a sensor to your Arduino MKR WAN board


In this section we are going to take some actual measurements and send them to The Things Network. The sensor we will be using is the DS18B20 waterproof digital temperature sensor. But before we do so, we shall have a closer look at the MKR WAN board.

Aims

  • We will be using an Arduino MKR WAN 1310 to which we will connect a DS18B20 temperature sensor.
  • We then write a program that will take sensor readings and prepare them for upload to The Things Network.
  • Upload the program to the board and test

Research

  • This video explains the use of the DS18B20 temperature sensor:
  • We will be using a version of the DS18B20, which features an integrated ‘pull up’ resistor.

Arduino with DS18B20

Equipment

My shopping list included the following items:

Procedure

Note: do not connect the Arduino board to your computer while it is still on the foam mount that it is supplied with (as shown below). The anti-static coating on the foam mount can have adverse effects on the board.

Arduino MKR WAN side view

The Digital Pins can read data from sensors. This data can then be processed by the board. The board can also power these pins on or off, meaning that it can control external devices.

The JST connector stands for ‘Japan Solderless Terminal’. This is where we could connect a 3.7V battery to power the board when not connected to a computer by a USB connection. The presence of the JST connector means that this is a MKR WAN 1310 board, as the MKR WAN 1300 board uses screw connectors to attach the battery.


Arduino MKR WAN battery side view

On the other side of the board, we see more digital pins. We also see pins for powering external devices. Here is what some of the letters mean:

VCC = Voltage Common Collector – this supplies power to your sensors. The output is 5V when plugged into a computer, but 3.7V when powered from a battery.

VIN = Voltage Input - powers the board from an external supply. Generally not used, as there is the battery connector on the other side of the board.

GND = Ground – this is the ‘earth’, allowing you to complete the circuit to your sensors or other devices.

5V supplies five volts, but only when plugged into a computer, otherwise you are limited to the 3.7V supplied from the battery.


Installing the Arduino Drivers and Libraries

The sensor we are going to use with our Arduino is the DS18B20 waterproof temperature sensor. The sensor from this supplier features an integrated 4.7k Ohm resistor, which simplifies the wiring. The resistor is required, as connecting the temperature sensor directly to the power supply would create a short circuit.

Install OneWire library

In order to get our Arduino to read the temperature from this sensor we need to install two more libraries, the One Wire library and the Dallas Temperature library.
To install the library in the Arduino IDE, we will use the Arduino Library Manager. Go to Tools > Manage Libraries.

Type ‘OneWire’ in the search box, click return, and install the OneWire library by Paul Stoffregen.

install the Dallas Temperature library

Then, search for ‘Dallas’ and install the Dallas Temperature library by Miles Burton.


Connecting the Temperature Sensor to the Arduino

The DS18B20 is connected to the Arduino with three jumper wires, one for the live supply, one for earth, and one for data (the temperature measurements).

The DS18B20 is connected to the Arduino with three jumper wires

Here I have used a brown wire for the live supply, blue for the ground, and white for the data, but you can use any colours you like. The Dupont connectors at the end of the wires just push into the pins on the boards. The board with the resistor comes supplied with the DS18B20 sensor.

The white data wire is pushed into pin 4 on the far side of the board (see the board images above).

The green terminal to the left of the Arduino board is a screw terminal for a battery. This means the board is a MKR WAN 1300. The 1310’s have JST connectors (like the 4.7k resistor board at the top of the image).

Wire connections

Left image: brown wire connected to VCC and blue wire connected to GND. Right image: white wire connected to pin ~ 4.

Uploading the sketch

Open the Arduino IDE that we were using in section 2. Click on File > New, or click on the ‘page’ icon, to open a new instance of the IDE with an empty sketch. Save the sketch to your computer.

Make sure your Arduino is plugged into your computer and that you have selected the correct port number (go to Tools > Port to check). Then copy and paste the following code into the sketch. Make sure that the code for ‘void setup()’ and ‘void loop()’ are pasted into the correct sections. You can also download the code from GitHub by following this link: Arduino-sketch-to-read-temperature-data-and-send-to-the-things-network. 

#include <MKRWAN.h>
#include <OneWire.h>

#include <DallasTemperature.h>>

LoRaModem modem;

// Please enter your sensitive data in the arduino_secrets.h tab
#include "arduino_secrets.h"

// Data wire is conntect to the Arduino digital pin 4
#define ONE_WIRE_BUS 4 // labelled ~4 on the mkr wan board

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

float tempVariable = 0; // initialises a variable to take temperature data

// We need the details below to connect your device to LoRa
// Read arduino secrets
String appEui = APP_EUI;
String appKey = APP_KEY;

void setup() {
// Put your setup code here, to run once:

Serial.begin(9600);
while (!Serial && millis() < 10000); //This line is needed to work with batteries https://forum.arduino.cc/t/it-does-not-work-powered-by-2xaa-batteries/520097/5

sensors.begin();

// The code below connects your device to LoRa
if (!modem.begin(EU868)) { // change this to your regional band (eg. US915, AS923, ...)
Serial.println("Failed to start module");
while (1) {}
};

Serial.print("Your module version is: ");
Serial.println(modem.version());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);

if (!connected) {

Serial.println("Something went wrong; are you indoors? Move near a window and retry");
while (1) {}

}
}

void loop() {
// put your main code here, to run repeatedly:

// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();

Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" - Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
// Populate our tempVariable variable.
tempVariable = (sensors.getTempCByIndex(0));

byte payload[1]; // The [1] means we are sending one item in the array. Arrays start at zero.
payload[0] = tempVariable;

delay(1000); // Gives it time to execute.

modem.beginPacket();
modem.write(payload,1);
int err = modem.endPacket(false);
if (err > 0) {
Serial.println("Data Sent");
} else {
Serial.println("Error");
}
Serial.print("temp data sent is: ");
Serial.println(tempVariable);

delay(30000); // This will pause the sending interval for 30 seconds. This is OK for testing, but change to 10 or 20 minutes for a production version.

This sketch is performing two tasks. Firstly, it is taking the temperature measurements and then it is transmitting them over the air to your TTN account using LoRa radio.

For the second part of the task, it requires your Arduino secrets taken from your arduino_secrets.h file. This is just a text file stored in the same directory as your .ino sketch. It has the general form as this:

//leave the APP_EUI filled with zeros.
#define APP_EUI "0000000000000000"
//Replace the Xs in the APP_KEY below with your key copied from your TTN account.
#define APP_KEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"

You will also find this on GitHub link above. 

Serial monitor output

When you are done, click the ‘Upload’ button (the second icon from the left). Once the sketch has finished compiling and has uploaded, click the ‘serial monitor’ button (on the right) in order to monitor progress. You should see something like this:




Here you see the temperatures being reported. The line ‘Data Sent’ also confirms that the temperature reading has been sent to your TTN account.

The sketch here is currently looping once every 30 seconds. That is under the control of the last line of text in the sketch delay(30000);. The Arduino measures time in milliseconds, which is why 30 seconds becomes 30000. This is only for testing purposes. LoRa is low bandwidth, so data should be sent no more frequently that once every five or ten minutes. Make sure that this line is changed for a live deployment.

We can now read temperatures and send them to server on the internet. But if you need to make decisions based on this information, then you will need a means of viewing and recording the data. Especially, as once in the field, your Arduino will no longer be connected to the serial monitor.

Here is the link to GitHub again: Arduino-sketch-to-read-temperature-data-and-send-to-the-things-network.

If you wish to check the hexadecimal code being sent to TTN, here is another hexadecimal to decimal converter.

The next section covers data visualisation, by sending the information to Datacake. Click here for the next steps.

© Copyright 2025 Dr Dulcamara - All Rights Reserved