This project shows how the visual language XOD can be used to program a simple temperature logger. If you haven't used XOD before, I recommend taking a look at the following resources:
The device has two temperature probes and writes date and time-stamped temperature measurements to a micro SD card every minute. Date, time, current temperature and max/min temperature are displayed on an LCD. Many different microcontroller boards could be used for this project. I've chosen an ESP8266, because they are cheap and relatively compact. N.B. XOD addresses the pins on the ESP8266 using their GPIO numbers.
XOD PatchesThe XOD patches required for this project are contained in the following xodball:
When you open this project in the XOD IDE dependencies will be automatically installed.
Temperature sensorsThe DS18B20 sensor offers high accuracy temperature measurements with a resolution of up to 12 bits (0.0625°C resolution). It has an accuracy of ±0.5°C over a temperature range of -10°C to +85°C, making it suitable for a wide range of temperature sensing applications. The DS18B20 uses the 1-Wire communication protocol, allowing multiple sensors to be connected to a single data line. Each sensor has a unique 64-bit ROM code that enables it to be individually identified and addressed on the bus. In this project the waterproof version of the DS18B20 sensor will be used.
The three wires correspond to the following pins on the DS18B20:
- red = VDD
- black = ground
- yellow = signal
Start by marking the two sensors so that they can be identified by eye. Here I have used green and brown insulation tape.
The next step is to read the ROM code (address) of each sensor in turn. Connect the first sensor to the ESP8266 as in the diagram below.
- The red wire (VDD) is connected to VIN on the ESP8266. The board will be powered via USB and so VIN will provide 5V.
- The black wire (ground) is connected to a GND pin on the ESP8266.
- The yellow wire (signal) is connected to GPIO0 (labelled D3) on the ESP8266.
- A 4.7kΩ resistor connects the signal and 5V lines.
Open temperature-logger-hackster.xodball in the XOD IDE, then go to the patch called read-ds18b20-address:
Run the patch in the debugger. The eight byte (64-bit ROM code) will be displayed in the watch node at the bottom-right of the patch. N.B. the get-device-countnode outputs one for both Ndev (number of devices on the one-wire bus) and Ntemp (number of DS18B20 devices). Make a note of the address and then repeat the process for the 2nd sensor.
The get-temp-c node uses these eight byte addresses to read a temperature measurement from a specific sensor. This will be demonstrated later.
Enclosure
Find a suitable enclosure for your device. I upcycled an old LTO tape case. Using a drill and craft knife I cut apertures for the LCD, temperature probes and the USB port of the ESP8266.
Temperature sensors
The DS18B20 sensors are wired in parallel:
- The red wires (VDD) are connected to VIN on the ESP8266. The board will be powered via USB and so VIN will provide 5V.
- The black wires (ground) are connected to a GND pin on the ESP8266.
- The yellow wires (signal) are connected to GPIO0 (labelled D3) on the ESP8266.
- A 4.7kΩ resistor connects the signal and 5V lines.
LCD
The LCD chosen for this project can display 4 rows of 20 characters. The I²C (Inter-Integrated Circuit) bus will be used to communicate with the LCD. A serial to I²C adapter can be added to a serial LCD.
The brightness of the display is controlled using a potentiometer on the I²C to serial adapter.
Make the following connections between the LCD and the ESP8266:
- Connect SCL on the LCD to GPIO5 (labelled D1) on the ESP8266
- Connect SDA on the LCD to GPIO4 (labelled D2) on the ESP8266
- Connect VCC on the LCD to VIN on the ESP8266
- Connect GND to GND (common ground)
Real-time clock
The I²C bus will also be used for the real time clock (RTC) module. Make the following connections between the RTC and the ESP8266:
- Connect SCL on the RTC to GPIO5 (labelled D1) on the ESP8266
- Connect SDA on the RTC to GPIO4 (labelled D2) on the ESP8266
- Connect VCC on the RTC to VIN on the ESP8266
- Connect GND to GND (common ground)
Micro SD card module
The Serial Peripheral Interface (SPI) bus will be used to communicate with the micro SD card module. Make the following connections between the micro SD card module and the ESP8266:
- GND - GND
- VCC - VIN (5V)
- CS (chip select) - GPIO15 (D8)
- MOSI (master out slave in) - hardware MOSI (D7)
- SCK (serial clock) - hardware SCK (D5)
- MISO (master in slave out) - hardware MISO (D6)
The microSD card should be formatted as FAT16. Tools for formatting the microSD card can be found here:
Once formatted, the microSD card can be inserted into the microSD module. The microSD card must be inserted into the microSD module before the device is switched on.
Assembled device
Use the set-rtc patch to set the date and time of the RTC. Run the patch in debug mode.
A date and time can be specified using the tweak-number nodes. Fire a pulse from the tweak-pulse node to set the RTC to the specified date and time, then check that the correct date and time are shown in the watch node.
Temperature logger patchThe XOD project contains a patch named main. This is the top-level patch for the temperature logger. The eight byte addresses of the get-temp-c nodes should be changed to match the addresses of your own probes before uploading the patch to the ESP8266.
Here is a brief description of the main patch:
- 1. The shutdown-wifi node is self-explanatory and is used to minimize the energy consumption of the device.
- 2. A one-wire bus for the temperature sensors is created using ds18b20-bus. The resolution of all sensors on the bus is set to 12 bits by the set-resolution.
- 3. A clock node emits pulses at 1 second intervals.
- 4. The rtc node reads the real-time clock each time it receives a pulse from the clock node (i.e. every second). Date and time are sent to the DT bus.
- 5. The date and time from the DT bus are formatted as a string and sent to the DTS bus. This datetime string will be displayed on the LCD and written to the micro SD card. The unpack-datetime node is used to extract the minutes from the time contained in the DT bus. A pulse-on-change node connected to the min output of unpack-datetime emits a pulse every minute and is used to trigger a write to the micro SD card.
- 6. The request-temperatures node commands all DS18B20 devices on the one-wire bus to perform a temperature conversion. The get-temp-c nodes are then used to read the measurements. Each get-temp-c corresponds to a specific sensor uniquely identified by its eight byte address (see section "Temperature sensors" above).
- 7. The format-number node sets the precision of the temperature measurement to one decimal place and converts the number to a string. The temperature readings are sent to buses BTemp and GTemp for the brown probe and green probe respectively.
- 8. A min-max node keeps track of the minimum and maximum temperature recorded by each probe.
- 9. The write-to-lcd node sends data to the display. The display is updated every second. See below for more detail.
- 10. The write-to-sd node sends data to the SD card at intervals of one minute. See below for more detail.
The screenshot below shows the patch for the write-to-lcd node. N.B. the LCD module I used had an I²C of 27h. If a different LCD module is used this address may need to be changed on the text-lcd-i2c-20x4 node (highlighted).
The screenshot below shows the LCD in action. Date and time are displayed in the first row. The 2nd row provides a header for the temperature measurements. The third row displays the current temperatures reported by the brown and green probes respectively. The range (min-max) of temperatures recorded by each probe are shown in the bottom row.
The screenshot below shows the patch for the write-to-sd node.
The temperature logger will start displaying and recording data the moment it is switched on.
TroubleshootingA note of caution: if either of the probes gives a reading of 85, check the wiring (see discussion here: https://www.raspberrypi.org/forums/viewtopic.php?t=63085). If a sensor becomes disconnected it will output -127.
Visualizing temperature logsSwitch off the temperature logger before ejecting the micro SD card from the micro SD module.
The log.csv
file can be opened and viewed in a spreadsheet application or text editor (see screenshot below). There is no header line in the log file.
The data in the log file can be plotted using any spreadsheet or statistics application.
I produced the plot above using the following R script: https://github.com/WaylandM/xod-temperature-logger/blob/main/scripts/plot_temperature_log.R
SummaryThis project hopefully demonstrates how easy it is to build a simple data logger using the XOD visual programming language.
Comments
Please log in or sign up to comment.