ATtiny 85 with I2C

Hello, I have a ATtiny85 that I want to program (Via a Sparkfun Tiny AVR Programmer) using Arduino IDE so It can control an Adafruit VL53L0X sensor with I2C. If this is not possible, than with SPI? I have looked at many examples, but I'm not sure where to even begin. Any help would be greatly appreciated.

Thank You.

If you use my core, it should work like it does on an Uno - my core has a special version of Wire.h that selects the appropriate implementation based on the features of the hardware.

If using another core, you'll need to use something like TinyWireM, and modify the adafruit library to use TinyWireM instead of Wire.

How do I use your core? Also, keep in mind that the programmer is USBtinyISP.

TinyWireM should be able to do what you want. Here's a short demonstration sketch that hooks two I2C devices (DS3231 and a 4X 7-segment display) to an ATTiny85 chip. I programmed the tiny85 with the same programmer you have, the Sparkfun Tiny Programmer.

/***********************************************
  Minutes and seconds
  A demonstration using TinyWireM on an
  ATTiny 85 with I2C input and output devices

  Tiny 85, 8MHz internal
    Adafruit 7-segment, I2C, LED backpack display
    DS3231 breakout board
  d2 (physical pin 7) -> I2C clk
  d0 (physical pin 5) -> I2C sda

  042618 clh 1376/71 show minutes and seconds of the real time


************************************************/

#include "TinyWireM.h" // https://github.com/adafruit/TinyWireM
#include "Adafruit_GFX.h"
#include "Adafruit_LEDBackpack.h"

Adafruit_7segment matrix = Adafruit_7segment();

#define rtcAddress 0x68  // DS3231

unsigned long previousSecondMillis = millis();
long oneSecond = 1000L;

boolean isColonOn = true;

void setup() {
  TinyWireM.begin();
  matrix.begin(0x70);
}

void loop() {

  // --------- Run this code every second ------------------
  if (millis() - previousSecondMillis >= oneSecond) {
    previousSecondMillis += oneSecond;

    // get data (in bcd format) directly from the rtc
    TinyWireM.beginTransmission(rtcAddress); // 0x68 is DS3231 device address
    TinyWireM.write((byte)0); // start at register 0
    TinyWireM.endTransmission();
    TinyWireM.requestFrom(rtcAddress, 2); // request just seconds and minutes

    while (TinyWireM.available())
    {
      byte seconds = TinyWireM.read(); // get seconds
      byte minutes = TinyWireM.read(); // get minutes

      // convert BCD to decimal
      minutes = (((minutes & 0b11110000) >> 4) * 10 + (minutes & 0b00001111));
      seconds = (((seconds & 0b11110000) >> 4) * 10 + (seconds & 0b00001111));

      // write to the display via I2C using a library
      // show the minuts:seconds on 7 segment display
      matrix.writeDigitNum(0, (minutes / 10));
      matrix.writeDigitNum(1, (minutes % 10));
      matrix.writeDigitNum(3, (seconds / 10));
      matrix.writeDigitNum(4, (seconds % 10));
      matrix.drawColon(isColonOn);
      matrix.writeDisplay();
      
      isColonOn = !isColonOn;  // flash the colon on and off
    }
  }
  
}

Here's a picture showing the hookup:

This should let you get going with the stuff you have now.

Tbot1000:
How do I use your core? Also, keep in mind that the programmer is USBtinyISP.

Install per instructions from github

Make sure you've selected a board definition corresponding to my core. Then you can use wire and existing libraries for i2c devices instead of having to use TinyWireM and do everything without libraries like shown in ChrisTenone's post

DrAzzy:
If you use my core, it should work like it does on an Uno - my core has a special version of Wire.h that selects the appropriate implementation based on the features of the hardware.

If using another core, you'll need to use something like TinyWireM, and modify the adafruit library to use TinyWireM instead of Wire.

Hi Spence,
Nice to meet you,
Would like to ask if using an Digispark ATTiny85 development board (which has microusb), and thought for something called Micronucleus and vusb to upload programs directly from the IDE, I could use your Core.
The board all that has is the AVR, an 78L05, some passive components, and the usb.
Thanks,
Best regards,

