Raspberry Pi ATTiny85 I2C Interfacing

Discussion about other hardware (including other Pi-like boards)
User avatar
bouke
Posts: 18
Joined: Sun Jun 18, 2017 8:28 am
Location: Enschede, The Netherlands
Has thanked: 1 time
Been thanked: 7 times
Contact:

Raspberry Pi ATTiny85 I2C Interfacing

Post by bouke » Thu Jul 13, 2017 6:34 am

So I was thinking about using an ATTiny85 to handle shutdown of the RaspberryPi and battery monitoring by communicating via I2C. I made a small Fritzing Sketch to illustrate what I want to do: ATTiny85 and RaspberryPi are connected using SDA/SCL, ATTiny85 is connected to a NeoPixel which is used as a battery status LED, furthermore a switch is connected to the ATTiny85.

There are a few issues however:
  • I would very much like to only use one switch, instead of a switch and separate pushbutton. However using this configuration the ATTiny85 and RaspberryPi (in low power halt state) will always be powered therefore draw power (is this a problem?).
  • How do I wake the RaspberryPi from the lower power mode? I've read somewhere that providing a signal to the SCL port can wake the Pi but I have no luck with this myself.
  • ATTiny85 has limited flash memory, programming everything in such a small package will be a challenge.
Is someone else currently working on this and possibly have feedback? I did see a Github Repo from Camble who is also working on this (https://github.com/Camble/RPi-I2C).

Image
Fritzing Sketch of the proposed setup.

Edit 16/07/2016:
Things I have learnt so far:

It is entirely possible and not that hard to get going with I2C communication between the Pi Zero and ATTiny85 (https://github.com/regnerus/Safe-Shutdo ... controller) however there are some limitations.
  • It is possible to turn the Raspberry Pi in to a halted state where it consumes little power and power could be completely shutoff using a MOSFET, however using only I2C communication it is not possible to reliably know when the Pi has entered this halted state (for this you need one extra GPIO pin which is high when the Pi is on and low when it is turned off.
  • It is not possible to use I2C to turn the Raspberry Pi back on, to achieve this you will need another GPIO pin.
  • Although the flash memory will probably not prove to be a limit the amount of pins available on the ATTiny85 will be an issue. Currently looking at alternatives with more pins such as the ATTiny84 or ATTiny167.
Last edited by bouke on Sun Jul 16, 2017 7:13 am, edited 4 times in total.
Currently figuring out how to build my first Game Boy Zero. 8-)

User avatar
abrugsch
Posts: 926
Joined: Tue Aug 02, 2016 10:00 am
Has thanked: 344 times
Been thanked: 408 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by abrugsch » Thu Jul 13, 2017 8:25 am

1)even in low power "off" state it's still drawing some power, therefore will drain a battery.
2) sorry, no idea
3) the ATTiny is small, but the program you're talking about would easily fit. I used a tiny85 for driving neopixels with a fairly complicated bit of code. even with the bigger of the bootloaders available you'll still have some reasonable space in there. (don't forget, compiled code is MUCH smaller than the .ino or .c files you start with.)

User avatar
bouke
Posts: 18
Joined: Sun Jun 18, 2017 8:28 am
Location: Enschede, The Netherlands
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by bouke » Thu Jul 13, 2017 9:39 am

abrugsch wrote:
Thu Jul 13, 2017 8:25 am
1)even in low power "off" state it's still drawing some power, therefore will drain a battery.
Thanks for the reply, I know the RPi and ATTiny85 will draw power, I was kind of wondering if people have done it before and have reasons to do- or not do it... Or possibly a way to really cut off the power.

Just measured the halted power draw from just the Raspberry Pi Zero and seems to draw about 22mA therefore it should comfortably be able to stay in this halted mode for about a month (assuming my 3800mAh battery).
Currently figuring out how to build my first Game Boy Zero. 8-)

User avatar
moosepr
Posts: 448
Joined: Wed Jan 25, 2017 6:18 am
Has thanked: 58 times
Been thanked: 187 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by moosepr » Thu Jul 13, 2017 3:09 pm

I was thinking of doing the same thing! You can get round the first few issues buy using a MOSFET. This is basically an electronic switch so the attiny will be able to cut the power to the pi at will, then the tiny can go into sleep mode which will use minimal power while being able to be woken by a button. The problem I had was that the tiny would be running from battery voltage in order to measure the batter voltage with no external pins or components, but that then means the tiny could be up to 1v higher than the 3.3v of the pi

Take a look at this
https://www.dfrobot.com/wiki/index.php/ ... U:_DFR0343

