I'm trying to use the TinyWire library for I2C communication between a master ATTiny85 and a slave ATTiny85. Either master or slave as the sender is fine, but I have not been able to get a basic transmission working. I am using the Arduino IDE to program the ATTinys. The code I am testing should turn on the LED if the communication is a success. Please advise me. Thanks!
Just a wild guess, but here, you immediately check the response from the slave, while in reality it may need some time to actually send it. What if you change that 2nd line I quoted above with a small while-loop with a timeout so that you check for e.g. 10-50ms if there's a response from the slave? (The 10-50ms is rather arbitrary and also rather long; I'd expect the actual response time would be less than 2 ms, but you could empirically determine what works well).
I've actually found another library that does the job exactly how I want it. It's called Manchester, and you can download it here. I wasn't able to get the TinyWire library working, but at least I've found a work-around that works for what I want to achieve!
This is the code I used for a basic test. The transmitter ATTiny85 collects the button information and sends it to the receiver ATTiny85, which turns on the LED based on if the button is pressed or not.
You're missing the pullups on SCL and SDA in the first one (though for simple applications. You can't rely on internal pullups working for I2C on parts with a USI** instead of a proper TWI (nor should you EVER do so, even on ATmega parts where it "works"*).
Also, I haven't tested it, but if you can't just use Wire.h and the master reader slave writer examples with ATTinyCore, that is a bug and should be reported as such. We have better features Back like 3-4 years ago I added "universal" Wire.h and SPI.h libraries to ATTinyCore that pick the appropriate implementation for the hardware (TWI, USI, or - in the case of i2c master on the 441/841 and 828, a software implementation., so they can all use the same libraries]
Receiving data on a master with Wire.h (all versions)idi s busy wait loop that periodically toghghles the data direction on the pin while it's set LOW. but that triggers the bit counter which
*I intentionally disable that by default on the version of Wire that comes with the two cores I maintain for the modern AVR parts (where it would "work" fine, underimple conditions, and then vfail in a sligghtly different dsituation... more stray capacitance,m klong3r wores ,more parts on the ,pdfiu;les that do weird things. T+So you cand ground unlike the classic tinies) - I give people a function they can call to reenable it (Wire.UsePullups() or something) so if they know that they're taking a shortcut, sure, go ahead - but IMO it does the users a disservice to hide a flaw in their design (missing pullups) from them in the simple cases, because the builtin pullups are 1/5th of what the standard requires, and these lead to nightmare debugging when you get to a case that doesn't work - maybe it works with short wires not with long ones, or adding another device to the I2C bus causes a one of the other devices to not work, etc. But it's not relevant on tiny85 because that doesn't work with the USI on classic AVRs, only on parts with a full I2C ("TWI", two wire interface - so named because until 2014, Phillips held I2C as a registered trademark; there is also SMBus which has tighter constraints electrically but an identical protocol. And we use a library called Wire to use it. )
**Note that there is a singe chip with a USI where that CAN work, the tiny 1634, ; I don't recall whether ATTinyCore does it or not, as I said, it's bad practice to rely on rthem. )
Where exactly do the pullup resistors go? It's from SDA > VCC and SCL > VCC right? I added those and unfortunately did not have any success. Also, yes, if there is just #include <Wire.h>, the IDE returns "error compiling" when the ATTiny85 is set as the board. Is that supposed to be a bug? Because I've read on sites that Wire simply doesn't work with this type of microcontroller, which is why people use TinyWire.
I'm still trying to figure out how to use the TinyWire library so that I can do something else with one master receiver and multiple slave senders, but I haven't been able to get it to work. For now, I am trying to connect one master to one slave, where if the slave successfully sends and the master successfully receives, the LED should turn on. The code I am trying is at the top of this post, and the master now has the while loop:
while (TinyWire.available()) {
and the wiring looks like this, with the slave on the left and master on the right:
You only need one 4.7k pull-up on each of SDA and SCL lines to VCC, not both ends as per your diagram above, does not matter where you place them.
I use ATTinyCore to communicate, with I2C, between two ATTiny85s (and a MCP23017 to read a keypad). One is the controller and the other a slave (I do not switch around). I have zero problems talking both ways with the native Wire included with ATTinyCore. I use Wire.onReceive() and Wire.onRequest() on the slave side. No need for external libraries.
You should not have any problems with what you are trying to do.
I installed the ATTinyCore by Spence Konde, and used the following code, however if I click on Verify, the IDE says "Error compiling for board ATtiny 25/45/85", I have 85 selected.
#include <Wire.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Please post the full text of the error so we can try to reproduce it and understand what you're running into on ATTinyCore.
My ATTinyCore (unlike - to my knowledge - all other tiny85-supporting board packages) includes a special version of Wire, which chooses the appropriate implementation of I2C for your hardware.
I have ATTinyCore by Spence Konde Ver 1.5.2 (where do you get 1.8.3? I downloaded from here), ATTiny85, 16MHz, how do you enable LTO and millis()? Compiling with Arduino IDE 1.8.12. Running on Mac Ver 11.2.3.
It's fine, using just master sender and slave receiver with TinyWire is alright because the board can switch between being the master or slave, so "requests for receiving data" can be programmed with code instead of using the command in the library.
That's what is expected when using any core other than my ATTinyCore (note that there are multiple cores using the same name - I didn't pick my own name, just tookon the one of the defunct ones that I started working from and kept the name, and made it into something far far larger.
Oh and exit status 1 carries no information - it meanst the same thing as compile failed, what matters is what it said before that which will tell you why. Usually , the first errors it displays are the most important ones. The often nonsensical ones that follow arevery often knockon effects from the state that the compiler was left in after the first error it hit.
Sometimes they're not - and you can get useful help with several errors at a time when running newly written code for the first time. But that's less common than seeing one error than makes sense,followed by 10+ that you can't make a lick of sense out of.
(background: When a program exits,it leaves behind an "exit code", where "0" indicates success. and anything else some sort of error condition. The only error condition that arduino builder ever reports is 1 for compilation failure. So literally every compile error will end with that, so tells one nothing., other than that the compile failed
My apologies. That is the version for AVR Boards. The version for ATTinyCore is indeed Ver 1.5.2.
I really dislike the Boards manager in IDE Version 2 Beta (where I checked the version). It is difficult to navigate (for me at least) and does tend to jump when unexpected.
I do not know how Windows and Mac differs. LTO and Millis() are options for ATTiny85 under the Tools menu for Windows.
Those options show up as submenus of the Tools menu once an ATTinyCore board is selected. There are a few other menus to control core features that show up there as well (these are for things like LTO that change command line params, and things like millis() which need a preprocessor macro to be defined or not defined globally, and that can't be done from inside the sketch file.
If you do not see those options, you do not have a board definition provided by my core selected.
btw - The board manager has always jumped around for me, I generally have to find the core I want three times before it will stay still without refreshing the list and jumping back to the top long enough for meto tell it to install.
Yes, using your ATTinyCore works with the Wire library and ATTiny85. It doesn't work with the other Boards set that I've downloaded (is it Spence Konde or some other?). Thank you very much!
Thanks! Yes, the LTO and Millis() options show up when the ATTiny board is selected with DrAzzy's ATTinyCore. I was using another called ATtiny, not sure if this is Spence Konde or some other boards set.