Δευτέρα 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. 

Δευτέρα 21 Απριλίου 2014

Testing ATmega32U4 Pro Micro (Leonardo combatible)

Leonardo Pro Micro (ATmega32U4) $5.31

This time I was to check the compact version of arduino without the need of any additional FTDI cable to connect. This board hasn't any led connected to Pin13 like UNO, so will use the onboard led for RX and TX indication. Actually RX led is connected to Pin17 (internal pin), but in our example will use predefined macros RXLED() and TXLED(). These macros are not compatible with the UNO board but the UNO has already onboard led on Pin13.


Test sketch:

// Blink test with RX-TX leds of Leonardo micro

void setup()
{
// nothing need to be setup :)
}

void loop()
{
 RXLED1;     //RX led set to ON
 TXLED0;     //TX led set to OFF 

 delay(1000);// wait for a second
 RXLED0;     //RX led set to OFF
 TXLED1;     //TX led set to OFF
 delay(1000);// wait for a second
}

Τρίτη 14 Ιανουαρίου 2014

Working with ENC28J60

ENC28J60 Lan module $3.59
5V 2 channel relay module $2.00
DHT11 sensor module $2.44

today I work with my ENC28J60 module, a cheap alternative to the standard W5100 Ethernet shield. If you want to use with the 1.0.5+ IDE then the EtherCard library is one way. after many research I found the best articles here (http://www.lucadentella.it/en/category/enc28j60-arduino)
My final scope is to remotely control some relays and read sensors thru web interface. Finally I manage to write a sketch, but I want to read/learn more in order to make html page code better and of course to add security in order to not anybody open/close my relays ...



This is the sketch till now:

 /**************************************************
Remotelly read sensor data and control relays
without any security... yet ;-)
arduino UNO, ENC28J60, DT11 and two relays module
DT11 3 cables: A0 analog pin, VCC +5V, GND
ENC28J60 6 cables: SI ->D11, SO ->D12, SCK ->D13,
                   CS ->D8, VCC ->3v3, GND
Relays Module 4 cable: IN1 ->D2, IN2 ->D3,
                       VCC +5v, GND
This is sketch based to infos collected from varius
sites but the most valuable was the pages found on
http://www.lucadentella.it/en/category/enc28j60-arduino/
many many many thanks for it's hard and good work!!!
the ENC28J60 solution is very cheap but it lacks in
support, especially for EtherCard.h library which is
the only one to make small and quick sketches working
with IDE 1.0.5+

I am not experience in programming so excuse any
mistakes that I make...
any comments are welcome in my blog
http://demerduino.blogspot.gr/
sketch ver.1401.14
*******************************************************/
// declare libraries and variables
#include <EtherCard.h>
#include <dht.h>

#define dht_dpin A0
#define REL1PIN  2
#define REL2PIN  3

static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
static byte myip[] = {192,168,1,2};
byte Ethernet::buffer[500];

boolean rel1Status;
boolean rel2Status;
unsigned long timer;
float temp;
float hum;
char temp_str[10];
char hum_str[10];

dht DHT;

void setup () {

  Serial.begin(9600);
 
// small delay for sensor
  delay(500);

// initialize lan CS pin on digital pin 8
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 8))
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");

  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");

// define relays pins
  pinMode(REL1PIN, OUTPUT);
  pinMode(REL2PIN, OUTPUT);
 
// in my board the 'LOW' (false) state put relays ON so I start with 'HIGH'
// if you want to change this change also the states OFF-ON later on the sketch
  rel1Status = true;
  rel2Status = true;

}
 
