Here is my first project where I use LM35 Temperature sensor (which converts temperature to linear voltage delta) and PIC16F1579.
LM35 simply outputs 10 x mV voltage in Celsius, for example 260 mV = 26.0 degrees of Celsius. Please note that negative temperature can be sensed with negative bias resistor only - not realized here. Please see LM35 datasheet for details.
Our task is to measure this Voltage using ADC (and FVREF - fixed Voltage reference for ADC). By coincidence it is perfect fit for PIC16F1579 because it has ADC but not I2C nor SPI (see note below).
Please note that minimum supply Voltage for LM35 is 4V so it may be difficult to use with some recent boards.
Additionally it is my first project where I'm playing with MCC Melody code generator tool.
Status:
- it Works!
- example UART output:
L86: App v1.01
L99 FVR ready in 0 [ms]
L109: #1 Temp=28.2 [^C] V=282 [mV]
L109: #2 Temp=28.3 [^C] V=283 [mV]
L109: #3 Temp=28.2 [^C] V=282 [mV]
L109: #4 Temp=28.3 [^C] V=283 [mV]
- UART configuration: Speed 19200 Baud, 8-bit data, 1 stop bit, no parity, no flow control
- legend:
Lxx
: Line number in PIC16-LM35-Melody.X/main.c source file#x
: simple counter that increments with each measurement.V=YYY [mV]
ADC input from LM35 in milivoltsTemp=X.X [^C]
measured temperature in degrees of Celsius (just millivolts divided by 10)
Used PIC pins (excluding PicKit3 programmer and power supply):
- PIN2 RA5 - UART TX (mapped with
RA5PPS=0x9
) - PIN8 RC6 AN8 - input from LM35 Vout (middle pin of LM35 in TO92 package)
- PIN10 RB7 - blinks LED around 4s rate (toggle rate 2s) using
__delay_ms(2000)
function inmain()
(no interrupt and/or Timer used/needed).- I slowed down this loop to not overflow UART at 19200 Baud.
Here is schema:
All schema files are in ExpressPCB/ folder. Made in ExpressPCB Classic.
Used MCC Melody Components
- MCC UART Driver
- MCC UART PLIB
- FVR PLIB - fixed positive Voltage reference (1.024V in my example) for ADC.
- ADC PLIB ADC to convert Voltage from LM35 ( Celsius times 10mV - 260 mV = 26.0 Degrees of Celsius) to Temperature and display it on UART
WARNING!
Chip PIC16F1579 has important Design limitations that are not apparent at first sight:
- no I2C module, no SPI module (in other words there is no single MSSP module)
- there is no external Crystal and/or Resonator support. The only external clock source is full digital Clock Input (but there is no inverting amplifier between CLKI/CLKO so no way how to make external passive parts oscillating ...) However there is at least internal oscillator (INTOSC) capable of do up to 16 MHz and also there is 4xPLL
- PICDEM DM163045 development board (you can use any PIC board that supports 20 pin PIC16F1579). I have one that is now 10 years old and I still like it - there is everything I need to work with 8-bit PIC MCUs.
Additional parts (not included with PICDEM):
- Power supply 9V (or 9V battery) to power PICDEM board and its LM317 Voltage
regulator.
- ensure that output from LM317 regulator is just 5V.
- PIC16F1579
- LM35 Temperature-to-Voltage sensor
- USB Console Cable #954 - or any other usable USB to UART adapter.
- connect Black wire to Ground, and Green wire to UART TX - PIN2 RA5
- XC8 compiler - tested version v2.45
- MPLAB X IDE - tested version v6.15
- Device Family Pack
PIC12-16F1xxx_DFP
v1.7.242 - MCC Melody Tool v5.3.7, Core v5.5.7
- Install all requirements (proper XC8 and MPLAB X IDE version as specified under "Software requirements" section)
- Ensure that everything is wired properly - use photo and schema as guide
- remember to properly setup connected UART in your Putty or another serial terminal application - see header of this readme for UART connection details
- run MPLAB X IDE
- open this project in MPLAB - folder
PIC16-LM35-Melody.X/
- on first open there will be warning that there is no path to XC8 specified.
- to fix this warning do this:
- click on Project Properties
- select XC8 -> your path to XC8
- click Apply and/or OK to finish
- finally click on
Make and Program Device Main Project
- once programmed there should be short report of measured temperature on UART.
In this age of I2C the good old LM35 analog sensor can be easily overlooked as obsolete. However I'm positively surprised how easy it is to setup and use it:
- because output is basically Temperature in Celsius in millivolts times 10 it is very easy to verify LM35 output even with plain DMM knowing that for example 250 mV is 25.0 Celsius - no other equipment (digital analyzer etc.) required.
- although PIC16F1579 may look limited at first sight I really like their smart FVREF voltage reference that is intentionally set to same value (1024 mV) as ADC range (10-bit or 1024). So just reading ADC Conversion value one get directly result in mV without need for any kind of normalization - which is nice.
So as long as you have suitable supply voltage (at least 4V required for LM35) it is still viable alternative to often overpriced digital sensors (I2C LM75 or even 1-wire Dallas 18B20).
I use lower UART speed 19200 Baud than more common (115200 Baud), because higher speeds have significant clock error (see PIC16F1579 datasheet pages 207 to 209 of DS40001782C).
MCC Melody is unable to Map UART TX pin in GUI - UART is shown in Pin Grid, but you can't assign pin to it. However here is solution - manually remap pin at program startup:
Also it is NOT enough to just select ADC Input pin in "Pin Grid Manager". One has also to:
-
select Project Resources -> System -> Pins
-
uncheck all stuff that may interfere with analog function including:
- uncheck Weak Pullup
- uncheck Slew Rate
- uncheck Input Level Control
-
basically only "Analog" should be kept checked for our PIN8, RC6, AN8
-
and Generate code again
-
if you did it properly there should be added ANx definition under
PIC16-LM35-Melody.X/mcc_generated_files/adc/adc1.h
for example:typedef enum { channel_Temp = 0x1d, channel_DAC = 0x1e, channel_FVR = 0x1f, channel_AN8 = 0x8 // <= if this line is missing that Analog pin is configured incorrectly } adc_channel_t;
You can find downloaded MCC Melody libraries under:
c:\Users\USERNAME\.mcc\libraries\@mchp-mcc\FOLDER