# KY-013 Temperature-Sensor module

## Technical data / Short description

Temperature measuring range: -55°C / +125°C

This module provides a NTC thermistor - it will have a lower resistant on higher temperatures.

You can draw near to the resistant change via maths and convert it into a linear course. With that you can determine the temperature coefficient (addicted from resistant change to temperature change). With that you can determine the actual temperature if you know the current resistance.

This resistor can be determinded via voltage devider, where a known voltage splits up between a known resistor and an unknown (variable) resistor.

With that Voltage you can determine the resistance of the resistor - you can see the full calculation in the example below.

## Code example Arduino

The program measures the actual voltage from the NTC, calculate the temperature and translates the result to °C for the serial output.

```#include <math.h>

int sensorPin = A5; // Declaration of the input pin

// These function translates the recorded analog measurement
// into the right temperature in °C and gives it out.
{
double Temp;
Temp = log(10000.0 * ((1024.0 / RawADC - 1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp - 273.15;            // convert from Kelvin to Celsius
return Temp;
}

// Serial output in 9600 Baud
void setup()
{
Serial.begin(9600);
}

// The program measures the current voltage value on the NTC
// and translates it intp °C for the serial output
void loop()
{

// Output on the serial interface
Serial.print("Current temperature is:");
Serial.print(temp);
Serial.print(char(186)); //Output <°> Symbol
Serial.println("C");
Serial.println("---------------------------------------");

delay(500);
}

```

Connections Arduino:

 Sensor +V = [Pin 5V] Sensor GND = [Pin GND] Sensor Signal = [Pin A5]

## Code example Raspberry Pi

!! Attention !! Analog Sensor  !! Attention !!

Unlike the Arduino, the Raspberry Pi doesn't provide an ADC (Analog Digital Converter) on its Chip. This limits the Raspbery Pi if you want to use a non digital Sensor.

To evade this, use our Sensorkit X40 with the KY-053 module, which provides a 16 Bit ADC, which can be used with the Raspberry Pi, to upgrade it with 4 additional analog input pins. This module is connected via I2C to the Raspberry Pi.
It measures the analog data and converts it into a digital signal which is suitable for the Raspberry Pi.

So we recommend to use the KY-053 ADC if you want to use analog sensors along with the Raspberry Pi.

!! Attention !! Analog Sensor  !! Attention !!

```#############################################################################################################
### Commercial use only after permission is requested and granted
###
### KY-053 Analog Digital Converter - Raspberry Pi Python Code Example
###
#############################################################################################################

# This code is using the ADS1115 and the I2C Python Library for Raspberry Pi
# This was published on the following link under the BSD license
from time import sleep

# import needed modules
import math, signal, sys, os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# initialise variables
delayTime = 0.5 # in Sekunden

# choosing the amplifing gain
gain = 4096  # +/- 4.096V
# gain = 2048  # +/- 2.048V
# gain = 1024  # +/- 1.024V
# gain = 512   # +/- 0.512V
# gain = 256   # +/- 0.256V

# choosing the sampling rate
# sps = 8    # 8 Samples per second
# sps = 16   # 16 Samples per second
# sps = 32   # 32 Samples per second
sps = 64   # 64 Samples per second
# sps = 128  # 128 Samples per second
# sps = 250  # 250 Samples per second
# sps = 475  # 475 Samples per second
# sps = 860  # 860 Samples per second

adc_channel_0 = 0    # Channel 0
adc_channel_1 = 1    # Channel 1
adc_channel_2 = 2    # Channel 2
adc_channel_3 = 3    # Channel 3

# temperature calculation function
def calcTemp(voltage):
temperature = math.log((10000/voltage)*(3300-voltage))
temperature = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temperature * temperature)) * temperature);
temperature = temperature - 273.15;
return temperature

#############################################################################################################

# ########
# Main Loop
# ########
# Reading the values from the input pins and print to console

try:
while True:

# print to console
print "Channel 0:", temp0, "C"
print "Channel 1:", temp1, "C"
print "Channel 2:", temp2, "C"
print "Channel 3:", temp3, "C"
print "---------------------------------------"

sleep(delayTime)

except KeyboardInterrupt:
GPIO.cleanup()
```

Connections Raspberry Pi:

Sensor

 +V = 3,3V [Pin 1 (RPi)] GND = GND [Pin 06 (RPi)] analog Signal = Analog 0 [Pin A0 (ADS1115 - KY-053)]

```sudo python KY-013_RPi_TemperaturSensor.py