logo smart home family kruse

Kruse Smart Home

Easy living in our smart home & Garden

How to measure temperature with Arduino Nano 33 IoT and DS18B20?

Arduino Nano 33 IoT temperature reading of DS18B20

15 December 2021

Jan-Willem

1. Introduction

This is my first real project with an Arduino board where I will use the Arduino Nano 33 IoT to measure outdoor air temperatures and my fish pond temperatures. This remote outdoor temperature measurement device should provide the following functionality:

  • temperatures available via the Homey Pro and the Arduino IoT cloud;
  • using WiFi connectivity of the Arduino board;
  • OLED display showing measured temperatures;
  • power provided by an battery or accu;
  • the board, OLED display and battery/accu will be placed inside a waterproof housing.

I am a beginner w.r.t. to Arduino boards and so to help others I provide my journey towards my end goal. It is not complicated if you have some basic understanding of one or more programming languages and interest in electronics.

2. Requirements

The following materials are required:

  • Arduino Nano 33 IoT
  • DS18B20 waterproof sensors
  • Waterproof casing
  • USB cable USB-A to USB mini to connect the computer with the Arduino board, while at same time providing the power for the board
  • Accu
  • More to come

3. Gathering knowledge

Thinking about the goal I have with this project a lot of questions about measuring temperatures with the Arduino Nano 33 IoT popup:

  1. What functionality does the Arduino board provide?
  2. How to connect the temperature sensor to the Arduino board and read the temperatures?
  3. How to connect the OLED display and get measured temperatures displayed?
  4. How to power the board with a battery or accu & which ones works the best at low ambient temperatures (below zero)?
  5. How to connect the board via WiFi?
  6. How to get the board connected to Arduino IoT cloud?
  7. How to make the measured temperatures available to Homey Pro?
  8. What is the maximum amount of DS18B20 temperature sensors I can connect?
  9. How do I know the accu is empty?
  10. How to get the device in a waterproof casing, including visible OLED display and wires going in/out?

The answers to these questions are provided in the next sections.

Additional information can be found on the Arduino website in the article Connecting the Nano 33 IoT to a Wi-Fi Network.

3.1 Functionality of the Arduino Nano 33 IoT

When looking around for the smallest Arduino board available and having interest in the Internet of Things (IoT) I decided to go for the Arduino Nano 33 IoT board for this project. I bought the book “Beginning Arduino Nano 33 IoT: Step-By-Step Internet of Things Projects” written by Agus Kurniawan. This book of approximately 180 pages does not have lots of technical details, but contains essential information in a snappy manner to get you on the road to build projects with this Arduino board. It provides also a summary of the Arduino development language.

For more technical details I went to the official Arduino website for this board.

3.2 How to connect the DS18B20 temperature sensor and read it out?

A half year ago I played already a little bit with the Arduino Uno R3  to measure temperatures with the DS18B20 sensor. This involved gathering knowledge about this sensor, which I published in my article “The DS18B20 digital temperature sensor“.  To check for differences with the Arduino Nano 33 IoT I searched the web for others using this board with DS18B20 sensors and found a blog describing setting up a pool water monitoring system. Part 3 of this blog describes using the Arduino Nano 33 IoT with a DS18B20 sensor to measure temperature.

This article convinced me that my goal is possible and gave information how to make the correct connections. Based on the information collected so far I used the software Fritzing to create a schematic how to position and connect the parts on a breadboard. See the images below.

My DS18B20 sensor has 3 wires instead of 4 as shown in the schematic of the breadboard. The yellow wire (data) from my sensor is the same as the white wire in the schematic. The orange one in the schematic is not applicable. I connected the Arduino board to my computer via USB and started the Arduino IDE. I used the following code to test that temperatures are being measured.

An example of the output shown in the Arduino IDE serial monitor is shown in the next image.

3.3 Displaying temperature measurements on an OLED display

The final device should have a display to see measured temperatures locally next to seeing them via Homey Pro or the Arduino IoT cloud. I had already an OLED display with the following specifications:

  • OLED 128×128 pixels, white text
  • controller SSD1327
  • works via  I2C protocol
  • required power 3.3V or 5V
  • dimensions 45.5 x 34.3mm