My core is not compatible with digispark boards, sorry. Only straight ATTiny device, or via serial bootloader for the 441/841/828/1634/87/167 (addition of serial bootloaders for the other ones is planned for the next release.

I do not currently have any plans to support a USB bootloader - but I've been getting so many inquiries that I'm wondering if I should try to pull in the digispark bootloader and board definitions - I don't have the understanding of USB to adapt it to other supported attinies though.

DrAzzy:
My core is not compatible with digispark boards, sorry. Only straight ATTiny device, or via serial bootloader for the 441/841/828/1634/87/167 (addition of serial bootloaders for the other ones is planned for the next release.

I do not currently have any plans to support a USB bootloader - but I've been getting so many inquiries that I'm wondering if I should try to pull in the digispark bootloader and board definitions - I don't have the understanding of USB to adapt it to other supported attinies though.

Thanks for your response.

May I henceforth use an Arduino Uno as ISP programmer for the core, putting aside the software usb thing in this board?
I will need to burn one of the core's bootloaders first? (putting aside the Micronucleus for some time) which one?
More todos?
Thanks.

Yeah, it will work fine if you program them via ISP - just no support for their USB thing.

You probably won't even need to do burn bootloader with my core; the first time you program via ISP, it will erase the bootloader, and there aren't any fuses specific to their bootloader, since there's no hardware bootloader support on the tiny85

DrAzzy:
Yeah, it will work fine if you program them via ISP - just no support for their USB thing.

You probably won't even need to do burn bootloader with my core; the first time you program via ISP, it will erase the bootloader, and there aren't any fuses specific to their bootloader, since there's no hardware bootloader support on the tiny85

Then it´s my turn now.
I ported my program that was on TinywireS to your nested Wire.h and compiles ok with your core, I would like to have Software serial also and have a console on such a tiny amazing chip also..
Hope I can upload now..
Will ask if I get stuck,
Thanks again

Hi,
As for the built-in serial support, how does it work?
Do I have to include a lib?
May I use a console for debugging and do Serial.print()?
Thanks in advance,
Regards

What settings do I need to use for the ATtiny Core?
I know I need:

Board: ATtiny 25/45/85

But what about all the other settings?

I get this error for my code:
(code):

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();
int pin = 3;
void setup() {
  pinMode(pin, OUTPUT);
  //Serial.begin(115200);

  // wait until serial port opens for native USB devices
  //while (! Serial) {
  //  delay(1);
  //}
  
  //Serial.println("Adafruit VL53L0X test");
  //if (!lox.begin()) {
  //  Serial.println(F("Failed to boot VL53L0X"));
   // while(1);
  //}
  // power 
  //Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}


void loop() {
  VL53L0X_RangingMeasurementData_t measure;
    
  //Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
  if (measure.RangeMilliMeter < 50){
    digitalWrite(pin, HIGH);
    delay(1);
    digitalWrite(pin,LOW);
  }
  //if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    //Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  //} //else {
    //Serial.println(" out of range ");
  //}
    
  delay(1);
}

But this error:

~~~
**Arduino: 1.8.5 (Windows 8.1), Board: "ATtiny25/45/85, Disabled, CPU, ATtiny85, 1 MHz (internal), EEPROM retained, B.O.D. Disabled"

Build options changed, rebuilding all
Archiving built core (caching) in: C:\Users\Tiernan\AppData\Local\Temp\arduino_cache_140639\core\core_ATTinyCore_avr_attinyx5_LTO_disable,TimerClockSource_default,chip_85,clock_1internal,eesave_aenable,bod_disable_b1aba50921474f5ec00c001d6108c665.a
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: C:\Users\Tiernan\AppData\Local\Temp\arduino_build_645164/TinyDISTANCE.ino.elf section .text' will not fit in region text'

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ld.exe: region `text' overflowed by 3586 bytes

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board ATtiny25/45/85.
Invalid library found in C:\Users\Tiernan\Documents\Arduino\libraries\usbtinyisp_libusb_1.2.6.0: C:\Users\Tiernan\Documents\Arduino\libraries\usbtinyisp_libusb_1.2.6.0
Invalid library found in C:\Users\Tiernan\Documents\Arduino\libraries\usbtinyisp_libusb_1.2.6.0: C:\Users\Tiernan\Documents\Arduino\libraries\usbtinyisp_libusb_1.2.6.0

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.**
~~~

How big does the code compile to for an uno?

Could it be that the library is bloated and the sketch is just too big?

apiffa:
Then it´s my turn now.
...

Hi apiffa, you should probably start your own thread rather thanhijack Tbot's thread.

Tbot1000:
...I get this error for my code:...

Hi again Tbot1000, perhaps try running your program on a Uno or other ATMega328 board, just to make sure your code works. Programs usually compile smaller on a Tiny, but remember they only have 8K of flash, and ... 512 bytes? of ram. If it compiles on a 328, but takes up 10K or more, it is unlikely you will be able to compile it on the tiny85.

Tbot1000:
What settings do I need to use for the ATtiny Core? ...

Select the 85, 8 MHz internal, and burn the bootloader prior to programming the chip the first time.