Arduino 1-Wire Device Libraries

DS18S20 Example Code

This example demonstrates the use of the library to continuously read a DS18S20 sensor and print its value in both degrees Celsius and degrees Fahrenheit to the serial port.

// Copyright 2013, bechter.com - All Rights Reserved
#include <Arduino.h>
#include <OneWire.h>
#include <DS18S20.h>

// define the Arduino digital I/O pin to be used for the 1-Wire network here
const uint8_t ONE_WIRE_PIN = 2;

// define the 1-Wire address of the DS18S20 temperature sensor here
uint8_t DS18S20_address[] = { 0x10, 0xc9, 0x5d, 0xd7, 0x01, 0x09, 0x00, 0x90 };

OneWire ow(ONE_WIRE_PIN);
DS18S20 ds18S20(&ow, DS18S20_address);

void setup() {
    Serial.begin(9600);
    ds18S20.begin();
}

void loop() {
    ds18S20.update();
    if (ds18S20.isError()) {
        Serial.println("Error reading temperature");
    } else {
        Serial.print("Temperature = ");
        Serial.print(ds18S20.getTemperature(), 1);
        Serial.print("C (");
        Serial.print(ds18S20.getTemperature() * 9.0 / 5.0 + 32.0, 1);
        Serial.println("F)");
    }
    delay(500);
}

<<Previous Page | Index | Next Page>>