brock

Monday, October 29, 2012

PIC16F877A-Based Temperature Monitoring System

Temperature monitoring and  control is important in industry environments. Sensors are widely used for measurement of temperature. Usually, a temperature sensor converts the temperature into an equivalent voltage output. IC LM35 is such a sensor. Here we describe a simple temperature measurement and display system based on LM35 sensor and PIC16F877A microcontroller. The temperature in degrees Celsius is displayed on a 16×2 LCD.

Fig.1 shows the functional block diagram of the PIC16F877A-based temperature monitoring system. The key features of this system are:

1. Continuous monitoring of temperature with 1-second update interval (which can be varied in the program).

2. Temperature measurement using LM35 precision integrated-circuit sensor.

3. Precise analogue-to-digital conversion using in-built 10-bit analogue- to-digital converter (ADC) of PIC16F877A microcontroller.

Hardware description


Fig.2.shows the circuit  of the temperature  monitoring  system. The circuit mainly consists of the LM35 temperature sensor, PIC16F877A microcontroller and HD44780 controller based 16×2 LCD.

fig2
The output of the sensor is fed to the internal ADC of the microcontroller. Pin 2 of the microcontroller (RA0/AN0) is channel-1 of the internal ADC. The analogue voltage output of the sensor is converted into its equivalent digital value by the ADC and then its equivalent degree Celsius value is calculated by the software. The calculated temperature value is displayed on the LCD.
LM35 sensor. Fig.3 shows the pin configuration of LM35. It is a precision integrated-circuit centigrade temperature sensor whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus has an advantage over linear temperature sensors calibrated in degree Kelvin, as the user is not required to subtract a large constant voltage from its output to obtain convenient Centigrade scaling. For each degree Celsius change in temperature, the sensor output changes by 10 mV.

The sensor can measure temperature in the range of 0 to 100°C, i.e., the output of the sensor varies from 0 to 1000 mV. The LM35 operates over the temperature range of -55° to +150°C, while the LM35C is rated for a -40°C to +110°C range (-10°C with improved accuracy).

Pin layout of the sensor is as follows:

Pin 1-VDD
Pin 2-Output of the sensor
Pin 3-VSS

PIC16F877A microcontroller. IC PIC16F877A is an 8-bit microcontroller with 8k×14-bit flash program memory, 368 bytes of RAM and many other extra peripherals like ADC, universal synchronous asynchronous receiver transmitter, master synchronous serial port, timers, compare capture and pulse-width modulation modules, and analogue comparators. It is based on the reduced instruction set computer (RISC) architecture.

The microcontroller processes the sensor output to compute the temperature in degree Celsius. The internal ADC of the microcontroller is used to convert the analogue output of the sensor into its equivalent digital value.

The internal ADC of the microcontroller has eight channels of analogue input and gives 10-bit digital output. In this project, the reference voltage to the ADC is the same as the supply voltage to the microcontroller, i.e., 5V. The resolution of the ADC can be calculated as follows:

Resolution = Vref / (1024-1) ...(as it is a 10-bit ADC)
                = 5/1023
                = 4.887 mV

It means that for 4.887mV change in the analogue input, the ADC output changes by binary ‘1’ with a reference voltage of 5V.

Analogue output of the sensor at its pin 2 is connected to Port A at RA0 for conversion into digital equivalent.


The LCD. A 16×2 LCD based on HD44780 controller is used for displaying the temperature. The control lines EN, R/W and RS of the LCD module are connected to pins RA1, RA2 and RA3 of Port A of the microcontroller, respectively. The commands and the data to be displayed are sent to the LCD module in the nibble mode from Port D of the microcontroller. The higher four bits of the LCD (D4 through D7) are connected to the lower nibble of Port D (RD0 through RD3).
Software description

The software code is written in ‘C’ language and compiled using Hitech C cross compiler in MPLAB IDE. The software performs the following operations in an infinite loop:

1. Initiate analogue-to-digital conversion and obtain the result

2. Calculate the equivalent voltage value from the ADC result

3. Calculate the temperature in degree Celsius from the voltage value

4. Display the temperature on the LCD

The voltage output (in volts) of the sensor is:

(ADC result × 5) /  1023

The temperature in degree Celsius is:

