Advertisement
belrey10

[KY-001] Temperature Sensor

Nov 9th, 2024
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 6.25 KB | Source Code | 0 0
  1. /*
  2.  * inventr.io 37 in 1 Sensor Kit (https://learn.inventr.io/product/37-in-1-sensor-kit/)
  3.  * Sensor Course (https://learn.inventr.io/course/sensor-training/)
  4.  *
  5.  * Code contributions by David Schmidt and user biTTOE.
  6.  *
  7.  * Lesson - [KY-001] Temperature Sensor
  8.  *
  9.  * The KY-001 is a simple temperature sensor module that measures ambient temperature using
  10.  * a DS18B20 digital thermometer chip.  The DS18B20 is a 1-Wire digital thermometer chip that
  11.  * is often used with the KY-001 temperature sensor module in electronics projects.
  12.  *
  13.  * The DS18B20 provides 9-bit to 12-bit temperature readings in Celsius, with a temperature
  14.  * range of -55°C to +125°C and a temperature accuracy of ±0.5°C in the range of -10°C to +85°C.
  15.  * The chip communicates with microcontrollers using the 1-Wire protocol, which allows multiple
  16.  * DS18B20 devices to be connected to a single microcontroller pin, making it a cost-effective
  17.  * solution for temperature sensing in various applications.
  18.  *
  19.  * Library requirements:
  20.  * Install OneWire library from Arduino IDE library manager
  21.  *
  22.  * Code contributions:
  23.  *    David Schmidt (davids@inventr.io)
  24.  *    biTToe (scy1961@yahoo.com)
  25.  */
  26.  
  27. #include <OneWire.h>  // OneWire library
  28.  
  29. #define DEBUG false  // Set to false and use Serial Plotter for graph of all temperatures
  30.  
  31. /* This project just needs a Digital pin.
  32.  * On the Hero (Arduino Uno compatible) we *could* use: D0-D13, A0-A5.
  33.  * Skip: A0-A5 (save for Analog),
  34.  *       D0/D1 (used by USB),
  35.  *       D2/D3 (save for interrupts),
  36.  *       D13 (used by LED_BUILTIN and SPI Clock),
  37.  *       D5, D6, D9, D10 and D11 (save for PWM)
  38.  *       D11 (SPI MOSI)
  39.  *       D12 (SPI MISO)
  40.  * Recommended for fewest conflicts:
  41.  *    D4, D7 or D8
  42.  */
  43. const uint8_t KY_001_PIN = 4;   // Good digital pin not used for other purposes
  44.  
  45. // Use OneWire library to communicate with KY-001's DS18S20 temperature chip
  46. OneWire ds(KY_001_PIN);   // Create OneWire object to communicate with sensor
  47.  
  48. // Each OneWire sensor is configured with a unique 64 bit address starting with an
  49. // 8 bit "family" code, 48 bit address and an 8 bit Cyclic Redundancy Check (CRC)
  50. // checksum to validate that the address read has no errors.
  51. //
  52. // Since we can have multiple DS18x20 sensors on the wire we will poll for all
  53. // devices on the wire, validate and identify each and save the addresses of all
  54. // DS18x20 type sensors here.
  55. const int MAX_DS_DEVICES = 5;   // We will save addresses for up to 5 sensors.
  56. byte ds_addresses[MAX_DS_DEVICES][8];   // 64 bits for each address
  57. int ds_count = 0;
  58.  
  59. // Function to print a 64 bit number as hexadecimal digits
  60. void print_address(byte[8]);
  61.  
  62. void setup(void) {
  63.   Serial.begin(9600);  // initialize console to 9600 baud.  Be sure to set Serial Monitor to 9600 baud
  64.  
  65.   // Scan OneWire for all devices, saving all DS18x20 device addresses
  66.   for (int i = 0; i < MAX_DS_DEVICES; i++) {
  67.     if (DEBUG) Serial.println("Searching for valid devices on OneWire");
  68.     if (ds.search(ds_addresses[ds_count])) { // if we found another address
  69.       if (DEBUG) Serial.print("Device Address: ");
  70.       if (DEBUG) print_address(ds_addresses[i]);
  71.  
  72.       if (OneWire::crc8(ds_addresses[i], 7) != ds_addresses[i][7]) {
  73.         if (DEBUG) Serial.println(" CRC is not valid (skipped)");
  74.         break;
  75.       }
  76.  
  77.       switch (ds_addresses[ds_count][0]) {
  78.       case 0x10:
  79.         if (DEBUG) Serial.println(", DS18S20");
  80.         break;
  81.       case 0x28:
  82.         if (DEBUG) Serial.println(", DS18B20");
  83.         break;
  84.       default:
  85.         if (DEBUG) Serial.println(", UNKNOWN (skipped)");
  86.         continue;     // continue for loop with next device.
  87.       }
  88.       ds_count++;   // valid DS18x20 device, point to next address slot.
  89.     } else {
  90.       break;  // No more devices.  ds_count countains count of valid devices found
  91.     }
  92.     delay(1000);
  93.   }
  94.   Serial.print("Found ");
  95.   Serial.print(ds_count);
  96.   Serial.println(" devices.\n");
  97.   if (ds_count == 0) return;
  98. }
  99.  
  100. void loop(void) {
  101.   // For conversion of raw data to C
  102.   int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  103.  
  104.   byte data[12];
  105.   byte addr[8];
  106.  
  107.   // DS18S20 supports having multiple devices on the same wire, each returning their own address
  108.   // Each time through the loop() we will find the next module and retrieve it's data
  109.   for (int i = 0; i < ds_count; i++) {
  110.     if (!DEBUG && i > 0) Serial.print(", ");    // Add separator if not in DEBUG mode
  111.     // Now read data from detected sensor
  112.     ds.reset();
  113.     ds.select(ds_addresses[i]);
  114.     // since we supply 5v we don't need to supply parasitic power and wait.
  115.     ds.write(0x44, false);  // start conversion
  116.     // Wait until we get a completion bit (1)
  117.     while(!ds.read_bit()) {}  // Returns 0 (false) until temp is ready to read.
  118.  
  119.     // we might do a ds.depower() here, but the reset will take care of it.
  120.     byte present = ds.reset();
  121.     ds.select(ds_addresses[i]);
  122.     ds.write(0xBE);  // Read Scratchpad
  123.  
  124.     if (DEBUG) Serial.print("Data    = ");
  125.     // Serial.print(present, HEX);
  126.     // Serial.print(" ");
  127.     for (int i = 0; i < 9; i++) {  // we need 9 bytes
  128.       data[i] = ds.read();         // Read next byte
  129.       if (DEBUG) {
  130.         char hex_string[3];           // 2 hex digits plus trailing null
  131.         sprintf(hex_string, "%02x", data[i]);
  132.         Serial.print(hex_string);
  133.         Serial.print(" ");
  134.       }
  135.     }
  136.     uint8_t crc = OneWire::crc8(data, 8);
  137.     if (crc != data[8]) {
  138.       if (DEBUG) {
  139.         Serial.print(" BAD CRC! (0x");
  140.         char hex_string[3];           // 2 hex digits plus trailing null
  141.         sprintf(hex_string, "%02x", crc);
  142.         Serial.print(hex_string);
  143.         Serial.println(")");
  144.       }
  145.       return;
  146.     }
  147.     if (DEBUG) Serial.println();
  148.  
  149.     // Get 16 bit reading from first two data bytes
  150.     TReading = (data[1] << 8) + data[0];
  151.     Serial.print(TReading * .0625);
  152.  
  153.     if (DEBUG) Serial.print(" degrees Celsius\n");
  154.     //End conversion to C
  155.   }
  156.   Serial.println();
  157. }
  158.  
  159. void print_address(byte address[8]) {
  160.   char hex_string[3];  // 2 hex digits plus trailing null
  161.  
  162.   for (int i = 0; i < 8; i++) {
  163.     sprintf(hex_string, "%02x", address[i]);
  164.     Serial.print(hex_string);
  165.     if (i == 3) Serial.print(":");  // display ':' between 32bit values
  166.   }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement