This project shows how to make a simple digital thermometer using PIC18F4550 microcontroller and DS18B20 temperature sensor where temperature value is displayed on Nokia 5110 (Nokia 3310) graphical LCD which has a resolution of 84×48 pixel.
The compiler used in this project is CCS PIC C.
The DS18B20 temperature sensor is a 3-pin electronic component (like a simple transistor) from Maxim (formerly Dallas) 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).
To see how to interface the PIC18F4550 with the Nokia 5110 for the first time, visit the post below:
Interfacing PIC18F4550 MCU with NOKIA 5110 LCD
and the following project shows how to interface PIC18F4550 MCU with DS18B20 sensor and 16×2 LCD:
PIC18F4550 with DS18B20 sensor and LCD project
Hardware Required:
- PIC18F4550 microcontroller
- Nokia 5110 LCD screen
- DS18B20 temperature sensor —-> datasheet
- 5 x 3.3k ohm resistor
- AMS1117 3V3 voltage regulator
- 10 uF capacitor
- 100 nF ceramic capacitor
- 5 x 2.2k ohm resistor
- 4.7k ohm resistor
- 5V power source
- Breadboard
- Jumper wires
PIC18F4550 MCU with DS18B20 sensor and Nokia 5110 LCD circuit:
The following image shows project circuit schematic diagram.
All the grounded terminals are connected together.
The DS18B20 sensor has 3 pins (from left to right): GND, data pin and VCC (or VDD) where:
GND: connected to circuit ground (0V),
data pin: connected to PIC18F4550 RB2 (#35) and
VCC: sensor power supply pin, connected to circuit +5V.
A pull-up resistor of 4.7k ohm is required because the DS18B20 has an open drain output.
In this project the PIC18F4550 uses its internal oscillator and MCLR pin function is disabled.
PIC18F4550 MCU with DS18B20 sensor and Nokia 5110 LCD C code:
The C code below is for CCS C compiler, it was tested with version 5.051.
To be able to compile project C code, a driver for the Nokia 5110 LCD is required, download link is below. After you download the driver file which named NOKIA5110.c, add it to your project folder:
Nokia 5110 LCD driver for CCS C compiler
Connection of LCD reset, chip select and data/command pins are defined in the code as:
1 2 3 4 | // define LCD module pin connections #define LCD_RST PIN_D0 // reset pin, optional! #define LCD_CS PIN_D1 // chip select pin, optional! #define LCD_DC PIN_D2 // data/command pin |
and the DS18B20 sensor data pin is defined as:
1 2 | // define DS18B20 data pin connection #define DS18B20_PIN PIN_B2 |
Functions used in the code:
int1 ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns 1 if OK and 0 if error.
ds18b20_write_bit(int1 value): writes (sends) 1 bit to the DS18B20 sensor, the bit is ‘value’ which may be 1 or 0.
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 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 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 1 if OK and 0 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.
Note that 1/16 = 0.0625
Full CCS C code:
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | /* * Digital thermometer using PIC18F4550 microcontroller, DS18B20 temperature * sensor and Nokia 5110 graphical LCD. * C Code for CCS C compiler. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ */ // define LCD module pin connections #define LCD_RST PIN_D0 // reset pin, optional! #define LCD_CS PIN_D1 // chip select pin, optional! #define LCD_DC PIN_D2 // data/command pin // define DS18B20 data pin connection #define DS18B20_PIN PIN_B2 #include <18F4550.h> #fuses NOMCLR, INTRC_IO, NOWDT, NOPROTECT, NOLVP #use delay(clock = 8MHz) #use spi(SPI1, BAUD = 1000000, MODE = 3, BITS = 8, STREAM = LCD_STREAM) #use fast_io(B) #include <NOKIA5110.c> // include Nokia 5110 LCD driver source file int1 ds18b20_start() { int1 ret = 0; output_low(DS18B20_PIN); // send reset pulse to the DS18B20 sensor output_drive(DS18B20_PIN); delay_us(500); // wait 500 us output_float(DS18B20_PIN); delay_us(100); // wait to read the DS18B20 sensor response if (!input(DS18B20_PIN)) { ret = 1; // DS18B20 sensor is present delay_us(400); // wait 400 us } return(ret); } void ds18b20_write_bit(int1 value) { output_low(DS18B20_PIN); output_drive(DS18B20_PIN); delay_us(2); output_bit(DS18B20_PIN, value); delay_us(80); output_float(DS18B20_PIN); delay_us(2); } 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); delay_us(2); output_float(DS18B20_PIN); delay_us(5); value = input(DS18B20_PIN); delay_us(100); 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 } // main function void main() { setup_oscillator(OSC_8MHZ); // set internal oscillator to 8MHz delay_ms(1000); // wait 1 second LCD_Begin(); // initialize the LCD LCD_Clear(); // clear the buffer LCD_SetContrast(60); // set LCD contrast LCD_TextSize(1); // set text size to 1 LCD_GotoXY(7, 4); // move cursor to position (7, 4) pixel LCD_Print("PIC18F4550 +"); LCD_GotoXY(0, 14); // move cursor to position (0, 14) pixel LCD_Print("DS18B20 SENSOR"); LCD_GotoXY(7, 27); // move cursor to position (7, 27) pixel LCD_Print("TEMPERATURE:"); LCD_Display(); // print LCD buffer int16 temp; while(TRUE) { LCD_GotoXY(9, 37); if(ds18b20_read(&temp)) { if(temp & 0x8000) { // if temperature is negative LCD_Print('-'); // put minus sign (-) temp = ~temp + 1; // absolute value } else { // temperature >= 0 if(temp >= 1600) // if the temperature >= 100 °C LCD_Print('1'); // put 1 of hundreds else // otherwise LCD_Print(' '); // put space ' ' } // why 0.0625?: because we're working with 12-bit resolution (default resolution). // temp/16 = temp * 0.0625 // (temp & 0x0F)*625 = (temp & 0x0F)*0.0625*10000 ---> extracting the 4 digits // after the decimal point. printf(LCD_Print, "%02Lu.%04Lu C", (temp/16) % 100, (temp & 0x0F) * 625); LCD_DrawRect(59, 37, 3, 3); // print degree symbol ( ° ) } else // sensor error! LCD_Print(" Error! "); LCD_Display(); delay_ms(1000); // wait 1 second } } // end of code. |
Proteus simulation of this project should give the same result as shown in the following video where Arduino UNO is used instead of the PIC18F4550 microcontroller:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.