I bought this display from the Dutch online store tinytronics.nl, where it is listed as “1.5 inch OLED Display 128*128 pixels wit – I2C” and costs €10 (2021 price).  It is one of the few 128×128 OLED displays currently available.

3.3.1 Connecting

This OLED display has the following connections (I2C protocol):

  • GND      Ground
  • VCC        3.3V or 5V power in
  • SCL         I2C SCL (clock signal)
  • SDA        I2C SDA (data signal)

The arduino has these connections in the signal lines indicated within a blue box in the schematic below.

So I connected the OLED display with the Arduino board:

When I connected the wires, the OLED display remained dark (I had no worries) when the board was powered up. Time to get some test code up and running.

3.3.2 Displaying text

The supplier of the OLED display indicated that the Arduino library U8G2 library from olikraus should be used. So I installed this library via the Arduino IDE library manager (just search for U8G2). Additional information provided by the supplier of the OLED display:

  • The Arduino Uno/Nano/Pro Mini do not have enough memory (RAM), hence it cannot be used with the “full_buffer” examples of the U8G2 library. Select instead the “page_buffer” examples
  • Use the following initialisation under the section “U8g2 Constructor List” in the example code to use page-buffer:
    U8G2_SSD1327_MIDAS_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

I started with a simple example showing “Hello World!” on the OLED display to check everything is working fine. The example code HelloWorld from the u8g2 library can be loaded in the Arduino IDE via File > Examples > U8g2 > page_buffer > HelloWorld. I modified the example code and cleaned it up to the bare minimum:

3.3.3 Display temperatures continuously in terminal

To have a continuously ongoing display of measured temperatures I loaded the example code ‘Terminal’ from the u8g2 library via File > Examples > U8g2 > page_buffer > Terminal. This code was modified to include learnings from the previous sections Arduino code:

3.4 Battery powering the Arduino Nano 33 IoT?

Now we have a working device it is time to get it working standalone with its own power source. Looking on the web for ways how to power the Arduino Nano 33 IoT board with a battery and specially which voltage range is accepted before damaging the board was a daunting task. A lot of people seem to be confused by info w.r.t. max acceptable 3.3V on the analog and digital lines versus the power to supply to the board. It turns out that the power supply can be in the range of 4.5 – 21V DC as stated on the packaging of the Arduino board.

See also the Arduino forum article “Nano 33 IoT Vin Voltage range“.

3.4.1 Using a 9V battery

I started with a 9V battery and connected it as shown below.

The device works now without an USB connection providing the power. I left it running to check how fast the battery would drain. It turned out to be approximately 24 hours, so not useful in this setup. This does not worry me at this moment as it is likely caused by a combination of:

  • continuously having on & updating the OLED display
  • reading the sensor and updating the OLED display every second

This can be solved later and worst-case adding a button to press before something is shown on the OLED display.

3.4.1 Using a lithium ion accu

Another is the wish to use rechargeable accus. I have two 18650 Lithium ion rechargeable accus of each 3.7 Volt, which should roughly double when placed in series. I have a holder for these accus and measured the voltage, which is about 8.3 Volt in series when fully charged. So within the required power bandwidth of the Arduino Nano 33 IoT.

3.5 How to connect the board via WiFi?

3.5.1 Scan for available WiFi networks

Before the WiFi connection of the Arduino Nano 33 IoT can be used the WiFiNINA library needs to be installed in the Arduino IDE via the library manager. Search for “WiFiNINA” from Arduino.

With this library also new code examples are installed, which can be loaded in the Arduino IDE via File > Examples > WiFiNINA. There are 2 example codes to scan 802.11b/g WiFi networks available and list them in the Arduino IDE serial monitor:

  • ScanNetworks
    Displays all WiFi networks in range
  • ScanNetworksAdvanced
    Displays all WiFi networks, including the encrypted ones.

Both example codes will only list available networks and their signal strengths, not connect to a network. Load the file ‘ScanNetworksAdvanced’. No modifications are required to the code, so upload it to the Arduino board and open the serial monitor in the Arduino IDE.

Note: In case the warning is displayed to update the WiFi controller firmware, see the Arduino website for the article “Check and update the firmware for WiFiNINA and WiFi101“.

3.5.2 Connect to a WiFi network

