Library problem in taking Digispark sketch to bare Attiny85

Hi all!

The sketch below works fine on a Attiny85 based Digispark board:

#include <DigisparkOLED.h>
#include <Wire.h>
#include <TinyDHT.h>
#define DHTTYPE DHT11
#define DHTPIN 1
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  //Inicializa o display
  oled.begin();
  dht.begin();
  oled.setFont(FONT8X16);
}

void loop() {

  //Limpa o display
  oled.clear();

  int8_t h = dht.readHumidity();
  int16_t t = dht.readTemperature(0);

  if (t == BAD_TEMP || h == BAD_HUM) {
    oled.clear();
    oled.setCursor(10, 0);
    oled.print(F("Erro sensor!"));
    delay(2000);
  } 
  
  else {
    //Posiciona o cursor
    //oled.setCursor(X em pixels, Y em linhas de 8 pixels comecando com 0);
    oled.setCursor(10, 0);
    //Seleciona fonte 8x16
    oled.print(F("TEMPERATURA"));
    oled.setCursor(10, 5);
    oled.print(t);
    oled.print(" C");
    delay(3000);
    oled.clear();
    oled.setCursor(10, 0);
    oled.print(F("UMIDADE"));
    oled.setCursor(10, 5);
    oled.print(h);
    oled.print(" %");
    delay(3000);
  }
}

I would like to take it to a bare Attiny85 chip, but I´m facing problems with the libraries.
I´m switching:

#include <DigisparkOLED.h>
#include <Wire.h>

to

#include <Tiny4kOLED.h>
#include <TinyWireM.h>

but then I get a bunch of errors that seems to be related to the Wire library (?)

C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:505:10: error: 'TWS4' undeclared (first use in this function); did you mean 'TWS5'?
   switch(TW_STATUS){
          ^
C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:505:10: error: 'TWS3' undeclared (first use in this function); did you mean 'TWS4'?
   switch(TW_STATUS){
          ^
C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:510:7: error: 'TWDR' undeclared (first use in this function); did you mean 'TWSR'?
       TWDR = twi_slarw;
       ^~~~
       TWSR
C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:530:10: error: 'TWCR' undeclared (first use in this function); did you mean 'TWDR'?
          TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
          ^~~~
          TWDR
In file included from c:\users\noteantigo\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\io.h:99:0,
                 from C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:26:
C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:530:21: error: 'TWINT' undeclared (first use in this function)
          TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
                     ^
C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:530:34: error: 'TWSTA' undeclared (first use in this function); did you mean 'TWS3'?
          TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
                                  ^
C:\Users\NOTEANTIGO\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility\twi.c:530:46: error: 'TWEN' undeclared (first use in this function); did you mean 'TWINT'?
          TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
                                              ^

exit status 1

Compilation error: exit status 1

Any hints?
Thanks in advance!

Have you tried updating the libraries? I do not see twi.c called in any of the library files.

I have seen @Juraj weigh-in with twi.c expertise.

The IDE Library Manager says it´s all up to date.

That´s what makes things weird. Looks like the compiler uses the original Wire.h library instead of the TinyWireM one.

Would you mind sharing the entire sketch that gives you the compile errors?

Here it goes:

#include <Tiny4kOLED.h>
#include <TinyWireM.h>
#include <TinyDHT.h>
#define DHTTYPE DHT11
#define DHTPIN 3

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  oled.begin(); //Inicializa o display
  dht.begin();
  oled.setFont(FONT8X16); //Seleciona fonte 8x16
}

void loop() {

  //Limpa o display
  oled.clear();

  int8_t h = dht.readHumidity();
  int16_t t = dht.readTemperature(0);

  if (t == BAD_TEMP || h == BAD_HUM) {
    oled.clear();
    oled.setCursor(10, 0);
    oled.print(F("Erro sensor!"));
    delay(2000);
  } 
  
  else {
    //Posiciona o cursor
    //oled.setCursor(X em pixels, Y em linhas de 8 pixels comecando com 0);
    oled.setCursor(10, 0);
    oled.print(F("TEMPERATURA"));
    oled.setCursor(10, 5);
    oled.print(t);
    oled.print(" C");
    delay(3000);
    oled.clear();
    oled.setCursor(10, 0);
    oled.print(F("UMIDADE"));
    oled.setCursor(10, 5);
    oled.print(h);
    oled.print(" %");
    delay(3000);
  }
}

May be kind of unbelievable, but visiting this example:

https://github.com/datacute/Tiny4kOLED/blob/master/examples/DoubleSizeText/DoubleSizeText.ino

I read:

// Choose your I2C implementation before including Tiny4kOLED.h
// The default is selected is Wire.h

I then tried switching the order of the libraries and including TinyWire before Tiny4kOLED.h.

I got no more compiling errors now :exploding_head:

will try to upload the code and see what happens.

1 Like

For future reference, this is the working code:

#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <TinyDHT.h>

#define DHTTYPE DHT11
#define DHTPIN 1

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  TinyWireM.begin();
  dht.begin();
  oled.begin(128, 64, sizeof(tiny4koled_init_128x64r), tiny4koled_init_128x64r); //Inicializa o display
  oled.clear(); //Limpa o display
  oled.on();
  oled.setFont(FONT6X8); //Seleciona fonte 8x16
}

void loop() {
  delay(2000);
  int8_t h = dht.readHumidity();
  int16_t t = dht.readTemperature(0);

  if (t == BAD_TEMP || h == BAD_HUM) {
    oled.clear();
    oled.setCursor(10, 0);
    oled.print("Erro sensor!");
    delay(2000);
  } 
  
  else {
    //Posiciona o cursor
    //oled.setCursor(X em pixels, Y em linhas de 8 pixels comecando com 0);
    oled.clear();
    oled.setCursor(10, 1);
    oled.setFont(FONT6X8);
    oled.print("TEMPERATURA");
    oled.setCursor(10, 5);
    oled.setFont(FONT8X16);
    oled.print(t);
    oled.print(" C");
    delay(3000);
    oled.clear();
    oled.setCursor(10, 1);
    oled.setFont(FONT6X8);
    oled.print("UMIDADE");
    oled.setCursor(10, 5);
    oled.setFont(FONT8X16);
    oled.print(h);
    oled.print(" %");
    delay(1000);
  }
}

DHT11 returned errors at 1MHz and only zeroes at 8Mhz. It worked well at 5V 16Mhz.
There´s an old version of Rob Tillart DHT library that is supposed to work at 8Mhz. I didn´t try it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.