Obstacle detection

I am working on a new project to detect obstacle , and after detecting it will turn off the device. So for this purpose i am using Tof10120 , initially i was thinking abouT PIR sensor but ambient will be warm so used this instead but the problem is now after plugging to Arduino in my serial monitor it is showing 0mm , i have found the code in dronebot workshop forum, kindly help.

  TOF10120 Distance Sensor Demonstration
  TOF10120-Demo.ino
  Demonstrates use of TOF10120 Distance Sensor
  Adapted from code from SurtrTech
  Displays results on Serial Monitor
 
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
#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() {
  Wire.begin(); 
  Serial.begin(9600,SERIAL_8N1); 
  printf_begin();          
 
}
 
void loop() {
  
 
   int x=ReadDistance();
   
   Serial.print(x);
   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++ = Wire.read();  // receive high byte (overwrites previous reading)
    *datbuf++ = Wire.read(); // receive low byte as lower 8 bits
  }
}
 
int ReadDistance(){
    SensorRead(0x00,i2c_rx_buf,2);
    lenth_val=i2c_rx_buf[0];
    lenth_val=lenth_val<<8;
    lenth_val|=i2c_rx_buf[1];
    delay(300); 
    return lenth_val;
}

Are you sure that I2c address of the sensor is 0x52? Did you test it with i2c scanner?

No I didn't checked it in I2c scanner but in that code and also on forum they explained why they used 82 instead of 164 so i didn't altered it, i will share the link of video for reference Laser vs Ultrasonic - TOF10120 vs. HC-SR04 - YouTube

I recomend you try the scanner anyway, because it will test whether the sensor is present on the line or not.

Hi, @tridibesh_11

Have you looked here;

Have you Googled;

TOF10120 arduino

Can you please post a circuit diagram?
Not a Fritzy image, a habd drawn circuit will be fine.
Make sure you include power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:
PS. This may help too.
TOF10120.pdf (1.6 MB)

This the diagram I used and also checked that uart code of GitHub ,and in that case serial monitor is showing start.... Start.....

Hi,
Where did you purchase the sensor?
Can you please post a link?

Can you please post an image(s) of your project so we can see your component layout?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Definitely check with an I2C scanner since the I2C address can be set to any value from 1 to 0xFE (0xA4/164 is the default). There is another register that can be set to disable I2C and use only the UART connection.

I did check the address with I2c scanner , and it is showing 0x52 . So after checking the physical connection I re uploaded the code and surprisingly it is working, thanx everyone for your feedback.

This is the first step ,ill update once I include conditioning code,but any help is appreciated,i am roughly describing my idea:

If distance is less than 300 mm then it will energise a relay ,but it will not stop giving output even the condition is altered until I manually reset it ,

Now my question is is there any way to manually rest it by external switch?

I think the datasheet says it has a range of 1800mm (1.8m). Did you mean 300mm?

Yes my mistake, i edited in the previous post.

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