Δευτέρα 12 Μαΐου 2014

ATtiny85 and DS18B20 temperature sensors

Blue Backlight Nokia 5110 LCD Module  $ 3.75

Except to light up led the ATtiny85 can be useful and in other simple projects. The most annoying think is the memory limitation (only 8kb available). I plan to read multiple DS18B20 onewire sensors (I have two loose and one more soldered in an RTC module). The measured values will showed on an cheap Nokia 5110 screen. I can't manage to use the DallasTemperature Library because the above mentioned memory limitation. I use the standard OneWire.h library. I found also a TinyWire(M/S) library specially made for ATtiny but is for I2C communication (maybe in future update). For the 5110 LCD screen I use the basic library LCD5110_Basic from henningkarlsen.com

In order to use multiple sensors we need first to know the HEX address of each one. I use the "Arduino 1-Wire Address Finder" sketch (found in many sites) with my UNO and connect one by one sensor write down the address of each sensor and remember to mark also the sensor itself to identify letter on.  


To save one pin the CS pin of LCD is connected to the GND and the pin D4 is used as OneWire bus. After all connections the only free pin is the RESET pin. This pin can be used as D5 but with limit load. 




// ATtiny85 + Nokia 5110 LCD + 3x DS18B20 sensors
// Binary sketch size: 7,476 bytes (of a 8,192 byte maximum)
// wire connections
// 1-Wire bus > D4

// LCD Clk   >  D0         LCD Din   > D1
// LCD DC    >  D2         LCD CE    > GND
// LCD RST   >  D3

#include <OneWire.h>
#include <LCD5110_Basic.h>

LCD5110 myGLCD(0,1,2,3,6); //D6 don't exist!!! conected to GND!


extern uint8_t MediumNumbers[]; //font that we use

OneWire  ds(4);  // 1-wire bus D4

byte addr1[8]={0x28, 0xEC, 0xAF, 0x5B, 0x05, 0x00, 0x00, 0x6F}; //loose sensor #1
byte addr2[8]={0x28, 0x6D, 0xF0, 0x3B, 0x05, 0x00, 0x00, 0xD8}; //loose sensor #2
byte addr3[8]={0x28, 0x27, 0xA0, 0x5B, 0x05, 0x00, 0x00, 0x67}; //sensor soldered to RTC (#3)

float temp;

void setup(void) {
   myGLCD.InitLCD(); // (xx)contrast value 70 default
}

void loop(void) {
  myGLCD.setFont(MediumNumbers);
  byte i;
  byte present = 0;
  byte data[12];
 
 myGLCD.print("1.", LEFT, 0);
 myGLCD.print("2.", LEFT, 16);
 myGLCD.print("3.",LEFT, 32);

  ds.reset();
  ds.select(addr1);
// sensor #1
  ds.write(0x44,0); //start conversion, NO parasite power
  delay(750);
  present = ds.reset();
  ds.select(addr1);   
  ds.write(0xBE);         // Read Scratchpad
  for ( i = 0; i < 9; i++){

       data[i] = ds.read();
  }
  // convert the data to actual temperature
 temp = ( (data[1] << 8) + data[0] )*0.0625;
 myGLCD.printNumF(temp, 2, RIGHT, 0);
 

// I know doing three time the same is NOT a clever thing!!
// sketch need to optimize

 ds.reset();
  ds.select(addr2); // sensor #2
  ds.write(0x44,0); 

  delay(750);    
  present = ds.reset();
  ds.select(addr2);   
  ds.write(0xBE);        
  for ( i = 0; i < 9; i++)
  {          
    data[i] = ds.read();
  }
  temp = ( (data[1] << 8) + data[0] )*0.0625;
  myGLCD.printNumF(temp, 2, RIGHT, 16);

 ds.reset();
  ds.select(addr3);
// sensor #3
  ds.write(0x44);
  delay(750);    
  present = ds.reset();
  ds.select(addr3);   
  ds.write(0xBE);        
  for ( i = 0; i < 9; i++)
  {           // we need 9 bytes
    data[i] = ds.read();
  }
  temp = ( (data[1] << 8) + data[0] )*0.0625;
  myGLCD.printNumF(temp, 2, RIGHT, 32); 
}


Because my LCD 5110 board is capable to work with 3.3V AND 5V I don't use any additional resistors but the most of 5110 screens on ebay is only 3.3V so either use only 3.3V for all or use limiting resistors to the LCD. Both the microcontroller and the DS18B20 sensors working with 3.3/5 Volts without problems. 


ATtiny85 micro Programmer

5pcs x Atmel ATtiny85 20PU + dip socket + sticker $9.90

 Because I plan to make a wearable project, I try the capabilities of ATtiny85, an arduino IDE compatible microcontroller with 4(5) digital I/O pins. 


 I decide to buy from EU seller because I want fast shipping. In the same e-store (attiny85.blogspot.com) there is also a "ATtinyShield" so I try to build my own similar shield for my UNO board. When use the UNO as ISP Programmer the sketch give some readings in order to check if all went well. I use 3mm leds for this purpose: 

  • Yellow - UNO Pin D7 - Programming / communication with the microcontroller
  • Red - UNO Pin D8 - Error / something goes wrong
  • Green - UNO Pin D9 - Heartbeat / ISP programmer is running

I add also one green 5mm test led connected to D3 or D0 pin of ATtiny85 selectable thru switch. Beware that when programming  is better to leave D3 connected with test led. In order to quick check the microcontroller I have merge the well known "Blink" and "Fade" examples of Arduino IDE.


// Fade & Blink test of ATtiny85 and my programmer shield

// initial declarations
int ledb = 4;
int ledf = 0;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup()  {
  // pins declarations
  pinMode(ledb, OUTPUT);
  pinMode(ledf, OUTPUT);
}

void loop()  {
  analogWrite(ledf, brightness);   
  analogWrite(ledb, brightness); // D4 isn't PWM pin so the fade effect became blink!
 
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 30 milliseconds to see the dimming effect   
  delay(30);                           
}


 One think that you must remember is that the ATtiny85 microcontroller came with 1MHz clock speed setting by the factory (the Blink/Fade effect will be tooo slow). So you have choose as board the 8MHz version and burn bootloader before upload the sketch.