Sensor output × 1000 / 10  = Sensor output × 100

The Hitech C cross-compiler provides floating-point library support that is required for doing the above calculations.


MPLAB IDE and Hitech C compiler. MPLAB IDE is a very powerful software development tool for Microchip products (microcontrollers). It is available for download at www.microchip.com free of cost. It consists of tools like text editor, cross-assembler, cross-compiler and simulator.

Hitech C cross compiler is meant for Microchip PIC10/12/16 series of microcontrollers. Its Lite edition comes free with newer versions of MPLAB IDE like MPLAB v8.2 and v8.3. It can also be downloaded for free from www.htsoft.com.
Compiling the program using MPLAB IDE. The steps follow:

1. Create project file and add source files. In the menu bar, click Project→ Project Wizard. The project wizard dialogue box appears. Click ‘Next.’ In ‘Next’ window, select the device as PIC16F877A from the drop-down menu. Click ‘Next’ and select ‘Hitech Universal Tool’ suite from the drop down menu. Click ‘Next,’ name your project file as ‘tempr’ and specify its location. The file is automatically saved with ‘.mcp’ extension. Click ‘Next’ and add source files tempr.c, lcd.c and delay. c to your project. If you want to create the source files on your own, you can skip the above step. Click Next→ Finish button. Now your project is created and the source files are added to your project.
2. Create and add source files of your own. After creating the project, proceed with typing the code. Open a new text file by clicking ‘New’ in ‘File’ menu. Type the code in the text editor and save it with ‘.c’ extension. You can create source files tempr.c, delay.c and lcd.c in this manner. After typing and saving the code, you have to add the source files to your project. In ‘Project’ menu, click ‘Add Files to Project’ and then add the files by browsing them from the location they are saved in.
3. Configure the system. To configure features like type of oscillator and WDT, click ‘Configure’ menu and then click ‘Configuration Bits.’ In the configuration bits window, select the type of oscillator as XT, and disable all other features like watchdog timer, power-up timer and brownout detect.
4. Compile the project. To compile the software, click ‘Build’ option in ‘Project’ menu. The software is compiled and ‘Build Successful’ message appears in the output window. After successful compilation of the program, the file tempr.hex is generated.

Downloading the software into the chip. After compiling the software, the hex file generated can be downloaded into the chip either using a locally made programmer (like JMD programmer) or Microchip’s programming kit (PICKit2, PICKit3, ICD2, ICD3, etc).

We have used Microchip MPLAB ICD2 (in-circuit debugger) to burn the program into PIC16F877A microcontroller.

The programming can be done from MPLAB IDE itself by selecting the kit (MPLAB ICD2) from ‘Programmer’ menu in the menu bar. If you use a JMD programmer, the hex file can be downloaded into the chip using WinPic800 software. The configuration bits have to be separately configured in WinPic800.


RFID-Based Security System


A radio-frequency identification (RFID) based access-control system allows only authorised persons to enter a particular area of an establishment. The authorised persons are provided with unique tags, using which they can access that area.

The system is based on micro controller AT89C52 and comprises an RFID module, an LCD module for displaying the status and a relay for opening the door. Fig.1 shows a user trying to open the door by placing an RFID tag near the RFID reader.

Radio-frequency identification

 You might be familiar with RFID systems as seen in access control, contactless payment systems, product  tracking and inventory control, etc. Basically, an RFID system consists of three components: an antenna or coil, a transceiver (with decoder) and a transponder (RF tag) electronically programmed with unique information.
