Example Code for Arduino-Temperature Reading
This article provides a detailed guide on using Arduino to read temperatures with the DS18S20 sensor, featuring sample code, wiring diagrams, and preparation steps for effective project implementation.
Connection Diagram

Connection Method: Gently press the single gray terminal, insert the wire into the lower hole, and release the gray terminal to finish the connection.
Pinout
3-Wire Interface: It is available in Type A and Type B. Please confirm your sensor interface before wiring.
Type A: Red wire (VCC), Black wire (GND), Yellow wire (DATA)
Type B: Red wire (VCC), Black wire (GND), Green wire (DATA)
Arduino 实例代码
Download Link for Full DS18B20 Documents & Arduino Library: Sample Codes
Sample Codes for Arduino 1.0
#include <OneWire.h>
int DS18S20_Pin = 3; //DS18S20 Signal pin on digital 3
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
Result:

Programming via Mind+ Upload Mode
Download and install the software. Download link: https://www.mindplus.cc; Detailed guide: Mind+ Basic Wiki – Software Download & Installation.
Switch to Upload Mode. Detailed guide: Mind+ Basic Wiki – Programming Procedure under Upload Mode.
Go to Extensions, select Arduino Uno under Main Controller, then add the DS18B20 Temperature Sensor from the Sensor category. Detailed guide: Mind+ Basic Wiki – How to Load Extension Libraries.
Complete your programming as shown in the screenshot below.
Click menu options: Connect Device → Upload to Device.
After upload finishes, open the serial monitor to view real-time output data. Detailed guide: Mind+ Basic Wiki – Serial Port Printout.

Programming in Mind+ Python Mode (Huskypad)
Mind+ Python Mode supports full standard Python programming, so a main controller board compatible with complete Python runtime is required. The Unihiker is used as an example in this guide.

Operation Steps
Download and install the latest official software from: https://www.mindplus.cc. Detailed tutorial: Mind+ Basic Wiki – Software Download & Installation.
Switch to Python Mode. Go to Extensions, select Unihiker under Official Libraries, then pick Pinpong Initialization and DS18B20 Temperature Sensor from the Pinpong library. Refer to the attached link for mode switching and library import instructions.
Complete the program coding.
Connect the Unihiker board and click Run; real-time data can be checked in the terminal. Official Unihiker Quick Start Guide: unihiker.com

Code Example
Sample codes based on the Pinpong library, see Unihiker official quick-start documentation (unihiker.com)
Was this article helpful?
