Previous Guides:

Getting to Know Arduino : Part 1 : Hello, World!

Getting to Know Arduino : Part 2 : Hello, Again!

 

So after getting a little familiar with flashing an LED in Getting to Know Arduino : Part 1 : Hello, World! and Getting to Know Arduino : Part 2 : Hello, Again! of my Getting to Know Arduino, I have decided to throw a bit of a curve ball with my Part 3. So assuming that you already have picked up a Breadboard and some wires with your Arduino Uno. I decided that some of you may have been like I was after getting to grips with it:

 

"Where's the cool stuff? All I'm doing is flashing a light".

 

So to be topically "Internet of Things", we're going to play with a temperature sensor. It can seem daunting to start with, but once you have it plugged in then it's easy enough to get instant feedback to be reading Celsius and Fahrenheit. Honest! And what's more fun than having empirical evidence to back up a complaint of the weather?

 

So to get to know your Arduino a little more you will need the following:

 

 

Along with the Arduino IDE installed onto your computer. When you have your parts, you will want to plug them into your breadboard and there is a specific way to plug them in to complete the circuit. Here is an example of a completed setup:

 

temp_sense.jpg

 

So at the moment, this looks like a mess of wiring in a dimly lit photograph (I guess that's partly what it is). However, there are some directions that can be taken to supplement this photograph. On a typical breadboard there is a grid referencing system. As you can partly see in the photograph it is labelled A to J along the columns across the top and numbered from 1 downwards for the rows. So simply we can wire it up thus:

 

  • Insert the DS18B20 to the breadboard. Push the outer pins into row 1 and the centre pin into row 2.
  • Insert the 4.7k resistor into row 2 and into row 5. Orientation does not matter.
  • Connect a wire:
    • From Pin 10 on the Arduino onto row 2.
    • From row 1 to GND on the Arduino, it does not matter which GND this is.
    • From row 5 to 5v on the Arduino

You should now have something that looks like the diagram! Only, possibly a lot neater.

 

Now once you have your temperature sensor, resistor and wires all connected to the Arduino, you can plug it into your computer via USB. However, we cannot go about using the Arduino IDE just yet, we need to get the example code to work from.

 

The temperature sensor communicates with the Arduino via a protocol called 'OneWire' and this is a protocol (a method of communicating) that was created by Dallas Semiconductor (now Maxim Integrated Products). The DS18B20 has a datasheet which goes into all sorts of detail about how it communicates on a very low level, what its voltage and current limits are, etc. This is common for electronic components and these datasheets, while laborious to read and learn are very useful for finding out what the equipment is capable of and how you can get it working.

 

So to communicate with the temperature sensor, we need to download the OneWire library, it can be downloaded from Paul Stoffregen's site, or I have attached it to this blog post. Once you have your OneWire.zip file, you will need to extract it to your Sketchbook folder. If you open up your Arduino IDE, click the File menu and select Preferences, your Sketchbook folder will be displayed under 'Sketchbook Location'. Extract the OneWire.zip file into this location using Windows or free software such as 7-Zip (merely copying the .zip file into the sketchbook folder will not suffice).

 

When the library is in place, restart the IDE and then you can open the File menu, go to Sketchbook, then OneWire and choose DS18x20_Temperature. This will load a sketch that you can then select the Sketch menu, and select Verify/Compile which will push it to your Arduino.

 

Once the sketch has compiled and it is on your Arduino, click the Tools menu, then select Serial Monitor. You should now see some values including the temperature detected by the sensor!

 

Since the code is running on the Arduino now, you can quite happily sit back and watch it collect the temperature. However what you could also now do, is alter what it prints to the Serial Monitor so that it could be picked up by, say, Python and presented as a graph. Below is code you can compile and send to your Arduino which will only show the temperature in Celsius without the additional information. Bare in mind that the temperature might need calibrating and it could be off by a few degrees, this is correctable in software.

 

#include <OneWire.h>

OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)

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

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    default:
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not

  present = ds.reset();
  ds.select(addr);
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  }
  celsius = (float)raw / 16.0;
//  fahrenheit = celsius * 1.8 + 32.0;
}








 

 

If you want the Fahrenheit temperature, you can just un-comment (remove the //) on the line. Have a play about with the code, look up the functions as to what they do and see what you can make of it. If you have any troubles please leave a comment. There's also more information in the The specified item was not found. community group!