Project tutorial

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

How to use the small module to measure distance for high frequency applications... or low.

• 3,736 views
• 4 comments
• 22 respects

## Components and supplies

 Arduino UNO & Genuino UNO
×1
 ToF10120
×1
 Adafruit Standard LCD - 16x2 White on Blue
×1

## About this project

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.

## Code

##### 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;
}
```

## Comments

• 15 projects
• 45 followers

March 19, 2019

#### Members who respect this project

and 14 others

See similar projects
you might like

#### Measure Distance Using JSN SR-04T + LCD

Project tutorial by SurtrTech

• 1,662 views
• 1 comment
• 6 respects

by remnis

• 20,379 views
• 3 comments
• 14 respects

#### Simple Ultrasonic Distance Measurer With LCD Display

Project showcase by onatto22

• 6,655 views
• 4 comments
• 23 respects

#### Making LCD Thermometer With Arduino And LM35/36

Project tutorial by Konstantin Dimitrov

• 69,653 views
• 17 comments
• 65 respects

#### Soil Moisture Sensor With LCD Display

Project tutorial by Patel Darshil

• 19,347 views
• 4 comments
• 37 respects

#### Arduino Keyless Door Lock System with Keypad and LCD

Project tutorial by DIY Hacking

• 17,557 views
• 16 comments
• 48 respects