NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » OneWire DS18B20 Temperature Switch on ATtiny85

May 07, 2014
by Noter
Noter's Avatar

My camper has a 110vac supply/charger for the 12v system that is in a very small space. The problem is when it heats up there is no vent and the unit tends to overheat. So I've decided to put a vent with a fan to manage the temperature.

For this project the tiny85 has plenty of speed (internal 16mhz) and flash memory so I can go a less efficient route and program it in arduino. While it's less efficient from a hardware perspective, it's much more efficient from a programming perspective as it only took a few hours to get the prototype programming done. The tiny85 must be programmed via an ISP programmer so I installed the ArduinoISP sketch on my UNO and used it to program the tiny85 on a piggy back development board.

Another cool thing about using Arduino is that it's very easy to implement a software serial port for printing over a serial line. Highly recommended if you can spare the pins.

Don't get me wrong, I am still a nerdkits guy but sometimes it nice to take the easy way out. ;-)

OneWire DS18B20 Temperature Switch on ATtiny85

// OneWire DS18B20 Temperature Switch on ATtiny85
//
// Check temperature every second.
// Fan turned on when hot, off when cool.
//

// fan switch temperatures in Fahrenheit
#define HOT 80.0
#define COOL 79.0

#include <OneWire.h> 
#include <SoftwareSerial.h>

// pins
#define SERIAL_RX 3
#define SERIAL_TX 4
#define ONEWIRE_BUSS 0
#define STATUS_LED  1
#define FAN 2

SoftwareSerial TinySerial(SERIAL_RX, SERIAL_TX); // RX, TX

OneWire TemperatureSensor(ONEWIRE_BUSS);  // Dallas one wire data buss pin, a 4.7K resistor pullup is needed

void blink(void){
    digitalWrite(STATUS_LED,HIGH);
    delay (250);
    digitalWrite(STATUS_LED,LOW);
    delay (175);
}

void setup(void) {
    pinMode(STATUS_LED, OUTPUT);
    pinMode(FAN, OUTPUT);
    TinySerial.begin(9600);
    TinySerial.println(F("DS18x20 Temperature Switch"));
    blink();
    blink();
}

void loop(void) {
    byte i;
    byte data[12];
    int16_t raw;
    float celsius, fahrenheit;

    TemperatureSensor.reset();       // reset one wire buss
    TemperatureSensor.skip();        // select only device
    TemperatureSensor.write(0x44);   // start conversion

    delay(1000);                     // wait for the conversion

    TemperatureSensor.reset();
    TemperatureSensor.skip();
    TemperatureSensor.write(0xBE);   // Read Scratchpad
    for ( i = 0; i < 9; i++) {       // 9 bytes
      data[i] = TemperatureSensor.read();
    }

    // Convert the data to actual temperature
    raw = (data[1] << 8) | data[0];
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    TinySerial.print(F("  Temperature = "));
    TinySerial.print(fahrenheit);
    TinySerial.print(F(" 'F"));

    // Turn Fan on/off depending on temperature
    if(fahrenheit > HOT){
      digitalWrite(FAN,HIGH);  // on
    }

    if(fahrenheit < COOL){
      digitalWrite(FAN,LOW);   // off
    }

    digitalWrite(STATUS_LED,digitalRead(FAN));  // copy fan state to status led

    if(digitalRead(FAN)==LOW){
      TinySerial.println(F(" [OFF]"));
    } 
    else {
      TinySerial.println(F(" [ON]"));
    }

}
May 08, 2014
by JKITSON
JKITSON's Avatar

NOTER

This is what I have been needing for my camper's solar system. My charge control unit is enclosed also. I am going to build one with an external fan. Thanks

This forum is still great...

Jim

May 08, 2014
by Noter
Noter's Avatar

Jim, please tell about your camper solar system. I'd like to add one to my camper at some point and any advice you can give would be helpful.

May 08, 2014
by JKITSON
JKITSON's Avatar

I installed a 45 watt solar panel on the roof. That connects to a Trace charge controller. There are 2 each OPTIMA rv batteries in parallel for 12 volt. My wife & I both use CPAP units to sleep with at night. So far after 4 years we have had plenty of power and no problems (other than in the 110+ Arizona heat for cooling the controller). When we go out now during high heat I leave the seat cushion off & it works ok. The solar panel is about 30 inches square. Hope this helps

Jim

May 09, 2014
by sask55
sask55's Avatar

Well! This is very interesting for me. I did not realize that there is a library for these micros available to handle the Dallas one wire interface. A few months ago I was playing around with interfacing to a bin temperature sensor setup. The sensor configuration is made up of over 150 individual one wire sensors connected to a single Dallas on wire buss. I more or less gave up on the idea of achieving reliable readings from the buss because of timing and other problems that I was experiencing setting up the Dallas one wire protocol.

Latter this fall, I will certainly be looking into what functions are available using “onewire.h”.

I don’t know why I never game across this information when I was investigating the Dallas one wire system. I can see already that this is a much more sensible approach then the one I was trying to implement.

Thanks for the post.

Darryl

May 11, 2014
by Noter
Noter's Avatar

The onewire library is part of the arduino development platform. While there are many add-on libraries, onewire comes with the core install and includes two little example programs that make it easy to get going. Arduino has their own bootloader and you can burn their bootloader on any atmega but it's easier to use one of their development boards to get started. They also like to use FT232 chips for the USB serial setup so might as well get one of those too.

Arduino downloads - http://arduino.cc/en/Main/Software

FT232 usb to serial - eBay

Development board - eBay

May 11, 2014
by Noter
Noter's Avatar

