DS18S20 - Temperature Sensor
1. Basic circuit
DS18S20 is a digital temperature sensor with 1-wire interface. The sensor can be connected to any I/O pin of Ansteron Board. The library provides a simple function to read the temperature from sensor as well as low level read and write functions.
DS18B20 is similar to DS18S20 but its has higher resolutions. See this link.
A pull-up resistor (4.7K) on data line is required. Only pin 1 and 2 of the sensor are used, pin 3 can be left unconnected.
To add the library into the program, go to menu Build, select Compiler settings. In the dialog, add the library file into Additional libraries list.
DS18S20_read_temperature(pin); to command the sensor to perform a temperature reading then return the measurement. Input
pin is the pin that the sensor is attached to (PIN_D7 as the circuit above). Return value is a 16 bit integer with value 0 means 0 degree Celsius. Each step of the value represents 0.5 degree Celsius.
|Temperature(degree Celsius)||Digital value|
To check if the temperature is negative or positive, check the 15th bit of the value by logically AND with
0x8000. If bit 15th is set, temperature is negative, if not, temperature is positive:
If the temperature is negative, its absolute value can be extracted by subtracting reading value from 65535 (
0xFFFF) then add 1 (negative value starts at -0.5); If the accuracy to 0.5 degree is not needed, simply divide the value by 2. The example below shows how to print temperature value to Serial Terminal. At first, the temperature is measured and the message is printed, then positive or negative sign, then the absolute value and finally the 1/10th decimal place.
DS18S20_sprint_temperature(buffer,value); can be used as well. It will print the temperature to a buffer and that can be sent directly to Serial or LCD.
To convert Celsius degree to Fahrenheit, use the formula
[F]=[C]* 9/5 + 32.
DS18S20_C_to_F(value); can be used to convert raw data from the sensor. Both positive and negative temperature will work.
Note: Read temperature function takes about 800ms to finish due to the time that sensor needed to do a temperature reading.
Perform a temperature convertion and return data when the convertion is done. The function does not check for error and it returns invalid data if DS18S20 is not properly connected. Function
DS18S20_reset(pin); can be used to check the connection before reading from sensor.
Reset DS18S20 device on specified pin. Function returns 1 if DS18S20 is detected and 0 if not. Input
pin is the same as other I/O functions (
Read a byte from DS18S20. Function returns random data if DS18S20 was not attached to specified pin.
Send a byte (usually command) to DS18S20. Refer device datasheet for the list of commands and how to use them.
Start a temperature convertion on DS18S20 connected to given pin. The program can then retrieve result using function below. Note that it takes about 750 milliseconds for a convertion to complete, reading result before a convertion is done may return invalid data. The function returns 1 if succeeded, 0 if DS18S20 was not detected on the pin.
Retrieve the temperature from the last convertion.
Convert Celsius to Fahrenheit. The function returns
int value with 0 means 0 degree Fahrenheit, 0xFFFF is -0.5 and so on. It's similar to the coding mentioned above. Note that each 0.5 degree Celsius corresponding to about 0.9 degree Fahrenheit. Accuracy of 0.5 degree (F) is not realistic and the program should divide converted value by 2 to get usable temperature.
Print coded temperature value as text to a buffer. For example, value 0 will be printed as "0.0", value 1 will be "0.5" and value 0xFFFF will be "-0.5". The buffer input is a pointer to
char array that will hold result. Note that buffer should be big enough for output (6 bytes minimum). The output can then be sent to Serial with
serial_message(buffer); or to be displayed on LCD with
Due to timing requirement of 1-Wire interface, all interrupts will be disabled while communication is going on. Between bytes sent and received, interrupts are re-enabled. Each byte transfer takes about 1 millisecond.
The library implements 1-Wire interface according to device specification and no error checking is needed for most cases. If temperature reading is critical for your application, error checking at communication level can be added to the library. Source file is included with the library and users are free to modify it as their needs.