In this blog there are many examples shows how to interface some sensors with different types of microcontrollers and in this topic I’m going to show how to connect the popular and low cose temperature sensor DS18B20 with PIC16F887 microcontroller. The compiler used in this example is mikroC PRO for PIC.
Related Projects:
Interfacing PIC MCU with DHT11 sensor – mikroC Projects
DHT22 Sensor interface with PIC microcontroller – mikroC Projects
LM335 Sensor interface with PIC16F887 MCU – mikroC Projects
Interfacing PIC microcontroller with LM35 sensor – mikroC Projects
Interfacing PIC16F877A with DS18B20 temperature sensor
The DS18B20 sensor is a 3-pin electronic component (like a simple transistor) from Analog Devices (formerly Maxim Integrated) which uses 1-wire protocol to communicate with master device (microprocessor, microcontroller….).
The DS18B20 sensor provides 9-bit to 12-bit Celsius temperature measurement resolution (programmable resolution).
Components Required:
- PIC16F887 microcontroller —> datasheet
- DS18B20 temperature sensor — datasheet
- 16×2 LCD screen
- 4.7k ohm resistor
- 10k ohm variable resistor or potentiometer
- Power source with 5 VDC
- Breadboard & jumper wires
- PIC Microcontroller programmer (PICkit 2, PICkit 3…)
Interfacing DS18B20 sensor with PIC microcontroller circuit:
Example circuit schematic diagram is shown below.
(All grounded terminals are connected together)
The DS18B20 sensor has 3 pins (from left to right): GND, data and VCC (+5V). The data pin is connected to PIC16F887 pin RB1.
The LCD screen is used to display the temperature value read by the DS18B20 sensor. It’s connected to PORTD pins with:
RS —> RD0
E —> RD1
D4 —> RD2
D5 —> RD3
D6 —> RD4
D7 —> RD5
In this example the PIC16F887 microcontroller runs with its internal oscillator (@ 8MHz) and MCLR pin is configured as a digital input pin (configured in the software).
Interfacing DS18B20 sensor with PIC microcontroller mikroC code:
The C code below is for mikroC PRO for PIC compiler, the configuration words are:
CONFIG1 : 0x2CD4
CONFIG2 : 0x0700
The mikroC compiler has a built-in one-wire protocol library which makes the interfacing more easier.
The default resolution of DS18B20 is 12-bit which means the step of the temperature is 0.0625°C. The datasheet of the device contains more details about that. Rest of code is described through comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /* Interfacing DS18B20 temperature sensor with PIC16F887 mikroC PRO for PIC code Internal oscillator used @ 8MHz Configuration words: CONFIG1 = 0x2CD4 CONFIG2 = 0x0700 */ // LCD module connections sbit LCD_RS at RD0_bit; sbit LCD_EN at RD1_bit; sbit LCD_D4 at RD2_bit; sbit LCD_D5 at RD3_bit; sbit LCD_D6 at RD4_bit; sbit LCD_D7 at RD5_bit; sbit LCD_RS_Direction at TRISD0_bit; sbit LCD_EN_Direction at TRISD1_bit; sbit LCD_D4_Direction at TRISD2_bit; sbit LCD_D5_Direction at TRISD3_bit; sbit LCD_D6_Direction at TRISD4_bit; sbit LCD_D7_Direction at TRISD5_bit; // End LCD module connections int raw_temp; char *temp = "000.0000 C"; void main() { OSCCON = 0X70; // Set internal oscillator to 8MHz ANSELH = 0; // Configure all PORTB pins as digital Lcd_Init(); // Initialize LCD module Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off Lcd_Cmd(_LCD_CLEAR); // clear LCD lcd_out(1, 3, "Temperature:"); while(1) { Ow_Reset(&PORTB, 1); // Onewire reset signal Ow_Write(&PORTB, 1, 0xCC); // Issue command SKIP_ROM Ow_Write(&PORTB, 1, 0x44); // Issue command CONVERT_T while(Ow_Read(&PORTB, 1) == 0) ; Ow_Reset(&PORTB, 1); // Onewire reset signal Ow_Write(&PORTB, 1, 0xCC); // Issue command SKIP_ROM Ow_Write(&PORTB, 1, 0xBE); // Issue command READ_SCRATCHPAD raw_temp = Ow_Read(&PORTB, 1); // Read temperature LSB byte raw_temp |= (Ow_Read(&PORTB, 1) << 8); // Read temperature MSB byte if(raw_temp & 0x8000) { // If the temperature is negative temp[0] = '-'; // Put minus sign (-) raw_temp = ~raw_temp + 1; // Change temperature value to positive form } else { if((raw_temp >> 4) >= 100) // If the temperatue >= 100 °C temp[0] = '1'; // Put 1 of hundreds else // otherwise temp[0] = ' '; // put space ' ' } // Put the first two digits ( for tens and ones) temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + 48; // Put tens digit temp[2] = (raw_temp >> 4) % 10 + 48; // Put ones digit // Put the 4 fraction digits (digits after the point) // Why 625: because we're working with 12-bit resolution temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + 48; // Put thousands digit temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + 48; // Put hundreds digit temp[6] = (((raw_temp & 0x0F) * 625) / 10 ) % 10 + 48; // Put tens digit temp[7] = ( (raw_temp & 0x0F) * 625) % 10 + 48; // Put ones digit temp[8] = 223; // Put degree symbol ( ° ) lcd_out(2, 4, temp); // Display temperature delay_ms(1000); // Wait 1 second } } // End of code |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.