geoffr
Posts: 85
Joined: Wed Aug 22, 2012 11:25 am
Location: Melbourne, VIC

Communication with ATTiny85

Fri Dec 16, 2016 12:41 am

I am busy tinkering around at the moment with using an ATTiny85 microcontroller to provide an analogue input to a Pi (the ultimate goal is to built a water level sensor for my rainwater tanks using a differential pressure sensor).

I am currently using I2C, but have been running into the issue that the Pi does not support I2C clock stretching, with the result that I am getting data corruption in some cases. What I have found is that what generally occurs is that bit 7 of the value I am reading gets zeroed. Given that I am reading a 10 bit ADC, a hack which greatly improves the quality of the data I get back is to split the way I store the value read from the sensor into 2 5 bit quantities rather than 8 and 2 - i.e. I can ignore bits 5, 6 and 7. (I am running the ATTiny at 8MHz - I haven't tried 16MHz yet as that will require running it at 5V, which will require level shifters on my I2C bus - I need to dig out some MOSFETs.)

What I was wondering was if anyone has a readily implementable better solution, especially an easily usable implementation of a SPI slave using USI. One of the reasons I haven't tried SPI yet is that I have been struggling to find an implementation that looks reasonably straight forward to use.

Any advice or suggestions would be appreciated.

Thanks,
Geoff.

User avatar
Douglas6
Posts: 4742
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Communication with ATTiny85

Fri Dec 16, 2016 12:52 am

I did a small test with I2C on an ATTiny85 some years ago. Some tips:
1. Lower the I2C speed.
2. Perform as little as possible in the I2C callback on the Tiny. I sampled the sensor in the main loop and stored it, so the callback only sent a value.
3. On the Pi side, catch any errors and handle them, perhaps returning the last good value.

Here's the code I used: viewtopic.php?p=494061#p494061

I never did find an SPI library for the Tiny.

geoffr
Posts: 85
Joined: Wed Aug 22, 2012 11:25 am
Location: Melbourne, VIC

Re: Communication with ATTiny85

Fri Dec 16, 2016 3:04 am

Douglas6 wrote:I did a small test with I2C on an ATTiny85 some years ago. Some tips:
1. Lower the I2C speed.
2. Perform as little as possible in the I2C callback on the Tiny. I sampled the sensor in the main loop and stored it, so the callback only sent a value.
3. On the Pi side, catch any errors and handle them, perhaps returning the last good value.

Here's the code I used: viewtopic.php?p=494061#p494061

I never did find an SPI library for the Tiny.
That is essentially the approach I am following. I'm now also adding a checksum to the data I transmit, so that I can re-request it if the checksum fails.

Return to “Interfacing (DSI, CSI, I2C, etc.)”