Fig.1 shows a typical RFID system. In every RFID system, the transponder tags contain unique identifying information. This information can be as little as a single binary bit or a large array of bits representing such things as an identity code, personal medical information or literally any type of information that can be stored in digital binary format. 
fig1
The RFID transceiver communicates with a passive tag. Passive tags have no power source of their own and instead derive power from the incident electromagnetic field. Commonly, at the heart of each tag is a microchip. When the tag enters the generated RF field, it is able to draw enough power from the field to access its internal memory and transmit its stored information. When the transponder tag draws power in this way, the resultant interaction of the RF fields causes the voltage at the transceiver antenna to drop in value. This effect is utilised by the tag to communicate its information to the reader. The tag is able to control the amount of power drawn from the field and by doing so it can modulate the voltage sensed at the transceiver according to the bit pattern it wishes to transmit.
Antenna. Fig.2 shows the internal diagram of a typical RFID antenna. An RFID antenna consists of a coil with one or more windings and a matching network. It radiates the electromagnetic waves generated by the reader to activate the tag and read/ write data from it.
fig2
Antennae are the conduits between the tag and the transceiver which control the system’s data acquisition and communication. These are available in a variety of shapes and sizes. Often, the antenna is packaged with the transceiver and decoder to become a reader, which can be configured either as a hand held or a fixed-mount device. The reader emits radio waves in ranges of anywhere from 2.54 cm (one inch) to 30 metres or more, depending upon its power output and the radio frequency used. When an RFID tag passes through the electromagnetic zone, it detects the reader’s activation signal. The reader decodes the data encode din the tag’s integrated circuit (silicon chip) and the data is passed to the host computer for processing.
Tags (transponders). Fig.3 shows the internal structure of a typical RFID tag. An RFID tag comprises a microchip containing identifying information and an antenna that transmits this data wirelessly to the reader. At its most basic, the chip will contain a serialised identifier, or licence plate number, that uniquely identifies that item, similar to the way many bar codes are used today.
fig3
There are three types of tags: active, passive and semi-passive.
Passive tags have no internal power source. These draw their power from the electromagnetic field generated by the RFID reader and then the microchip can send back information on the same wave. The reading range is limited when using passive tags.
Active transponders have their own transmitters and power source, usually in the form of a small battery. These remain in a low-power ‘idle’ state until they detect the presence of the RF field being sent by the reader. When the tag leaves the area of the reader, it again powers down to its idle state to conserve its battery. As a result, active tags can be detected at a greater range than passive tags.

Semi-passive tags have their own power source that powers only the microchip. These have no transmitter. They rely on altering the RF field from the transceiver to transmit their data.

There are three ways for data encoding into tags:

1. Read-only tags contain data, which is pre-written onto them by the tag manufacturer or distributor.

2. Write-once tags enable a user to write data to the tag one time in production mor distribution processes.

3. Full read-write tags allow new data to be written to the tag as needed and later other data can be rewritten over the original data.

RF transceiver. The RF transceiver is the source of the RF energy used to activate and power the passive RFID tags. It may be enclosed in the same cabinet as the reader or it may be a separate piece of equipment. When provided as a separate piece of equipment, the transceiver is commonly referred to as an RF module. The RF transceiver controls and modulates the radio frequencies that the antenna transmits and receives. The transceiver filters and amplifies the back-scatter signal from a passive RFID tag.
Circuit description

Fig.4 shows the circuit of the RFID based security system. The compact  circuitry is built around Atmel AT89C52 microcontroller. The AT89C52 is a low-power, high performance CMOS 8-bit microcomputer with 8 kB of Flash programmable and erasable read only memory (PEROM). It has 256 bytes of RAM, 32 input/output (I/O) lines, three 16-bit timers/ counters, a six-vector two-level interrupt architecture, a full-duplex serial port, an on-chip oscillator and clock circuitry. The system clock also plays a significant role in operation of the microcontroller.
fig4
An 11.0592MHz quartz crystal connected to pins 18 and 19 provides basic clock to the microcontroller. Power-on reset is provided by the combination of electrolytic capacitor C4 and resistor R1. Switch S1 is used for manual reset. Port pins P2.0 through P2.7 of the microcontroller are connected to data port pins D0 through D7 of the LCD, respectively. Port pins P3.7 and P3.6 of the microcontroller are connected to register-select (RS) and enable (E) pins of the LCD, respectively. Read/write  pin of the LCD is grounded to enable for write operation.

All the data is sent to the LCD in ASCII format for display. Only the commands are sent in hex form. Register-select (RS) signal is used to distinguish between data (RS=1) and command (RS=0). Preset VR1 is used to control the contrast of the LCD. Resistor R6 limits the current through the backlight of the LCD. Port pins P3.0 (RXD) and P3.1 (TXD) of the microcontroller are used to interface with the RFID reader.

