Digital Thermo-Hygrograph

(UNDER CONSTRUCTION)
This post, like others, is intended to be a more detailed description of my work and to share experience and advice with colleagues. If interested in the code, ask for git.
The project was oriented to upgrade the sensor being used for experiments by members of the phytotherapy department of the National University of Central Buenos Aires.
The specifications were a digital sensor capable of monitor the relative humidity and temperature of the greenhouse, where the experiments take place, saving the data of one week up to a month, powered by a 9v battery. The data should be downloaded with a computer in .csv format.
The project was broken into several parts:
  • Study of the alternatives.
  • Budget elaboration.
  • Realisation.
  • Testing and validation.
Starting with the study of the alternatives found into the market, the use of the si7021 A20 was the most convenient in terms of reliability, cost and instrumentation. The si7021 A20 is a relative humidity and temperature sensor with i2c interface. Data is delivered in 2 blocks of 2 bytes each one, one block for relative humidity (RH) and the other for the temperature. Both have configurable resolution, being the default 12 bits for RH and 14 bits for temperature.
The sampling rate needed to be one each half an hour. This means 5952 bytes of data per month. To allow future possible experiments that demand several months, a 24LC512 eeprom memory was chosen in order to handle up to 11 months of continuous working.
The date and time of the experiments are one of the main interests, so to keep track of the time, a real time clock (RTC) was added to the design. The DS1307 was the only one available in the local market.
As a processing unit, a PIC16F886 was used due to local availability and low price.
The preliminar budget for the device, consisting of the 4 ICs plus the components and batteries was less than 10 USD, so it was suitable for the client.
Realisation
The si7021 bought comes mounted on a board with the resistances for the i2c interface and a voltage regulator for 5v compatibility. The device is the following:
si7021 - A20
It has 2 configurable registers, the default configuration is appropriated for the application so none initial configuration is needed on startup. To perform the measures, several registers can be used. For this application, the measures are made independently one of each other and with the clock stretching method to indicate to the master that a measure is in progress. The data received must be treated to calculate the real values, to release the microcontroller from floating point calculations and transmission, the calculation is performed on the computer side.
Data is stored in the eeprom in big-endian format. The 24LC512 has 64 KBytes of storage capacity. The lower part, from 0x00 to 0x0F is used to save initialisation information and the memory pointers for each quantity. The last 140 bytes the memory are used to save the time and date of upto  10 start/stop acquisition events. The remaining memory is split into 2 equal parts giving approximately 341 days of total storage capacity.
The RTC has 8 one-byte-wide registers destined to configure the date and time, enable the clock and other special features. The data in the registers is presented in BCD format, so some software processing is required to read and save data properly. Splitting the byte into two nibbles and adding the lower with ten times the second is the easiest way to convert the data read. To write the values, the conversion is made in the computer and sent to the microcontroller ready to be saved. Similarly, a loop counts how many tens fit in the number and then calculates de modulo by ten of the original number to get the reminder, then both values are pasted as nibbles in the correct order, thus calculation load is reduced in the microcontroller. 
Since the system is supported by a battery, consumption is crucial. A CR2032 coin battery is connected to the RTC as backup supply, keeping always the time track either the 9v battery is connected or not. The eeprom is a non-volatile memory so data is never lost when the supply is disconnected.
The firmware was written in c language using the XC8 compiler in MPLAB X IDE. A pickit 2 was used for debugging purposes. There was no third party code used, all libraries, for controlling the i2c interface, to handle the memory, the RTC and the sensor were written from zero for this project. If somebody is interested can contact me and will share them for free.
The communication between the computer and the system was made by serial communication, using the UART of the PIC along with a serial to USB converter that the client had. The later was based on a CH340G chip.
Some considerations to be taken into account when developing solutions with the above devices (based on problems that I encountered):
PIC
  • The configuration bits, the LVP bit will cause misbehaviour if not configured properly.
  • A pull up resistor on the UART Rx pin will help to avoid noise when (dis)connecting the port and false interrupt triggering.
  • The main function call graph should be consulted frequently if many functions are being used to avoid possible stack overflow.