BTW, I like to solder 90' pins/sockets on my little arduinos so the connection with the FT232 card is done without extra wiring -

one

!one

May 11, 2014
by Rick_S
Rick_S's Avatar

Wow, i thought getting those on arduino day at sparkfun for $3.00 Ea. Was a good price. Looks like they pulled sparkfuns open source design and ran with it.

May 11, 2014
by Noter
Noter's Avatar

Yep, other than the sparkfun logo this one looks exactly the same. I've noticed minor differences in some of them depending on the vendor but functionally they are all equivalent so I usually just go for the least expensive.

And that price on the FT232 board is pretty amazing too. Just the chip alone from mouser is about $4 which is more than the whole little board from china.

May 11, 2014
by sask55
sask55's Avatar

Thanks again Noter.

I have never looked into the Arduino it does look to be very interesting. I will be ordering a bit of the Arduino based hardware so that I will have something available to play around with when I get the time.

We are currently using a commercially available system to continuously log and report on the grain bin temperature cable readings. $$$. We also have a number of bins that are still being occasionally checked using a hand held unit.

When I have time, I will be investigating the Arduino one wire software. I require a setup to handle the one wire addressing and communication from a large number of temp sensor chips all connected on a single one wire buss. Best case scenario would be if the coding was available to read the individual sensors. Building a GUI and data recording system on a connected PC should be relatively straight forwarded. Failing that, if the code is open source I may gain some valuable insight as how to establish one wire communications on the one wire buss.

With my very limited programming skills I found it difficult to establish commutation with even one sensor on the buss let along 150. The Dallas one wire protocol is all about timing, a lot of timing. I found the entire concept interesting but well beyond what I am likely to be able to accomplish in any reasonable timeframe. This Arduino approach may offer more attainable solution.

One more project to put back on my “to do list”

May 12, 2014
by Noter
Noter's Avatar

I was mistaken about onewire being in the arduino core download. It's an add-on and the latest can be found at http://www.pjrc.com/teensy/td_libs_OneWire.html. Install the arduino development system first then for add-on libraries just download, unzip, and copy to the appropriate directory to install.

The hard programming part with onewire is dealing with multiple sensors and having to do a the binary search to discover their addresses. This is handled for you in the onewire library. The example that comes with the library searches for all available devices and then reads/reports temperature from each one. So you can use the example program to verify communication with your sensor array and check temps before you start writing your specific application.

The wire library (aka I2C) is the built-in that I was confusing with onewire and it makes using I2c devices easy as pie. Another add-on is the DHT11/22 library that is very handy because those sensor interfaces are similar to onewire but different enough to need their own drivers.

A couple of years ago I came across a really good eBay deal on DS18b20's so I bought a bag of them. As I was exploring arduino and hooking up everything I had to it I used as many of the DS18b20's as would fit on a small breadboard. No problems until I added the micro SD card and ran out of memory. A little more programming and the SD card is now an I2C slave and works great. The one thing you will notice about arduino compared to writing all your own code for the nerdkit is arduino librarys have a rather large footprint so you won't be able to fit as much on a single mpu with arduino. Another difference is most of the time you'll be using the serial interface to display data on your PC instead of the nerdkit LCD but when you do want to use a LCD with arduino there is an library for it too.

pic

May 12, 2014
by JKITSON
JKITSON's Avatar

Very interesting. Have looked at the data sheets & am impressed. Don't know if I have the ambition to learn a new system & programming. Thanks guys for a very nice description of the hardware etc. Jim

May 12, 2014
by Noter
Noter's Avatar

No doubt learning new things always takes more time and effort. Sometimes its worth it and others not. In the case of arduino I am happy I took the time to learn about it. There are a lot of arduino users out there that have never looked at a data sheet and know very little about the mcu they are using. Taking the nerdkit route and learning the nuts and bolts first really helps a lot and definitely eases the task of learning the arduino.

I also learned something about the DS3232 RTC in the above experiment and that is to get the extreme accuracy as advertised it needs to be powered with 3.3v. At 5v it's off by a couple of seconds a day but at 3.3v it's right on. Changed the I2C buss to 3.3v too so there was no 5v going to the RTC over those lines either.

May 12, 2014
by sask55
sask55's Avatar

Noter

You have once again provided a lot of detailed and very specific information. It appears to me, after a very quick look at the links you provided, that my original idea of building a bin monitoring setup will be greatly simplified. By using an arduino setup and software to handle the communication with the OneWire buss the project becomes mostly about developing a PC application to handle the GUI, data logging, email notifications ect. For me that is likely to be enough of a challenge depending on how carried away I get. I imagine that when I get around to actually doing this there will be a considerable learning curve setting up the Arduino. I am confident that if I can get the individually identified temperature readings to a PC on a com port I will be able to handle the information from there and more or less emulate the system that We have operating now in one location.

I just don’t have the available time to get started on this during the growing (crops) season. I will be doing some reading and ordering a few parts this summer.

I said it before but I think it is worth saying again. This forum, with its very friendly open and informative manner, is by far the best source of general electronic information I can find. Over the past few years I have learned a great deal about electronics and embedded programming by just following this forum. Many times I have made use of something I discovered here that I did not even realize was possible or available until it was posted to this forum. Even in its current state for me this is a very valuable and much appreciated forum. Somehow; I would sure like to see Nerdkits to continue and be revitalized.

Darryl

May 13, 2014
by Ralphxyz
Ralphxyz's Avatar

And Darryl you have contributed greatly to the forum, for me personally I have greatly grown thanks to your patience advice and help.

Post a Reply

Please log in to post a reply.

Did you know that multiple microcontrollers can communicate with each other? Learn more...