Hi, I am trying to use a couple of ToF10120 sensors in a project. I downloaded the sketch (TOF10120-Demo.ino, 2019) from the Workshop, but there are errors when it compiles, probably relating to the libraries.
Is there any way to find out which versions of the libraries were used for the original sketch?
TIA
Hi @neonervian,
I assume you are using the code listed in
https://dronebotworkshop.com/laser-vs-ultrasonic-distance-sensor-tests/
which starts with the comment:
/*
TOF10120 Distance Sensor Demonstration
TOF10120-Demo.ino
Demonstrates use of TOF10120 Distance Sensor
Adapted from code from SurtrTech
I don't have any ToF hardware, so I can't check to see if the programme works, but simply compiling the code, (for a Mega2560 as an example), suggests that the 'complaints' from the compiler are limited to 3 warnings, not errors, so that you should still be able to download the program onto your board.
(For my test, "wire.h" in the programme code, is referencing part of Arduino hardware board package AVR 1.8.6, and I am running Arduino IDE 2.3.6)
---------------
Nevertheless, it is usually good practice to overcome warnings, as sometimes their complaint is valid, so I offer the following:
The warnings relate to SensorRead function code listed in the main program. I have a suspicion that the warnings are harmless, but my C++ programming code ability has almost completely rusted away with time, plus I can't test it on hardware, so the following suggestions, which may be in error, are offered in good faith for you to try.
- Change the line
- void SensorRead(unsigned char addr,unsigned char* datbuf, unsigned char cnt) to
- void SensorRead(unsigned char addr,unsigned char* datbuf, uint8_t cnt)
- Change the line
- Wire.requestFrom( 82, cnt); // request cnt bytes from slave device #82 (0x52) to
- Wire.requestFrom( (uint8_t)82, cnt); // request cnt bytes from slave device #82 (0x52)
- Delete the line
- unsigned short result=0;
-----------------
I hope this helps, apologies for any mistakes on my part, and good luck with your project, Dave.
Hi Dave,
Thanks for the encouragement*! The sketch does in fact upload & run on a Uno, so I see that the 2 sensors work OK. Next, I have to change the address in one of them, so that they can both work on the I2C bus.
BUT they don't work on an ESP32 (dev module V2) (yet), an important part of my project because of the memory available & of course the WiFi capability. I see SCL & SDA on the scope, same as for the Uno, but the sensors are not found 🙁
I have to keep looking!
Regards,
neonervian
P.S. *when I was working, I would often tell the client that it's just tech stuff, & it will work at the end of the day 🙂
Hi @neonervian,
It is good to hear you have it working with a Uno, but clearly you have a few hurdles to jump for your project.
Please remember, I don't have the hardware, and I haven't tested things, so my comments below will be a mixture of what I can see on the web, and general background, which leaves plenty of opportunities for mistakes and missing things ... treat what I say like 'brainstorm' suggestions arising over a chat with appropriate beverages, not as a tested and fact checked report.
The test program previously discussed consisted of a couple of functions, credited as originating from a somewhat mysterious source, "SurtrTech". The principal function SensorRead provoked 3 compiler warnings: two examples of hoping the default compiler type conversion would provide the required result, and creating a variable that was never used. The first two examples can easily produce a programme that fails or only works under certain conditions, whilst the third is usually harmless.
However, most of the code is 'semi-hidden' in the Wire library, which is widely used, and hopefully maintained.
-------------
You mention that the TOF10120 works with the UNO, but not the ESP32. As the interface is I2C, I am inclined to suspect hardware, rather than software issues.
Data on the TOF10120 is rather sparse and confusing. It seems to refer a PCB module, which will be dominated by the main device, but does not have a recognisable source. I suspect it is a clone/adaption of a development board or similar by the device manufacturer. I found two variations of the data sheet:
https://www.electroniclinic.com/wp-content/uploads/2021/06/TOF10120-Datasheet.pdf
https://www.mikrocontroller.net/attachment/618619/TOF10120___-1.pdf
The latter includes "The TOF (time-of-flight) measurement technology of TOF10120 is implemented using Sharp's original low-cost CMOS process SPAD (single photon avalanche diode). "
I also found a small GitHub repo at https://github.com/SoyM/TOF10120/
This repo includes a small example program that might also be of interest, albeit it isn't clear which microcontroller is used as the host. At first glance, it may be connecting to two TOF10120 devices, using a separate I2C bus for each.
--------------
Googling for a Sharp TOF device datasheet yielded:
https://global.sharp/products/device/lineup/selection/opto/haca/diagram3.html
which appears to be linecard for two variants of a Sharp device:
(Click on image to see enlarged view.)
This states:
Operating voltages of 2.6 to 3.5V and 3.0 to 3.5V.
This suggests that the I2C bus voltage should not exceed 3.5V, which in turn suggests it should not be supplied with 5V, or connected to a 5V I2C bus, which Bill (@dronebot-workshop) shows in his blog:
Of course, as the documentation is so sparse, there are many possible explanations, such as the TOF10120 uses a different device, in practice the device can withstand 5V operation, the I2C pins are an unusual design which allows 5V I2C connection, etc.
--------------
However, my first thoughts are that the device should be supplied with 3.3V, and usable with an ESP32, which has 3.3V GPIOs for I2C.
----------------
The latter datasheet from mikrocontroller also includes a protocol table:
This shows a Baud rate of 9600, which is much slower than the typical I2C rates of 100kHz and higher.
The I2C databus bit rate is 'determined' the clock rate microcontroller sent on the I2C CLK pin, but driving it faster than the peripheral devices, such as the TOF10120 can support, could obviously be a problem.
I don't know what bit rate the Wire.h library uses, but maybe this is a point to check. Does it vary between the Uno and the ESP32? I suspect there is a magic routine in the Wire package for setting the rate, so it maybe changeable, by a single code line change or addition.
I note that the GitHub program uses a different approach, which explicitly sets a data rate of 9600. (The 'SurtrTech' sets the Serial monitor rate to 9600, but obviously, this does not affect the I2C bus.)
-------------
The two I2C databus lines are open-collector/open-drain, so they each of them must be pulled high with a resistor to Vdd (3.3V for ESP32). Sometimes the peripheral board will include the resistor, but not always. As the data rate is slow, a relatively high value such as 2.7 kOhms should be ok, but an oscilloscope check is always advisable, if you have one available.
-----------
You say you need to connect two TOF1-120s to the same microcontroller. Unfortunately, I think the I2C Address of the TOF10120 is fixed to a single value, so direct connection of two devices to the same bus will not work. I2C bus expander devices are available, but a simpler solution is two I2C buses from the same ESP32.
Once again, Bill (@dronebot-workshop) has covered this situation in video/blog
https://dronebotworkshop.com/i2c-esp32/
Additional discussion is available from Random Nerd Tutorials at
https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/#7
---------------------------
I hope some of this rather long sermon will be helpful. I hope you get your TOF10120s to perform to your expectations, in spite of the limited documentation, etc. However, if you hit a brick wall, you might like to note other manufacturers are actively developing alternate devices. I haven't looked in any detail, but as an example, I am aware STMicro are in the market.
Good luck, and let us know what how you get on. Best wishes, Dave
Hi Dave,
Thanks for all that! I've had a relatively quiet day today (changing a battery in Bose QC35 headphones). I'll study what you wrote during the week.
I shall carefully check the data sheets, to make sure I'm not risking a fire in the parts! The I2C signals on the ESP32 look the same as on the Uno, & I have 4k7 pull-ups on the lines.
Concerning 2 sensors on the I2C bus: the WaveShare parts can be set to any address (once you've got them connected to the utility - not evident!), & also, it's apparently possible to change the address of the ToF10120 sensors. These things work out of the box, connected to a (Waveshare) serial-to-USB adapter with a dummy Arduino sketch. Maybe I should try them in UART mode, if I can give them separate addresses.
Regards,
N
Hi @neonervian,
Sorry, but I am a bit confused. The title of this thread refers to ToF10120 sensor, but you are now referring to Waveshare products.
A quick look at the Waveshare web pages revealed about 4 or 5 time of flight sensors, albeit at least one is probably no longer available, but I couldn't see any reference to TOF10120.
Furthermore, there is evidence that one or more of these modules include an 'onboard' microcontroller, that are interfacing between the actual sensor and the accessible pin connections. Whilst this may provide improved functionality, it might also be complicating/confusing the interface and changing the protocols involved. Hence, at least some of my comments will probably be inappropriate or wrong.
I don't know which of the modules you have, so I could only briefly scan the information covering all of them.
Sadly, the information I could see, left me with more questions than answers. It mentions supporting software, but what this consists of, or how it is obtained and accessed, was not clear.
So sorry, but at this stage, I don't think I can get any further.
If you still have queries, perhaps you would like to pose a new question, clearly specifying which modules you are working with, together with references to all the appropriate data sheets, programmes, etc.
Good luck and best wishes with your project.


