A Beginner’s data logger project using PIC12F683 microcontroller
It is a very simple data logger project based on PIC12F683 microcontroller. The microcontroller reads temperature values from a temperature sensor on a regular interval basis and stores them into its internal EEPROM memory. The recorded temperatures can be later transferred to a PC through serial interface. I originally published this project on electronics-lab.com last summer. I thought this could be a very good learning project for beginners, and so I am posting it here for Embedded Lab’s readers too.
The sensor used in this project is DS18B20. It is a digital temperature sensor manufactured by Dallas Semiconductor (now MAXIM) that can measure temperature ranging from -55°C to +125°C with an accuracy of ±0.5°C over the range of -10°C to +85°C. The sensor provides the measured temperature (°C) output in user-configurable 9, 10, 11, or 12-bit data corresponding to the desired resolution of 0.5, 0.25, 0.125, and 0.0625 °C. The sensor communicates with a host microcontroller over a 1-wire bus. Readers are suggested to refer the datasheet on Maxim’s website for details on this sensor. Please keep in mind that there are three versions of this sensors, namely DS1820, DS18S20, and DS18B20, and they have some architectural differences. All of them have the same pin configuration and therefore the circuit diagram would be same for all three types. However, some modification in the software may be required while the microcontroller reads the temperature data from them.
PIC12F683 has 256 bytes of internal EEPROM. Each temperature reading is stored as a byte, which means only the eight most significant bits of DS18B20 output is recorded. Therefore, the temperature resolution is decreased down to 1 °C. This temperature logger can store up to 254 temperature values (254 bytes) in its internal EEPROM. The remaining two EEPROM locations are used to store the sampling time and number of samples information. Three tact switches are used to provide user inputs for controlling the operation of the data logger.
The PIC microcontroller uses its internal clock source operated at 4.0 MHz. The DS18B20 sensor is interfaced to GP0 pin (7) of the microcontroller. An LED connected to the GP2 pin serves as the only display in the circuit to indicate various actions of the data logger. For example, it blinks every time a sample is recorded into EEPROM. The circuit is powered with +5 V derived from a 9V battery using an LM78L05 regulator IC. The LM78L05 circuit is a very common circuit and therefore, it is not shown here.
The three tact switches provide the following functions.
- Start: Starts data logging
- Stop: Stops the logging procedure
- Send/Reset: Transfers data to PC through serial port. However, if it is held pressed for 2 sec or more, the EEPROM locations are cleared and ready for new recordings.
Selection of sampling time
This data logger offers three options for sampling interval: 1 sec, 1min, and 10 min. The selection is made through the same three tact switches. Here is how it works. Suppose if 10 min sampling time is needed, then first turn OFF the power, hold the ‘Send/Reset’ button pressed, turn the power ON, and wait till the LED glows. Once the LED glows, release the button, and the sampling interval is set to 10 min. The new sampling time will be updated to EEPROM location 0 so that in case of power failure, the previous sampling time will be restored. Similarly, the use of ‘Start’ or ‘Stop’ button instead of the Send/Reset one sets the sampling time to 1 sec, or 1 min respectively. With 10 min sampling interval, this data logger can record temperature samples over 42 hours.
Serial Interface to PC
Transferring data to PC through serial port requires a voltage translation circuit to convert the TTL logic levels from PIC12F683 to appropriate RS232 voltage levels. A regular PNP transistor with few other passive components can do this job. The RS232 standard uses a -3 to -12 V for Logic 1 and +3 to +12 V for Logic 0. The required negative voltage is stolen from the TX pin of the RS232 port on PC’s side which is unused as there won’t be any data transfer from the PC to PIC12F683. Under idle condition the TX pin on PC’s side is held high (-12 V). The two figures below describe the operation of converting TTL levels for 1 and 0 to corresponding RS232 levels. The positive terminal of the 10 uF capacitor is grounded because its negative terminal has to deal with a more negative voltage.
The firmware for PIC12F683 is developed in C using mikroC Pro for PIC compiler from mikroElektronika. PIC12F683 doesn’t have built-in hardware UART module for serial communication but the mikroC compiler has built-in library routines to implement software UART through any digital I/O pin of PIC microcontroller. Due to wide use of DS18B20 sensor for temperature measurements, mikroC also provides 1-wire library routines for controlling the operation of this sensor. The built-in functions for communicating with a 1-wire sensor are,
- Ow_Reset is used for reseting sensor;
- Ow_Read is used for receiving data from sensor; and
- Ow_Write is used for sending commands to sensor.
MikroC also provides EEPROM library for read and write operations to internal EEPROM locations. The overall programming is made much easier with the help of these built-in library functions of mikroC. The source code with HEX files can be downloaded from the links below.
The configuration bits setup for PIC12F683 can be done through Edit Project window in mikroC. The figure below shows the required settings for this project.
When the circuit is powered on, the LED blinks for 3 times that shows the data logger is turned on and being initiated. The program checks for any key pressed for setting sampling interval. If yes, it identifies the key and store the appropriate sampling interval information to EEPROM location 0. The LED is turned on to indicate that the sampling interval has been set and it’s time to release the pressed key. The program is now inside the main loop. The three switches (Start, Stop and Send/Reset) operate in Interrupt-on-change mode, which means, any time the button is pressed, an interrupt is generated. Pressing the Start button begins data recording. Each time a temperature sample is recorded in to the EEPROM, the LED blinks to indicate that the logging process is going on. Pressing the Stop button will interrupt this process, while Send will initiate the serial transfer of recorded samples through GP1 pin (6). If the Send button is pressed for more than 2 sec, the entire EEPROM locations will be cleared.
On PC’s side, the hyperterminal program is used to receive logged temperature data from PIC12F683. The rate of data transfer is set to 9600 baud on both end. The figure below shows the hyperterminal settings for this project.
Conclusion and future work
A simple data logger project was demonstrated using a PIC12F683 micrcontroller and DS18B20 temperature sensor. A number of improvements can be done with this project, such as adding an external EEPROM for expanding its recording capacity. A separate application program can be developed on PC’s side to receive the serial data from the logger and generate temperature vs time plots. A real time clock chip can also be added in the project to keep record of the actual time stamp. A bigger PIC with more I/O pins would be more appropriate for implementing these additional features.
Update (Dec 28, 2012)
Peter Albeda from The Netherlands has made some improvements in the firmware provided here. His version of software has the following improved features:
1) Support for negative temperatures
2) When you delete the data, it now no longer resets the sample time
3) Deletes the leading zero’s while sending data to PC over a serial link
Update (Nov 29, 2014)
Fabian (from Argentina) has successfully built this project and has shared a picture of his final working prototype.