0

[I asked this question on the Arduino Stack Exchange, and I waited a while and got no answer, so I thought that I'd ask it here!]

I want to integrate Pololu's vl53l0x Time of Flight distance sensor into a project of mine. They created a library to interface with the vl53l0x over I2C, which works great on my Uno. The only problem is that Wire.h doesn't work on the ATtiny85.

TinyWireM is a replacement library for Wire that I think would be compatible. My question is this: how can I go about replacing Wire with TinyWireM? Is it as simple as replacing all instances of Wire with TinyWireM? Is there anything else I need to do to make the library ATtiny85 compatible?

I did try just redefining "Wire" as TinyWireM as shown below:

#include "TinyWireM.h"
#define Wire   TinyWireM

Everything compiles and uploads to my ATtiny85 just fine. However, the sensor only appears to be outputting a value of about 7600mm pretty consistently.

Thanks!

Community
  • 1
  • 1
Alex Wulff
  • 2,039
  • 3
  • 18
  • 29
  • why does tinywirem not work, are the peripherals not on the chip, are the register names wrong? – Grady Player Jan 27 '17 at 00:11
  • @GradyPlayer do you mean why does Wire not work? – Alex Wulff Jan 27 '17 at 00:23
  • yeah, why does it not work? – Grady Player Jan 27 '17 at 01:02
  • @AlexWulff can you check if the communication is running? I mean attach an oscilloscope and see the actual communication. Anyway 7600mm is a bit strange, since the sensor should report at most 2m... The documentation of the IC is quite obscure, though, so correcting it may be difficult... – frarugi87 Jan 27 '17 at 09:06
  • @GradyPlayer I get errors like 'TWS4' undeclared and 'TWIE' undeclared and 'TWEN' undeclared and things like that. – Alex Wulff Jan 27 '17 at 16:35
  • @frarugi87 I2C looks like it's working. I hooked up an O Scope to SCL and I'm getting a pretty consistent square wave (which I think sounds about right). Additionally, SDA is showing an erratic sequence of square waves which I guess indicates that data is being transferred. – Alex Wulff Jan 27 '17 at 22:14
  • @AlexWulff The TinyWireM library does not output errors, rght? anyway did you try to "decode" the I2C communication? At least the 16-bits return value... – frarugi87 Jan 27 '17 at 22:30

1 Answers1

1

The Wire Library doesn't work on the ATTiny, because it has only a USI (Universal Serial Interface), which is not the same hardware as in the Arduino Uno. The TinyWireM library shows almost the same behavior as the Wire library on the Uno, but is not the same library ported to the ATTiny. I don't understand completely, why you want to rename the TinyWireM library to Wire. If you just want to pretent, that it would really be the Wire library, this might not be a good coding practice, since this somewhat hides the fact in your code, that you use not the Wire library, but a different library. If you want to create code, that is reusable both on Uno and ATTiny, than I would suggest to use a combination of #define and #ifdef,#ifndef and #endif directives, so that depending on a defined flag only the corresponding code is compiled. As far as I know the ArduinoIDE also uses these to include the right header files for each target. If you can find the corresponding definition in the header files, you can use this. But using your own definition at the start of the file is easier.

As a tip: If you are ever trying to establish multi mastered communication over I2C with TinyWireM (which works perfectly for me), you have to give free the bus after your transmission, so that another master can use it. I have done this with disabling the USI hardware with USICR&= 0b11001111; pinMode(0,INPUT); pinMode(2,INPUT); The library doesn't do this for you.(If you don't do this, the bus will stay occupied and no other master can communicate) To use the USICR register directy you also have to #include <USI_TWI_Master.h>

chrisl
  • 393
  • 3
  • 16
  • I will try this soon - thank you. However, the problem I'm having is simply that the TinyWireM library can't seem to communicate correctly with the device. I'll try setting the registers like you suggested. – Alex Wulff Mar 20 '17 at 08:38
  • Did you have some progress in your problem? Maybe - if you have a spare arduino - you can check, what data is actually send to the sensor. – chrisl Apr 02 '17 at 15:45
  • I'm still working on it, but I'm still not getting any success. Things work just fine with an Uno which is the strange part. I'm going to keep investigating it more though, so thank you for your answer. – Alex Wulff Apr 03 '17 at 22:32