void loop() {

// applying the status on pins (relays)
 digitalWrite(REL1PIN, rel1Status);
 digitalWrite(REL2PIN, rel2Status);

// This is the "heart" of the DT11 program, the proposal sample rate
// is 1/sec after the variables fill the string variables
 if (millis() > timer) {
   timer = millis() + 1000;
   DHT.read11(dht_dpin);
   temp= DHT.temperature;
   hum = DHT.humidity;
   dtostrf(temp,6,2,temp_str);
   dtostrf(hum,6,2,hum_str);
  }

// check for ethernet and tcp packet
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

// read the parameter and change the status of relay's pin
  if(pos) {
    
      if(strstr((char *)Ethernet::buffer + pos, "GET /?REL1") != 0) {
        rel1Status = !rel1Status;
      }
    
      else if(strstr((char *)Ethernet::buffer + pos, "GET /?REL2") != 0) {
        rel2Status = !rel2Status; 
      }
    
// bulding the html file...
// the two $S are filled with string variables
      BufferFiller bfill = ether.tcpOffset();
      bfill.emit_p(PSTR(
        "HTTP/1.0 200 OK\r\n"
        "Content-Type: text/html\r\n"
        "Pragma: no-cache\r\n\r\n"
// if you want to enable the auto refresh mode
//      "<meta http-equiv='refresh' content='5'/>"
        "<html><head><title>Web Control</title></head><body>"
        "<body bgcolor=""#ccccff"">"
        "<font size=""6"">"
        "<center><br>"
        "Temperature... : "
        "<b> $S &deg;C </b> <br>"
        "Humidity... : "
        "<b> $S %</b><br>"
        "____________________<br>"
        "<BR>"),
        temp_str,hum_str);

// and now the relays status / hyperlinks    
      if(rel1Status) bfill.emit_p(PSTR("Relay No_1  is <a href=\"/?REL1\">$F</a><br>"),PSTR("OFF"));
      else bfill.emit_p(PSTR("Relay No_1  is <a href=\"/?REL1\">$F</a><br>"),PSTR("ON"));
   
     if(rel2Status) bfill.emit_p(PSTR("Relay No_2   is <a href=\"/?REL2\">$F</a><br>"),PSTR("OFF"));
     else bfill.emit_p(PSTR("Relay No_2   is <a href=\"/?REL2\">$F</a><br>"),PSTR("ON"));

// and html close, any footer goes here...   
      bfill.emit_p(PSTR("<br></body></html>"));
      ether.httpServerReply(bfill.position());
  }
}



Πέμπτη 9 Ιανουαρίου 2014

Special characters and graphics on 1602 LCD

All LCD displays that are compatible with the Hitachi HD44780 driver has the following haracter map:
All above standard (?) characters can be printed with default library  'LiquidCrystal lcd' and use the lcd.write(number) command. the 'number' can be taken from above table when add the upper row number with the vertical (par example 43 is the + symbol). I found useful the characters 126, 127, 219, 223, 255.

As for graphics and custom characters this can been done with little more programming. Some examples can been found in the next pages

http://forum.arduino.cc/index.php/topic,54411.0.html
http://arduino.cc/en/Reference/LiquidCrystalCreateChar
http://www.quinapalus.com/hd44780udg.html


16x2 LCD Shield


This time I try my first LCD screen

SainSmart LCD 1602 Keypad Shield ($6.93)

http://www.sainsmart.com/arduino/arduino-shields/lcd-shields/sainsmart-1602-lcd-keypad-shield-for-arduino-duemilanove-uno-mega2560-mega1280.html 

A 16x2 LCD panel with couple keys on board. on the manufactures site there is couple sample sketches but because the LCD is actual a standard 1602 LCD thus is capable to use any existing sketch compatible to the Hitachi HD44780 driver. The only change is to pin assignments so change in library declaration

LiquidCrystal lcd(12, 11, 5, 4, 3, 2) with this one:
LiquidCrystal lcd(8, 9, 4, 5, 6, 7)

also I found the minimum cable to work the shield is D4 to D9, A0, +5V, GND and if you want access to reset button one more. I found usefulness this button maybe with one additional resistor this can be use in separate pin. The other five button they use single pin (A0) and with voltage divider it reads the button that pressed. 

To minimize the flickering (in my case) I add delay(10) to the Key_Grab demo sketch.

The library DFR_Key.h control the five buttons.



My arduino board

First of all I present you my board:

SainSmart UNO R3 ATmega328-AU ($11.10)

I don't recommended for non experience people because the controller chip is SMD and thus is very difficult to replace in case of failure. But in the other hand the free space on the board is filled with pins with all analog and digital in/out.

 


Editorial


Welcome to another blog about the famous arduino board. In this blog I will try to post my personal attempts experimenting with arduino together with varius sensors. I apologize for any spelling mistakes but my English is not in the best shape...