To connect to a WiFi network and obtain an IP address I started with the example code ‘ConnectWithWPA’  as my WiFi network uses WPA/WPA2 to encrypt. This script will also open the file ‘arduino_secrets.h’, which needs to be modified contain your WiFi network’s credentials. This file will be included in the main code when compiled before uploading to the Arduino board. The correct SSID can be fetched from the output created by the earlier used scan networks routine.

I modified this example code extensively based on information found in the WiFiNINA reference guide and info found elsewhere on the web to add:

  • Extensive and nicely formatted WiFi information for the WiFi access point and the Arduino board within the Arduino IDE serial monitor;
  • Set a network host name for the Arduino board;
  • Every 5 seconds flash the internal orange LED of the Arduino Nano 33 IoT;
  • Reconnect to the WiFi access point if the connection gets lost.

Download/copy the next two pieces of Arduino code and upload it to your Arduino Nano 33 IoT.

3.5.2.1 Arduino code part 1/2

3.5.2.2 Arduino code part 2/2

Do not forget to update the following two lines in the code below

3.5.2.3 Serial monitor example output

Examples of the output in the serial monitor is shown below.

So far the code to read out the DS18B20 temperature sensor and connecting to the WiFi network has been created. It is now time to get some temperature measurements online via the Arduino IoT cloud.

3.5.2.4 Arduino code split-up

As the main code file becomes too large for easy handling I copied the WiFi routines to a separate file. So the files of the project are now:

  • Connect to Homey Pro.ino –> main code
  • jwWiFiRoutines.h –> WiFi routines
  • arduino_secrets.h –> SSIDD and password for WiFi connection
Icon
Arduino code files

Code for Homey Pro interfacing

3.6 Making temperature measurements available via the Arduino IoT cloud

Based on information provided in the article Getting started with the Arduino Nano 33 IoT , I created an Arduino Create account, installed the required Arduino Create plugin on my computer and went to the Arduino IoT Cloud website.

So if I would subscribe to the Maker Plan from The Netherlands I need to pay yearly $87 dollars including tax. Hmm… this is too expensive for me as there are cheaper alternatives available. Also data retention is only 3 months.

So I will first concentrate on interfacing with Homey Pro and maybe later look for free alternative IoT cloud options.

3.7 Interfacing with Homey Pro

Connecting Arduino Boards works by installing an app on Homey Pro; the Homeyduino app. This app allows you to connect your Arduino based creations with the Homey Pro. Next to installing the app on Homey Pro you also need to install the Homeyduino Arduino library on the Arduino board. A usage guide, Homeyduino API reference and a set of example projects are available. Flows can be created on the Homey Pro to define actions based on information received from an Arduino board.

The Homey Community forum has also posts w.r.t. using the Homeyduino app. Be aware that the company Athom is not supporting this app anymore, but it still works.

3.7.1 Test code to interface with the Homeyduino app

I loaded the example code “dht11-esp8266” via File > Examples > Homeyduino > Example projects > dht11. Although it cannot be used straight away for the Arduino Nano 33 IoT board, it is helpful in writing my own code for the Nano 33 IoT.

The setup routine contains:

The loop routine contains:

The update sensor routine contains:

From the usage guide:

  • Homey.begin("devicename") in the setup() routine: the device name entered should be unique for all Homeyduino devices in your network;
  • Homey.loop() in the loop() routine: homey.loop function needs to be executed as often as possible, therefore use of Arduino’s delay() function should be avoided at all cost. Alternative delay methods are described in the Homeyduino usage guide;
  • The Arduino device can be integrated with Homey flows by defining triggers, conditions and actions which will be available directly in the Homey flows.
With above info in mind I created the following test code (changes compared with earlier code highlighted in yellow):

When compiling the Arduino code I got the following error in the Arduino IDE:

fatal error: Ethernet2.h: No such file or directory #include <Ethernet2.h>
compilation terminated.

Hmm that is not what I wanted to see… The Homeyduino library info on the Arduino website states the following:

Homeyduino allows you to connect your Arduino projects with Homey.
This library communicates with the Homeyduino Homey app using an ethernet or WiFi connection, making it easier than ever to connect your projects with Homey. Homeyduino is compatible with all your regular Arduino boards, ESP8266 and ESP32 based boards.