It's what I started using on my minimal pi project, but never got further than on and off
Get a small cheep LCD in your project https://www.sudomod.com/forum/viewtopic.php?f=22&t=2312
Wrap it all round a battery https://www.sudomod.com/forum/viewtopic.php?f=13&t=2292
Or use a custom PCB to make it really small https://www.sudomod.com/forum/viewtopic.php?f=13&t=2512
or make it really really really really tiny!! https://www.sudomod.com/forum/viewtopic.php?f=13&t=2919

User avatar
bouke
Posts: 18
Joined: Sun Jun 18, 2017 8:28 am
Location: Enschede, The Netherlands
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by bouke » Fri Jul 14, 2017 2:46 am

moosepr wrote:
Thu Jul 13, 2017 3:09 pm
You can get round the first few issues buy using a MOSFET.
Thanks, stupid that I did not think about just keeping the ATTiny85 instead of both... I still have some MOSFETs laying around will try that soon! :) Have to figure out how to do that with the limited pins on the ATTiny85 though, I'm already using all of them...
moosepr wrote:
Thu Jul 13, 2017 3:09 pm
The problem I had was that the tiny would be running from battery voltage in order to measure the batter voltage with no external pins or components, but that then means the tiny could be up to 1v higher than the 3.3v of the pi
I was just planning on running the ATTiny at 5v from my voltage converter and then measure the voltage using a voltage divider (and some support components such as a Zener diode and capacitor) to get the voltage between 0 and 5v, in my case the voltage can go up to 16.8v. This is illustrated in the Fritzing sketch by the yellow wire (LiPo Voltage).
Currently figuring out how to build my first Game Boy Zero. 8-)

User avatar
moosepr
Posts: 448
Joined: Wed Jan 25, 2017 6:18 am
Has thanked: 58 times
Been thanked: 187 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by moosepr » Fri Jul 14, 2017 4:17 am

wont you need to drop the attiny to 3.3v? i think the pi gpio runs at 3.3v
Get a small cheep LCD in your project https://www.sudomod.com/forum/viewtopic.php?f=22&t=2312
Wrap it all round a battery https://www.sudomod.com/forum/viewtopic.php?f=13&t=2292
Or use a custom PCB to make it really small https://www.sudomod.com/forum/viewtopic.php?f=13&t=2512
or make it really really really really tiny!! https://www.sudomod.com/forum/viewtopic.php?f=13&t=2919

User avatar
bouke
Posts: 18
Joined: Sun Jun 18, 2017 8:28 am
Location: Enschede, The Netherlands
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by bouke » Fri Jul 14, 2017 7:19 am

moosepr wrote:
Fri Jul 14, 2017 4:17 am
wont you need to drop the attiny to 3.3v? i think the pi gpio runs at 3.3v
It is possible to use a level converter for the I2C communication, however since the Raspberry Pi is running as "master" and the ATTiny85 is running as "slave" this should not be needed. Reason for this is that the Raspberry Pi has pull-up resistors on its I2C connectors pulling them to 3.3v. To transmit data on the bus, a device brings the bus low by connecting it to ground. Meaning that a slave device (the ATTiny85) should never drive the bus high (to 5V).

EDIT:

Found a source which confirms what I just mentioned: https://area-51.blog/2014/02/15/connect ... using-i2c/.
petermount1 wrote:Now the trick, Vdd is only there to pull those signals up and in I2C a 1 is when the signal is pulled down to 0V. It’s not there to power the devices so, as long as we keep Vdd at 3v3 and no device has a pull up resistor on them (i.e. to 5V) then we are save to connect it to the PI. There’s only a problem if any device on the I2C bus also has a pull up resistor.

Now do the Arduino’s have pullup resisitors? Well they actually don’t, they actually cannot as the I2C interface is shared by two of the analogue inputs (4 & 5 to be precise) so there cannot be a resistor there else it would affect those pins when not being used for I2C.

So, we have a solution as long as the Raspberry PI is the I2C Master which is what we want. Also, of the available GPIO pins, only SDA and SCL have pull up resistors, so we are set.
Currently figuring out how to build my first Game Boy Zero. 8-)

User avatar
moosepr
Posts: 448
Joined: Wed Jan 25, 2017 6:18 am
Has thanked: 58 times
Been thanked: 187 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by moosepr » Fri Jul 14, 2017 8:09 am

awesome!! that actually solves my issue then!!

the tiny can actually assess its vIn and measure it to a reasonable accuracy (i think)
https://code.google.com/archive/p/tinke ... meter.wiki

so i technically have an extra pin spare to use on the mosfet

what kind of communication protocol were you going for?

