How much can you do with a ATTiny85?

I had tested a sketch for esp32 with a OLED display and two DS18B20 temperature sensors and had it working so I wanted to transfer it to the ATTiny85. Doing so I had to replace the SSD1306 with a smaller library SSD1306_minimal and the Wire library with TinyWireM. Even so, the executable came out over 100% so I took out all unneccessary code and I even had to comment out a few lines of code that I needed just to come down just under 100%, 99.6% for RAM and 32.0% for FLASH. It then worked but I hadn’t even begun calling the sensor functions and in this situation I will never be able to. I looked at PROGMEN to see if that could help but, moving code from SRAM to FLASH, it worked the other way than what I needed. Is there anything I can do or have I reached the limit of the ATTiny85?

Sketch:

#include <Arduino.h>
#include <SSD1306_minimal.h>
#include <TinyWireM.h> // I2C
#include <DallasTemperature.h>
#include <OneWire.h>

float tempC;
String Buff;
DeviceAddress devAddr[8];

SSD1306_Mini OLED;
OneWire oneWire(3); // OneWire bus = 3
DallasTemperature sensors(&oneWire);

void setup()
{
    //Serial.begin(115200);
    //delay(500);
    sensors.begin();
    sensors.begin();

    OLED.init(0x3C);
    OLED.startScreen();
    OLED.clear();
    OLED.printString((char*)"OLED setup");

     // Search for devices on the bus and assign based on index
    for (uint8_t i = 0; i < sensors.getDeviceCount(); i++)
    {
        if (!sensors.getAddress(devAddr[i], i))
        {
            OLED.cursorTo(0, 1);
            OLED.printString((char *)"Text1 " + i);
            //    Serial.println("Not found sensor " + i);
        }
        else
        {
            Buff = String(i + 1);
            OLED.cursorTo(0, 2);
        //    OLED.printString((char *)"Sensor ");
            OLED.printString((char *)&Buff);
        //    OLED.printString((char *)", Address: ");
        //    OLED.printString((char *)(devAddr[i]));
        }
    }

}

void loop()
{
}

Hmmm. Interesting.

One thing that will most likely save you space is to lose the string data type(s). They are classes with class overheads. It’s possible that a char * or char[nnn] will do.

Strings also do a lot of allocating and deallocating of scarce RAM and their use can lead to weird, difficult to trace, problems and/or crashes if the grab too much RAM.

Do you need to scan the bus for sensor addresses? Surely once a device is on the bus and scanned, you know its address? You could write code to scan the bus and note the addresses then hard code those addresses into your code above. That would allow you to omit the scanning code and save more (flash) RAM.

The most severe option would be to try to write the code in AVR flavoured C/C++ rather than using Arduino Speak as the Arduino code takes up a huge amount of space, for some commands, that what it actually needs. I’m thinking of pinMode, digitalRead, digitalWrite and so on. Not necessarily in your code, but possibly in the libraries in use.

HTH.

Cheers,
Norm.

The OLED library is probably allocating a lot of RAM for screen buffering in case of using the graphics. Try searching for ASCII (just text) OLED libraries.

Everything you described could be, in case of doing all from scratch using C, done with just 1 or 2 KB of FLASH and maybe 50 bytes of RAM (or even less) but it would take considerably more time (days instead of hours) than doing the same by using general purpose libraries.

Norman

Thanks for your reply. Write some of the code in pure C++ seems interesting so I’ve just purchased a book ‘Arduino Software Internals’ by modest writer. The thing to decide is what to write in C++ and what to leave Arduino but I suppose I will figure that out by the time I’ve finished the book.

As for my little sketch I had already found out the addresses so I call them now using the addresses and it now works as expected.

2 Likes

Hi Hans,

thank you for buying my book, it’s appreciated. Any questions, drop me a DM and I’ll give you my email address.

Another book you might also find useful is this one, which helped me a lot: Amazon.co.uk. (I’m not sure how much of that URL is strictly necessary! :wink:)

Cheers,
Norm.