Using ATTiny84 with DS18B20 temp sensor as temperature logger

Hi All,

I have been trying to build a temperature measurement and recording kit using an ATTiny84 chip and a DS18B20 temp sensor. The intention is to take temperature readings every 10 seconds and send these via serial communication to an external storage area such as a raspberry pi.

After some googling I managed to find a tutorial on how to use an arduino to read the sensor and send the result to the serial monitor every second.

#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(3); 

DallasTemperature sensors(&oneWire); 
void setup()
{
  // start serial port
  Serial.begin(9600);
  // Initialize sensors
  sensors.begin();
} 
void loop()
{
  sensors.requestTemperatures();
  float currentTemp0;
  currentTemp0 = sensors.getTempCByIndex(0); 
  Serial.println(currentTemp0,2); 
  delay(10000);
}

This is exactly what I want to do but be able to do it using the ATTiny84 chip instead of the arduino. Does anyone know if this is possible?

I have done it, using ATtimy85, so I can't see why not.

http://forum.arduino.cc/index.php?PHPSESSID=pj61g4bqqengv8m112nh5encg3&topic=216663.0

edit: I am using this core for tinys https://code.google.com/p/arduino-tiny/

Thanks Erni. I follow your post and the various pages that it led onto and managed to get as far as having my ATtiny pump out 0.00 to the serial which is a major leap forward for me so many thanks for your help. This at least proves I can write and send stuff from the chip.

When preparing, I was able to get the temp sensor working on an arduino so that it made a measurement every 10 seconds, and successfully sent the result to the serial monitor. For some reason it doesn't want to do this when I hook it up to the Tiny84. Ive tried connecting up the sensor to each leg of the chip in turn, and remembered to include the resistor each time but still no luck. Does anyone have any ideas why this may be?

There is a full example in the Cosa library that does all that. Download Cosa and install. Select the example sketch CosaDS18B20 and Cosa ATtiny84 as board. Compile and upload. All is included. You do not have to surf madly for libraries and cores. And there are tons of other classes to use. Fully integrated and compiles for all Arduino (AVR) boards, Mighty and Tiny.

https://github.com/mikaelpatel/Cosa https://github.com/mikaelpatel/Cosa/blob/master/examples/OWI/CosaDS18B20/CosaDS18B20.ino

If you want to send the temperature readings wireless there is an example sketch for that.

https://github.com/mikaelpatel/Cosa/blob/master/examples/Wireless/CosaWirelessDS18B20/CosaWirelessDS18B20.ino

By changing a few lines in the sketch configuration you can change between RF433, CC1101 and NRF24L01+. More details on the blog.

Cheers.

Hi kowalski, Thanks for your response. I have just looked at the links you put and they look extremely interesting and worth further investigation however one part caught my attention; 'Unfortunately Cosa is not a beginners entry level programming platform'. Im afraid I would consider myself a beginner so I will have to leave it for now.

Im hoping that I might be able to get my present setup working first as Ido seem to have got quite close and I hoping that its just a couple of lines of code to change or a rearrangement of my wiring left to do. The sketch from my first post seems to be working. Just need to find out why those zeros are appearing instead of a temperature reading.

If anyone could offer any suggestions to help me solve it that would be a huge help.

Just tryed the sketch mentioned in reply #1, on a ATtiny84, and it works.

The only difference I made was the pin assignment

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

PA7 is physical pin 6

Thats it Erni. All done. I now have the temperature being displayed. Fantastic! Thank you very much for your help and advice. :-)

You are welcome

My next step was to detach the ATtiny from the arduino and, using a hacked USB/Serial cable, plug it directly into the USB port of my windows laptop. I opened up the arduino IDE and opened the serial monitor. The text was garbled and mangled. Its definitely receiving things from the ATtiny as the monitor updates every 10sec, however the text is all on one line which suggests that the line break character is being mangled too. Any ideas gratefully received, and thanks in advance.

The garbled text could be a sympton of bad timing.

You can tune the internal osccilator by setting a value for the OSCCAL

There are sevaral option for tuning.
Coding Badly has made TinyTuner

TinyTuner 1

http://forum.arduino.cc/index.php/topic,8553.0.html

And TinyTuner2

Another option is to use SoftwareSerial as an indicator for the tuning.
If you load this sketch to your Attiny84 it will output the OSCCAL value to the serial monitor.
A potmeter connected to PA3 will vary the OSCALL value, and you will see the output going
from garbled to readable and garbled again. Then you pick a value in tyhe midlle of the readable text.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3 ); //rx=PB2,tx=PA7 setup of software serial

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

void loop() {
  int val=analogRead(3); //PA3
  OSCCAL=val/4;
  mySerial.print("Osccal= ");  
  mySerial.println(OSCCAL,HEX);
  delay(200);
}

If you have a frequencee counter you can use the sketch below.
With a attiny84 @ 8MHz it will output 1MHz on pin5, if it is tuned correct.
Again a potmeter is connected to PA3, which will alter OSCCAL.
When you read 1 MHz on the frequence counter, you can read the OSCCAL value on the serial monitor.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3 ); //rx=PB2,tx=PA7 setup of software serial

void setup(){
  mySerial.begin(9600);
  OSCCAL = 0x9F;  
  pinMode(5,OUTPUT);
  noInterrupts();  
  TCNT1 = 0;
  TCCR1A=0;
  TCCR1B=0;
  TCCR1B |=(1<<WGM12); //Start timer 1 in CTC mode Table 14.4
  TCCR1A |=(1<<COM1B0); //Timer1 in toggle mode Table 14-1
  TCCR1B |=(1<<CS10) ; //prescaler Table 14-5, no prescal
  interrupts();
  OCR1A=3; //CTC Compare value
 }

void loop(){
  int val=analogRead(3); //PA3
  OSCCAL=val/4;
  mySerial.print("Osccal= ");  
  mySerial.println(OSCCAL,HEX);
  delay(200);
}

Thanks Erni,

Ive opted to go with TinyTuner2. Reading the instructions and my brain is having a burp. What is meant in Coding Badly's instructions when it says:

'Reset the processor. A welcome message should be displayed.'

How do I reset the processor? I'm sure will regret this question but short of cutting the power and then turning it on; Im lost.

'Reset the processor. A welcome message should be displayed.'

You breefly connect the reset pin (PB3) to ground, and you will see this in the serial monitor:

--------------------------------------------------------------------------------
Poor Man's Tiny Tuner
Slowly send lowercase 'x' to tune the oscillator...

  // Starting OSCCAL value is 0x9C

Step  OSCCAL
  1

Then you start sending x 's. You get a new line for every x, as the tuner try to determin the right OSCCAL value.

This is the final result for my particular ATtiny84

Poor Man's Tiny Tuner
Slowly send lowercase 'x' to tune the oscillator...

  // Starting OSCCAL value is 0x9C

Step  OSCCAL
  1     9F
  2     9E
  3     9F
  4     9F
  5     9F
  6     9F
  7     9E
  8     9E
  9     A0
 10     A0
 11     A0
 12     A0
 13     A0
 14     9F

Copy-and-paste the following line of code at the top of setup...

  OSCCAL = 0x9F;

So in my sketch i will place this value in setup()

void setup(){

OSCCAL = 0x9F;

//other code

}

BYTW this is TinyTuner 1

Do you think that this is also possible with a ATTiny44?

We have the same goal; read a DS18B20.

But till this moment it didn't work yet. However I have to try the solution that is given over here, but I'm now at work so I will do this tomorrow.

Moreover, my question for now is; has someone experience with the ATTiny44 and the DS18B20?