Interfacing the popular microcontroller PIC16F877A with the temperature sensor DS18B20 is so easy, in this topic I’m going to show how to build a digital thermometer using PIC16F877A and DS18B20 sensor where the temperature is displayed on 16×2 LCD screen.
Firstable 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 ….). Each DS18B20 device has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus and controlled with one master device.
The DS18B20 sensor provides 9-bit to 12-bit Celsius temperature measurement resolution (programmable resolution).
The DS18B20 sensor is available in 8-Pin SO (150 mils), 8-Pin μSOP, and 3-Pin TO-92 Packages. DS18B20 pin configurations is shown below:
In this example the master device is the PIC16F877A microcontroller.
Components Required:
- PIC16F877A microcontroller —> datasheet
- DS18B20 temperature sensor —> datasheet
- 16×2 LCD screen
- 8 MHz crystal oscillator
- 2 x 22pF ceramic capacitors
- 10K ohm resistor
- 4.7k ohm resistor
- Power source with 5 VDC
- Breadboard
- Jumper wires
Interfacing PIC16F877A with DS18B20 temperature sensor circuit:
The image below shows the circuit schematic diagram of the project.
(All grounded terminals are connected together)
The DS18B20 sensor has 3 pins: VCC (+5V), data and GND. The data pin is connected to PIC16F877A pin RB1. The 16×2 LCD module is connected to PORTD pins.
The LCD screen is used to display the temperature value read by the DS18B20 sensor.
Interfacing PIC16F877A with DS18B20 temperature sensor CCS C code:
Functions used in the code:
int1 ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns TRUE (1) if OK and FALSE (0) if error.
void ds18b20_write_bit(int1 value): writes (sends) 1 bit to the DS18B20 sensor, the bit is ‘value’ which may be 1 or 0.
void ds18b20_write_byte(int8 value): writes 1 byte (8 bits) to the DS18B20 sensor, this function is based on the previous function. This function writes LSB (Least Significant Bit) first.
int1 ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0).
int8 ds18b20_read_byte(void): reads 1 byte (8 bits) from the DS18B20 sensor, this function is based on the previous function. This function reads LSB first.
int1 ds18b20_read(int16 *raw_temp_value): reads the temperature raw data which is 16-bit long (two 8-bit registers), the data is stored in the variable raw_temp_value, returns TRUE if OK and FALSE if error.
The value of the temperature in degree Celsius is equal to the raw value divided by 16 (in case of 12-bit resolution). The default resolution of the DS18B20 is 12 bits.
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | // Interfacing PIC16F877A with DS18B20 temperature sensor // C Code for CCS C compiler. //LCD module connections #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_ENABLE_PIN PIN_D2 #define LCD_DATA4 PIN_D3 #define LCD_DATA5 PIN_D4 #define LCD_DATA6 PIN_D5 #define LCD_DATA7 PIN_D6 //End LCD module connections #include <16F877A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock = 8MHz) #include <lcd.c> #define DS18B20_PIN PIN_B1 // DS18B20 Data pin is connected to pin RB1 signed int16 raw_temp; float temp; int1 ds18b20_start(){ output_low(DS18B20_PIN); // Send reset pulse to the DS18B20 sensor output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as output delay_us(500); // Wait 500 us output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as input delay_us(100); //wait to read the DS18B20 sensor response if (!input(DS18B20_PIN)) { delay_us(400); // Wait 400 us return TRUE; // DS18B20 sensor is present } return FALSE; } void ds18b20_write_bit(int1 value){ output_low(DS18B20_PIN); output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as output delay_us(2); // Wait 2 us output_bit(DS18B20_PIN, value); delay_us(80); // Wait 80 us output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as input delay_us(2); // Wait 2 us } void ds18b20_write_byte(int8 value){ int8 i; for(i = 0; i < 8; i++) ds18b20_write_bit(bit_test(value, i)); } int1 ds18b20_read_bit(void) { int1 value; output_low(DS18B20_PIN); output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as output delay_us(2); output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as input delay_us(5); // Wait 5 us value = input(DS18B20_PIN); delay_us(100); // Wait 100 us return value; } int8 ds18b20_read_byte(void) { int8 i, value = 0; for(i = 0; i < 8; i++) shift_right(&value, 1, ds18b20_read_bit()); return value; } int1 ds18b20_read(int16 *raw_temp_value) { if (!ds18b20_start()) // Send start pulse return FALSE; ds18b20_write_byte(0xCC); // Send skip ROM command ds18b20_write_byte(0x44); // Send start conversion command while(ds18b20_read_byte() == 0); // Wait for conversion complete if (!ds18b20_start()) // Send start pulse return FALSE; // Return 0 if error ds18b20_write_byte(0xCC); // Send skip ROM command ds18b20_write_byte(0xBE); // Send read command *raw_temp_value = ds18b20_read_byte(); // Read temperature LSB byte and store it on raw_temp_value LSB byte *raw_temp_value |= (int16)(ds18b20_read_byte()) << 8; // Read temperature MSB byte and store it on raw_temp_value MSB byte return TRUE; // OK --> return 1 } void main() { lcd_init(); // Initialize LCD module lcd_putc('\f'); // Clear LCD lcd_gotoxy(3, 1); // Go to column 3 row 1 printf(lcd_putc, "Temperature:"); while(TRUE) { if(ds18b20_read(&raw_temp)) { temp = (float)raw_temp / 16; // Convert temperature raw value into degree Celsius (temp in °C = raw/16) lcd_gotoxy(5, 2); // Go to column 5 row 2 printf(lcd_putc, "%f", temp); lcd_putc(223); // Print degree symbol ( ° ) lcd_putc("C "); // Print 'C ' } else { lcd_gotoxy(5, 2); // Go to column 5 row 2 printf(lcd_putc, " Error! "); } delay_ms(1000); // Wait 1 second } } // End of code. |
Arduino + DS18B20 sensor video:
The video below shows Proteus simulation of this project.
Arduino + DS18B20 sensor + LCD Proteus simulation file download:
Download
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
can i have the library of ds18b20 pf mikroC?
gracias por el aporte, funciona correctamente
is it possible to run on 20 Mhz? 🙂 i can’t send command 0x44 to my tempsens
Can I run this code in Atmega32
where is the lcd.c file???????????
It’s already comes with CCS C compiler installation file!
After compilation Successfully print Temperature Error on lcd in Proteus
Make sure that you’re using the same clock frequency setting for the compiler and Proteus (8MHz).
Also check the 4.7K resistor properties (Model Type: DIGITAL).
could u plz tell how to call function in main c
i need hex file of same code