Indicating it should work with the Arduino Nano 33 IoT.  The site above also provides a link to the documentation, where under the section welcome you will find a link to the usage guide.  This guide lists under “Hardware prerequisites” the following:

Homeyduino is compatible with all ESP8266 and ESP32 based development boards as well as Arduino boards compatible with the Arduino Ethernet v2 shield.

Support for using the legacy Arduino Ethernet v1 shield is also available, but you will need to change a configuration setting in one of the libraries files, click here for more information.

Other networked Arduino devices such as Arduino Yun and the Arduino GSM shield are currently not supported.

The software has been tested on the following board configurations:

    • Arduino UNO with ethernet v2 shield
    • Arduino Leonardo with ethernet v2 shield
    • Arduino Due with ethernet v2 shield
    • ESP8266 NodeMCU v1
    • ESP32 devkit

This is in conflict with the previous statement; it is clear that the Arduino Nano 33 IoT with WiFiNINA is not supported. Is there a solution available to get it working?

3.7.2 Modifying the Homeyduino arduino source code

I started looking at the Homeyduino library at GitHub to see if people made comments about supporting the Arduino Nano 33 IoT. Unfortunately this was not the case, but there was a post w.r.t. adding support for arduino uno wifi (rev2) under “Pull requests”. Opening this post and selecting “Files changed” provides a clue where to look for to get the Arduino Nano 33 IoT supported.

The fix for the Arduino UNO WiFi refers also to the WiFiNINA library. So I need to find a similar definition as ARDUINO_ARCH_MEGAAVR, but then for the Arduino Nano 33 IoT board.

The definition seems to be related to the CPU of the board, which is for the Nano 33 IoT the Cortex-M0 32-bit SAMD21. I started by doing a file search in all the arduino related folders to look for “ARDUINO_ARCH_” to find the file containing these definitions. It turned out that there is not one file with these definitions, but that the definition is set in the arduino.h file within the folder for the CPU type (samd):

C:\Users\<USER>\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.12\cores\arduino

Within this file the following code is found:

So likely the definition to add to the homey.h file is “ARDUINO_ARCH_SAMD”. In the homey.h file, before the following instructions:

I added:

Fingers crossed it will work.

Luckily id did. No compile errors anymore and hence I uploaded the code to the Arduino Nano 33 IoT. Later I discovered that the output of the compilation also shows some hints:

“C:\\Users\\ …/bin/arm-none-eabi-g++” …  -DARDUINO_SAMD_NANO_33_IOT -DARDUINO_ARCH_SAMD …”

This indicates “ARDUINO_SAMD_NANO_33_IOT” should be more specific. Modifying the code (only the first line) above  with this definition did work.

Note: to assure the Arduino IDE re-compiles the source code of homey.h, disable the inclusion of the homey.h library in the main code by using

Compile the code (which will give an error) and remove the // and compile the code again. No errors should be shown now.

3.7.3 Connecting the test code with the Homeyduino app

Via the Homey Pro desktop I added the device “DS18B20 sensor” via the Homeyduino app. Every 5 seconds it changes the displayed temperature from 12°C to 31°C, as we told the Arduino board to do so in the code. So the test code works and it is time to get real temperature measurements.

3.7.4 Realtime temperature measurements

Work in progres 🙂

To start providing real time temperatures to Homey Pro, the code to read a DS18B20 temperature sensor has been integrated into the latest code (interfacing with Homey Pro from section 3.7.1). Also an updateSensor routine has been added which is called every 10 seconds. The code is not in final polished form, but enough to test everything works as expected.

Code changes:

  • Serial renamed to SerialUSB
  • Interval to read temperature changed; measure every 5 minutes. if increase of more than 0.5c it will measure more frequent; each 30 seconds
  • interval –> HomeyInterval
  • defining multiple T sub devices: https://community.homey.app/t/homeyduino-device-with-two-temperature-sensors-registers-only-one/3721/13
    Problem: which sensor is what?
    Problem: Flow Cards will not be automatically generated for sub-capabilities, you should create these cards yourself.
  Homey.addCapability(“measure_temperature.T1”);
  Homey.addCapability(“measure_temperature.T2”);

 

 

Article by <a href="https://smarthome.familykruse.eu/author/iamjwk/" target="_self">Jan-Willem</a>

Article by Jan-Willem

Position

Writen by the website owner

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *