Sketch modify for TOF10120 sensor

Hi everyone.

I am working on a project that will open and close a lid when within a desired range.

I have a small TOF sensor (TOF10120) found sample code that works on Arduino, have not been able to get it working with particle.

Ultimately I would like to trigger 2 Digital ports HIGH and LOW when at 25mm. then wait 15 seconds and change the state of the pins back.

below is the code for this i2c TOF sensor for Arduino.

#include <Wire.h>

unsigned char ok_flag;
unsigned char fail_flag;

unsigned short lenth_val = 0;
unsigned char i2c_rx_buf[16];
unsigned char dirsend_flag=0;

void setup() {

void loop() {
   int x=ReadDistance();
   Serial.println(" mm");

int serial_putc( char c, struct __file * )
  Serial.write( c );
  return c;

void printf_begin(void)
  fdevopen( &serial_putc, 0 );

void SensorRead(unsigned char addr,unsigned char* datbuf,unsigned char cnt) 
  unsigned short result=0;
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(82); // transmit to device #82 (0x52)
  // the address specified in the datasheet is 164 (0xa4)
  // but i2c adressing uses the high 7 bits so it's 82
  Wire.write(byte(addr));      // sets distance data address (addr)
  Wire.endTransmission();      // stop transmitting
  // step 2: wait for readings to happen
  delay(1);                   // datasheet suggests at least 30uS
  // step 3: request reading from sensor
  Wire.requestFrom(82, cnt);    // request cnt bytes from slave device #82 (0x52)
  // step 5: receive reading from sensor
  if (cnt <= Wire.available()) { // if two bytes were received
    *datbuf++ =;  // receive high byte (overwrites previous reading)
    *datbuf++ =; // receive low byte as lower 8 bits

int ReadDistance(){
    return lenth_val;

This issue description is not very helpful. Can you elaborate and be a bit more specific?

BTW, Wire.h is not needed in Particle code.

1 Like

Like I said in my previous post the sample sketch is for Arduino whats why the wire.h is included.

So some of the problems I am having is with the line with " fdevopen( &serial_putc, 0 ); "

I am new to C++ learned a good bit the last 2 months, however I am not sure how to get it working and create a PIN action form a distance picked up by the sensor. I seen some of your previous post on VL53L0X Time-of-Flight Ranging Sensor… I am not able to find any info on this TOF sensor working with Particle devices. I would appreciate it if you can help me with it or get me heading in the right direction. Thanks.

Again, what is “some of the problems” supposed to mean?
A specific description of the problem is required.

  • Does it not compile?
  • Error messages?
  • Does it compile but do the wrong stuff?
  • In what kind is that stuff wrong?

e.g. like this

  if (length_val < 25) {
     digitalWrite(pin2, LOW);
     digitalWrite(pin1, HIGH);
  else {
     digitalWrite(pin1, LOW);
     digitalWrite(pin2, HIGH);

@bbarr7iot, remove the printf_begin(); line and the entire printf_begin() function. Also, change the Serial.begin(9600,SERIAL_8N1); to Serial.begin(9600);. It should then compile without error.

Nonetheless, I agree with @ScruffR, that detail is everything when looking for support. The more you describe what you tried and what didn’t work, the more we can help.


Yes sir it will not compile.
'no matching function for call to ‘USBSerial::begin(int, uint32_t)’
‘fdevopen’ was not declared in this scope

Thanks for reviewing the sample code I provided; works now with your changes.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.