RTC
  • Choosing the right crystal is important, wrong parameters will cause inaccuracy or even no functioning.
  • Noise isolation, even with the right crystal some inaccuracies may be present, ideally, when prototyping, I would recommend you make a board with datasheet's recommended layout.
  • Do not hesitate to use the square wave output to debug, it is possible to check if the crystal is working by connecting a led or a volt meter. If you connect all, you clear the CH bit and the count does not increment and you think everything is OK, touch the crystal with your finger, if it starts to count, although erratic, it meas bad crystal selection or noise isolation problems. If not, the device could be damaged.
SENSOR
  • The acknowledging sequence by the master is crucial to get the LSB of the measurements, otherwise, you will get always 0xFF. When receiving the MSB, the master must ACK and after receiving the LSB the master must NACK. On the PIC side, this is done by correctly setting or clearing the acknowledge data bit (SSPCON2 register) before enabling the masters' ACK. If not done properly, the device will no longer respond and the entire i2c bus will stop working.
Software interface
To be able to download data from the device, an interface was developed in Visual Basic using Visual Studio. 
App main interface

This simple interface has 3 functions: real-time monitoring, date and time setting and data saving.

The application uses the COM port to communicate with the device. A drop-down list is used to list available COM ports, once selected the corresponding one, the "ABRIR" (open) button opens the port. Neither date setting nor monitoring nor data saving is allowed before the port is open. 

The main function is to save data generated by the device. To accomplish that, once the port is opened, the button "GUARDAR" (save) is enabled and, after downloading the data from the device, a save dialogue is prompted with a .csv filter. If desired the user can free the memory by clicking on "LIBERAR MEMORIA" (clean memory), this button is enabled only after data saving to avoid data losing risks.

The lower shaded text box is destined to "real-time" monitoring. It actually has a refresh rate of 1.2 secs approximately, but is enough fast to the scope of the feature, considering that the normal sample rate is two samples/hour. This function is activated via Menú > Monitor. 


Menu
To set the time and date to the RTC, the user can opt between manual or automatic setting. This is accomplished via Menú > Grabar fecha > Manual/Automático. In case of choosing automatic, the time and date shown at the top-right corner is transmitted to the device (in Argentina day/month format is used). In case of choosing manual, the user will be prompted to a second window with 5 fields consisting of drop-down lists. Leap years and 30 day's months are taken into account to avoid misbehaviour of the RTC.
Time and date manual setting
After the debugging stage on a breadboard and tested for several days, the whole system was performing the tasks very well so it was time to implement the system on a PCB.
The board was designed in Altium on an FR4 5x6.5cm single layer board with soldermask and silkscreen. 
PCB layout
Since the system runs on 5 v and the power supply is a 9 v battery, an LM7805 was added. The current drawn out from the regulator is small, so a heat-sink is not necessary. There are two switches, one used to turn on/off the device and other to start/stop the acquisition. 
The ICSP connector for the pickit was added for debugging purposes and firmware update.

Testing and Validation
With all components mounted on the board, the sampling time was adjusted to 25 ms to simulate almost 2 months of work in 11 minutes. The result was a .csv document with 2753 samples as shown in the following picture.
An Excel sheet showing the data generated
The columns' labels are in Spanish, from left to right, Date / Time / Humidity / Temperature.
The time step in the example shown is 30 minutes because this is the step desired by the client.
A second test with a sample rate of 1 sample/min took place for almost 4 days, performing 5547 samples successfully.
Although the client needs the 9 V supply feature, using a 9v battery and a regulator represents a problem of autonomy since the whole system consumes around 5.2 mA, from which 4.3 mA is consumed by the regulator.
To improve the autonomy and cover an entire working period, the "PWR" connector on the board can be used to supply directly the system, and thus, avoid the consumption of the regulator. The user can unplug the 9v battery, remove the jumper from PWR and connect a AA battery pack of 3 batteries in series. The current drawing descended to 0.9 mA approximately. In the worst case, the capacity of these batteries is 1100 mAh, which gives the system an autonomy of 50 days.

Comentarios

Entradas populares de este blog

Weighing Scale