i was planning on having the pi ping for a voltage reading every few seconds, in normal use the tiny will return the voltage so the pi could technically do something with it (maybe even OSD battery?)

the tiny can return a status if the button has been pressed to request shutdown, so at the next ping, if the pi gets the shutdown flag instead of a voltage, it can trigger a clean shutdown

the pi could set a shutdown flag rather than requesting the voltage so the tiny knows things are going down (led colour can change)

if the tiny has had no ping within a set time it would just assume the pi has gone off

on power on, the tiny would just wait for a ping from the pi, then it can know we are in booting mode (led colour can change)
Get a small cheep LCD in your project https://www.sudomod.com/forum/viewtopic.php?f=22&t=2312
Wrap it all round a battery https://www.sudomod.com/forum/viewtopic.php?f=13&t=2292
Or use a custom PCB to make it really small https://www.sudomod.com/forum/viewtopic.php?f=13&t=2512
or make it really really really really tiny!! https://www.sudomod.com/forum/viewtopic.php?f=13&t=2919

User avatar
bouke
Posts: 18
Joined: Sun Jun 18, 2017 8:28 am
Location: Enschede, The Netherlands
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by bouke » Fri Jul 14, 2017 8:32 am

moosepr wrote:
Fri Jul 14, 2017 8:09 am
awesome!! that actually solves my issue then!!
Awesome!
moosepr wrote:
Fri Jul 14, 2017 8:09 am
the tiny can actually assess its vIn and measure it to a reasonable accuracy (i think)
https://code.google.com/archive/p/tinke ... meter.wiki
On the website it mentions the following: "This works on Arduinos with a 328 or 168 only." but does it also work on the ATTiny85? If it can actually do that it would be awesome and then I will definitely experiment with it...
moosepr wrote:
Fri Jul 14, 2017 8:09 am
what kind of communication protocol were you going for?
Similar to yours I think, I like the I2C communication since you can get access to the battery percentage using the raw voltage data indeed possibly for OSD battery. In theory it would also mean that I can save settings such as: timeout of the shutdown, LED colours etc. on the PI but I am not sure yet how far I want to take this.
Currently figuring out how to build my first Game Boy Zero. 8-)

User avatar
kite
Posts: 1109
Joined: Thu May 12, 2016 4:30 am
Location: Bristol, UK
Has thanked: 247 times
Been thanked: 1735 times
Contact:

Re: Raspberry Pi ATTiny85 I2C Interfacing

Post by kite » Fri Jul 14, 2017 8:34 am

moosepr wrote:
Fri Jul 14, 2017 8:09 am
awesome!! that actually solves my issue then!!

the tiny can actually assess its vIn and measure it to a reasonable accuracy (i think)
https://code.google.com/archive/p/tinke ... meter.wiki

so i technically have an extra pin spare to use on the mosfet

what kind of communication protocol were you going for?

i was planning on having the pi ping for a voltage reading every few seconds, in normal use the tiny will return the voltage so the pi could technically do something with it (maybe even OSD battery?)

the tiny can return a status if the button has been pressed to request shutdown, so at the next ping, if the pi gets the shutdown flag instead of a voltage, it can trigger a clean shutdown

the pi could set a shutdown flag rather than requesting the voltage so the tiny knows things are going down (led colour can change)

if the tiny has had no ping within a set time it would just assume the pi has gone off

on power on, the tiny would just wait for a ping from the pi, then it can know we are in booting mode (led colour can change)
If you can spare the 1x extra pin, I'd do something simpler like I do on mine where when the Pi is ON it has a pin held high, and when off it is low

Code: Select all

# Enable safe shutdown
dtoverlay=gpio-poweroff,gpiopin=22,active_low="y"
This means that the ATTINY can detect when the Pi is on (the pin is now high) and when the shutdown is completed it knows that it is off (pin is low) .. suggest to put a weak pull down on it.

If used with a resistor (pi end) you can even have a push button to pull that line to GND which the attiny will detect as 'pi has shut down' and so will kill the power! In order to do all this you will probably have to enable use of the 'reset' pin, which does make programming it more difficult but hey an extra pin :) I've done all this myself on an even smaller ATTINY10 and it works really well (no I2C though)
CURRENT PRE-ORDER: CLOSED: viewtopic.php?f=38&t=4351
Kite's SUPER AIO Support Thread: viewtopic.php?f=25&t=2508
Kite's PRE-ORDER SIGN UP NOTIFICATION: https://goo.gl/forms/e97uUvPOfUxPWdz82
Kite's Next Project Hardware Survey: https://goo.gl/forms/mUZYg7hAA8XqFaTk2

Post Reply

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests