Trybotics Logo

ToF 10120 Laser Rangefinder to Measure Distance + LCD © GPL3+

DESCRIPTION

Tutorial video

         Hey everyone, and welcome to this tutorial, it’s about the ToF 10120 (Time-of-Flight) LASER Range sensor, the ToF sensors have become very popular due to their reliability and easy use, the most known ones are the Ultrasounds like HC-SR04 (there are a lot of variations), JSN SR-04…, you can find Infrareds too.

But today we are about a LASER one, they all have the same principle (Send signal (Ultrasound-Laser wave-Infrared), and wait for it to return back) the duration between sending and receiving is called the Time of Flight: Speed is known before, Duration is calculated and divided by 2 -> We can find the distance.

The advantage of the LASER ones is that they are very small like the one we’re using, and can be used for high frequency applications such as camera AF…

The module has 6 pins (GND-VCC) then (RX-TX) for UART and (SDA-SCL) for i²c, in this tutorial we gonna use the i²c interface to wire it with Arduino also I’ll add a LCD i²c screen to see the measure in (mm) you can convert it if you want just simple calculations.

You can add LCD i²c screen as I did or whatever you prefer

Tests:

To use this module is straight forward, wire and upload the codes, in its datasheet the max range is 1.8 meter, but as you can see I could measure up to 2 meters:

That’s all folks … comment if you have any problem.

Description:

Tutorial video

         Hey everyone, and welcome to this tutorial, it’s about the ToF 10120 (Time-of-Flight) LASER Range sensor, the ToF sensors have become very popular due to their reliability and easy use, the most known ones are the Ultrasounds like HC-SR04 (there are a lot of variations), JSN SR-04…, you can find Infrareds too.

But today we are about a LASER one, they all have the same principle (Send signal (Ultrasound-Laser wave-Infrared), and wait for it to return back) the duration between sending and receiving is called the Time of Flight: Speed is known before, Duration is calculated and divided by 2 -> We can find the distance.

The advantage of the LASER ones is that they are very small like the one we’re using, and can be used for high frequency applications such as camera AF…

The module has 6 pins (GND-VCC) then (RX-TX) for UART and (SDA-SCL) for i²c, in this tutorial we gonna use the i²c interface to wire it with Arduino also I’ll add a LCD i²c screen to see the measure in (mm) you can convert it if you want just simple calculations.

You can add LCD i²c screen as I did or whatever you prefer

Tests:

To use this module is straight forward, wire and upload the codes, in its datasheet the max range is 1.8 meter, but as you can see I could measure up to 2 meters:

That’s all folks … comment if you have any problem.

Description:

TOF10120_i2c_SerialM.inoArduino
/* This code is to use with ToF10120 to measure distance in (mm) and shows it on the Serial monitor using Ic inteface
 * Modified and adapted from a code found on some dodgy chinese website
 * Refer to www.SurtrTech.com for more details
 */

#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;
}
TOF10120_i2c_LCD.inoArduino
/* This code is to use with ToF10120 and LCD ic to measure distance in (mm) and shows it on the LCD screen
 * Modified and adapted from a code found on some dodgy chinese website
 * Refer to www.SurtrTech.com for more details
 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>


#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here
#define BACKLIGHT_PIN 3 // Declaring LCD Pins
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

unsigned char ok_flag;
unsigned char fail_flag;

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

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup() {
  Wire.begin(); 
  Serial.begin(9600,SERIAL_8N1); 
  printf_begin();
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH); //Lighting backlight
  lcd.home ();          

}

void loop() {
  

   int x=ReadDistance();
   Serial.println(x);
   lcd.clear();
   lcd.print(x);
   lcd.setCursor(0,5);
   lcd.print("mm");
   delay(500);
  
}

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;
}
LCD_i²c NewLiquidCrystal library

Description:

ToF10120 + LCD Wiring
Wiring xllzx9tdaj

Description:


YOU MIGHT ALSO LIKE