ATTiny memory and libraries

I want to use an ATTiny85 for a simple project, where small size is important. My project is a temperature sensor/display for my bicycle handlebars. I have used other boards, including a XIAO, to accomplish this and now want something even smaller.

Before I get deeply into it, I have a basic feasibility question: The size of the libraries for the OLED display, the temp sensor, and the I2C protocol would seem to be much too large for the small amount of ATTiny memory. Do those entire libraries get loaded with the #include commands? Or maybe the compiled sketch is much smaller than the code files themselves?

I'm pretty new to this, and I'm hoping someone can educate me a bit. Thanks.

It sounds like you are confusing the size of the source files with the size of the compiled binary file.

Having said that, you will likely find that you have to use ATtiny specific libraries (or roll your own) due to the memory constraints you have already discovered.

Easiest way to find out is to select your particular ATtiny device and try and compile a sketch that does more or less what you want. You'll find out in a hurry just how much over you are, if at all.

Not if the libraries are properly written. Only the object code for the functions actually called will be loaded into program memory.

Not all libraries are properly written.

This will usually be the case.

But if you're going from a XIAO (at least a SAMD21 with 256k program space and 32k of RAM) to an tiny85, you losing a LOT of memory and power, and could easily have problems.

You can certainly take your sketch and compile it with the standard "Uno R3" as a board type; that is also an AVR, so the resulting binary size should be very similar to what you'd see for a tiny85.

I have actually done something like this a few years ago! Using an ATTiny85 to just display temp when it is powered on. Made a small portable temp sensor for getting temp readings from my hottub or pool. Even using it for my fish tank. Here is the code that I made long ago.

 /*  Runnig on ATTiny85 @ Internal 8Mhz
             ā”Œ ─ ─ ─ ─ ─ ┐
     Reset   |1   ()    8|   5V
     A3  3   |2         7|  2  A1
     A2  4   |3         6|  1  PWM1
       GND   |4         5|  0  PWM0
             ā”” ─ ─ ─ ─ ─ ā”˜

 * Physical pin 2 (3) [A3]
 * Physical pin 3 (4) [A2]
 * Physical pin 5 (0) [PWM0]
 * Physical pin 6 (1) [PWM1]
 * Physical pin 7 (2) [A1]
 */
#include <TinyWireM.h> 
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3 //Physical pin 2 (A3)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
/*
 How many bits to use for temperature values: 9, 10, 11 or 12
   9 bits: increments of 0.5C, 93.75ms to measure temperature
   10 bits: increments of 0.25C, 187.5ms to measure temperature
   11 bits: increments of 0.125C, 375ms to measure temperature
   12 bits: increments of 0.0625C, 750ms to measure temperature
*/
#define SENSOR_RESOLUTION 12
DeviceAddress sensorDeviceAddress;


#include <Tiny4kOLED.h>
#include "font16x32digits.h"
const DCfont *currentFont = FONT16X32DIGITS;

void setup() {
  TinyWireM.begin(); 
sensors.begin();
  sensors.getAddress(sensorDeviceAddress, 0);
  sensors.setResolution(sensorDeviceAddress, SENSOR_RESOLUTION);
  sensors.requestTemperatures();
oled.begin();
//oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
oled.setRotation(1);     // Screen orientation 
  oled.setFont(FONT16X32DIGITS); // 1 row of 8 characters exactly fills 128x32
  
  oled.clear();
 oled.print(F("TEMP SENS"));

 oled.on();
 oled.switchRenderFrame();
 
   // Position the cusror
  // usage: oled.setCursor(X IN PIXELS, Y IN ROWS OF 8 PIXELS STARTING WITH 0);
  oled.setCursor(12, 0);

 oled.print(F(".SENSOR."));
  oled.switchRenderFrame();

 
} // END SETUP

void loop() {
  static int lastchk;
  if (sensors.getDS18Count() != 0) {
    sensors.requestTemperatures();
    oled.clear();
    oled.setCursor(25, 0);
    oled.print(sensors.getTempFByIndex(0));
    oled.switchRenderFrame();
  }

    
} // END LOOP

@jremington Your replies always hit me at exactly the level I need. Thanks.

@syntax1269 Thanks for sharing this code. Were you using the SSD1306 OLEDs?

Thank you all for your explanations. I understand a lot better now. My next issue will be which libraries I need in order to use the AHT20 temp/humidity sensor and the SSD1306 OLED display. I'll try the regular ones first. Also, how to set up the I2C. I have seen a reference to Tinywirem.h - maybe that is the one I need.
I'll let you all know how it goes. I'm very appreciative that you people spend your time helping noobs like me. Thanks again.

I would recommend this Arduino board/core package:

It includes a Wire library in the package, so you don't need to install any other tiny-wire libraries.

What size is your SSD1306 in pixels?

A 128x64 display will consume 1K RAM if you use the Adafruit library, or 0.5K for 128x32. ATtiny85 has only 0.5K, and you can't use all of it for the display.

The U8G2 library can use less RAM, especially if using it's U8X8 character-only mode. I've never tried it with ATtiny85.

The Tiny4KOLED library works very well with Attiny85 if you use a SSD1306 OLED.

I'm using a 64x32 SSD1306 (cute little thing).

Besides OLED and AHT20, are you going to connect any other devices with ATtiny85?

Not for this project.

Now, you can nicely develop the prototype of your project using the following ATtiny85 Dev Board.

or

I have tested sometimes ago, the Library Works well with ATtiny85.

The attached file contains few tested examples using ATiny85 Dev Board.
ATtiny85OLEDStopWatch.pdf (642.1 KB)

Have look at this website.

Plenty of examples of Attiny85 and other micros.

http://www.technoblogy.com/

Tiny4kOLED library works great! I also used TinyWireM. One of Tiny4kOLED example sketches displays to my 64x32 SSD1306 OLED just fine. The sketch uses ~3k of program memory (out of 8k).

Next step is to read the AHT20 and display the data (and still be within the memory limit).

How are you developing your Project -- using ATtiny85 Dev Board or Arduino as ISP?

Yes, using the small SSD1306 OLED display. my little temp sensor compiled, Sketch uses 7560 bytes (92%) of program storage space. Maximum is 8192 bytes.
Global variables use 143 bytes (27%) of dynamic memory, leaving 369 bytes for local variables. Maximum is 512 bytes.

So you do have some more space to add some other code to it. Have it running on a 9v battery, then using a voltage regulator to bring it down to 5v. Looking back at this old code there is room for improvement for sure. As when I made it, I was just starting to dive into AVR programing. But good luck in your project!

Are you using ATtiny85 Dev Board or Arduino as ISP? Which temp senosr have you used?

Arduino as ISP.