When an authorised person having the tag enters the RF field generated by the RFID reader, RF signal is generated by the RFID reader to transmit energy to the tag and retrieve data from the tag. Then the RFID reader communicates through RXD and TXD pins of the microcontroller for further processing.

Thus on identifying the authorised person, port pin P3.2 goes high, transistor T2 drives into saturation, and relay RL1 energises to open the door for the person. Simultaneously, the LCD shows “access granted” message and port pin P1.7 drives piezobuzzer PZ1 via transistor T1 for aural indication.

If the person is unauthorised, the LCD shows “access denied” and the door doesn’t open. LED2 and LED3show presence of the tag in the RFID reader’s electromagnetic field.

To derive the power supply, the 230V, 50Hz AC mains is stepped down by transformer X1 to deliver a secondary output of 15V, 500 mA. The transformer output is rectified by a full-wave rectifier comprising diodes D1 through D4, filtered by capacitor C1 and regulated by ICs 7812 (IC2) and 7805 (IC3). Capacitor C2 bypasses the ripples present in the regulated supply. LED1 acts as the power indicator and R2 limits the current through LED1.

CODE:

Time-Controlled Switch Using PIC16F72

A time-controlled switch is an automatic timer switch that turns an appliance ‘on’ for the desired time duration. After the preset time duration, the timer automatically switches off, disconnecting the appliance from the power supply. The time duration for which the appliance should be ‘on’ can be set from 1 to 99 minutes. 

This switch obviates the need to continuously monitor the appliance—an advantage over the manual switch. It can be used to switch on or switch off any electrical home appliance at a predetermined time. Switching an appliance on or off in a timely manner increases the life of the appliance and also saves power consumption. 

The switch also finds industrial applications, where the machines which control the processes can be run for the desired time.

Circuit description
Fig. 1 shows the circuit of the time-controlled switch using PIC16F72 microcontroller. It comprises microcontroller PIC16F72 (IC1), regulator 7805 (IC2), two 7-segment displays (LTS542) and a few discrete components. 
fig1
Microcontroller PIC16F72 is the heart of the switch. It is an 8-bit, low-cost, high-performance, Flash microcontroller. Its key features are 2 kB of Flash program memory, 128 bytes of RAM, eight interrupts, three input/output (I/O) ports, three timers and a five-channel 8-bit analogue-to-digital converter (ADC). There are 22 I/O pins, which are user-configurable for input/output on pin-to-pin basis. Architecture is RISC, and there are only 35 powerful instructions. 

System clock plays a significant role in operation of the microcontroller. A 4MHz quartz crystal connected between pins 9 and 10 provides the basic clock to the microcontroller (IC1). 

Two 7-segment displays (DIS1 and DIS2) are used to display the time in minutes. Port pins RB2, RB3, RA0, RA1, RA2, RB1 and RB0 are connected to segment pins ‘a’ through ‘g’ of display DIS1, respectively. Ports pin RC6, RC7, RC1, RC2, RC3, RC5 and RC4 are connected to segment pins ‘a’ through ‘g’ of display DIS2, respectively. 
Switches S2 (start/stop), S3 (select), S4 (decrement) and S5 (increment) are connected to port pins RB4 through RB7 of the microcontroller, respectively. Port pin RC0 of the microcontroller is used to control relay RL1 with the help of transistor T1. When port pin RC0 is high, transistor T1 drives into saturation and 12V-relay RL1 energises to connect the load to power supply. Diode D5 acts as a free-wheeling diode. 

To derive the power supply for the circuit, the 230V, 50Hz AC mains is stepped down by transformer X1 to deliver a secondary output of 12V, 500mA. The transformer output is rectified by a full-wave rectifier comprising diodes D1 through D4, filtered by capacitor C4 and regulated by IC 7805 (IC2). Capacitor C5 is used to bypass the ripples present in the regulated supply. LED2 gives power-‘on’ indication. Resistor R19 limits the current through LED2. Switch S1 is used for manual reset.

Set the time using switch S4 for decrement and switch S5 for increment. The time is indicated on 7-segment displays DIS1 and DIS2. To start timing count-down, press start/stop switch S2. Relay RL1 energises to switch on the appliance and LED1 glows. If you press start/stop switch S2 again, the count-down process will stop and relay RL1 de-energise to switch the appliance off. 

CODE: