TOF10120 Laser Range Sensor / Micropython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay in this video we are going to be looking into distance measurement and we'll be using this little device here this is the tof10120 it's a time-of-flight ranging sensor and it's very small very compact and they're available online now there are other distance sensors available like the ultrasonic distance transducer which uses sound to measure distance or there's the infrared distance sensor which uses infrared leds to measure distance but this sensor here uses a laser to measure distance so it has two parts it has a laser and it has a detector and the detection beam width is a lot narrower than the ultrasonic or the infrared distance sensor okay i have my range sensor connected up to my raspberry pi pico i'm running a little program that will detect range and i have four leds that will show the distance from an object and a little beeper for alarm when i get too close so it's a collision warning system so as i apply an object i bring my hand closer you see the first led comes on second third fourth and we come right up to it the alarm goes off indicating collision as i bring my hand back leds go out there's my little collision detection system using the range sensor okay the sensor has six wires and we only need four wires to interface it to the raspberry pi pico and if they're labeled one to six starting on the very right that's pin one and then pin six is on the very left so pin one is ground pin two is vcc which could be either five volts or three point three volts then pins five and six is i squared c bus so pin five is the data line and pin six the one on the very left is the clock line so we hooked it up to the raspberry pi pico running a little program and we got a little distance measuring system okay on the back of the sensor you can see it has an onboard micro which does all the calculations so you don't have to do any calculations in software it actually outputs the distance directly now the range of the sensor is 1800 millimeters or 180 centimeters which is 1.8 meters which is around six feet so all you have to do is read the serial port which is you are compatible or the i squared c port to get your distance measurement and i'll read it out directly in millimeters okay here's a schematic diagram of the circuit that i built on my breadboard so you could see the raspberry pi pico which is driving four leds on gpio pins 18 19 20 and 21 with 680 ohm current limiting resistors for leds 1 2 3 and 4. now gpio pin 22 is driving this 2nd 3904 npn transistor which is driving the beeper and the beeper is getting its supply voltage it's 5 volts from v bus which is pin 40. now on the sensor side we have pin 2 connected up to 3.3 volts that's a vcc and pins 36 and pin one is connected to the ground pins 5 and 6 is the i squared c bus on gpio pins 12 and 13 which is connected to sda0 and scl0 now all we have to do is write some code in micropython to get the system up and running okay here's the code running on the pico and it's written in micro python and i'm using thawney as my ide editor so if you look at the very top from machine i'm importing pin and i2c so pin is for the gpio pins for the leds and the beeper and i2c is to drive the sensor now from time i'm importing sleep for my time delays so the first thing we do is set up our i2c bus i'm using i2c0 and the clock is on pin 13 and the data line is on pin 12 and the speed of the i2c bus is 100 kilohertz now i'm setting up my leds led 1234 on pins 18 19 20 and 21 and i'm setting the pins as outputs and my peeper is on pin 22 and i'm setting the pin as an output now the first thing it's going to do is going to scan for a nice squared c address on the bus so address equals i2c scan and we'll scan all the legal 7-bit addresses and then we'll store it in the variable address then i set up a byte array with two bytes because the data coming back from the sensor will be in two bytes and then we put them together we join them together for a 16-bit word and that's going to be our distance data so we go into the while true loop and so the first thing we do we send the null to the sensor at the address that it has picked out now in my case it was address decimal 82 then the sensor will send back two bytes of data and i'll put it into my byte array with two bytes then i take the byte array with the two bytes and i make the two bytes into a 16-bit word and i take the first byte the high byte and i shifted eight bits to the left and or it with the second byte and that'll give me a 16 bit data distance so we could print the distance right here so that will give us our distance in millimeters now if you're doing a robotic project you could just stop there that's all you need there's your distance but we're going to continue on for the led display so we're going to go further so here's all the leds so if the distance is greater than 200 it's going to turn off all the leds and the beeper and if the distance is between 180 and 140 get one led 139 and 100 you'll get two leds and it goes all the way down two three four until the very end if we come right up to the sensor we're going to get all four leds on and the beeper is going to sound that's going to be our alarm so that's the code there it's pretty simple i'll have to do is load it up into the pico and run the code for the distance measurement okay here's my board layout you can see the raspberry pi pico and the four leds that's driving and the current limiting resistors there's my beeper with the transistor that's driving the beeper i'm getting the five volts for the beeper from pin 40 that runs along the top rail comes down and feeds the beeper and i have a 3.3 volt rail that comes down and feeds the sensor now the sensor has dual pin headers which do not fit into the breadboard so what i did i soldered them onto a dip socket and that fits into the breadboard properly so that's kind of handy so another thing we could use this for instead of distance measurement we could use it as a tilt sensor because as you tilt it we'll get a response from the leds if we tilt it right down we'll get the alarm so it's another another way we could use this sensor now i made another video on how you could do this with arduino nano and i'll put a link in the description box so you can either use the nano or the pico for distance measurement
Info
Channel: 0033mer
Views: 6,423
Rating: undefined out of 5
Keywords: Time of Flight, Laser, Forth, Pico, Micropython, Range sensor, Raspberry Pi, 0033mer, I2C, SDA, SCL
Id: 9f7JOaR3o-c
Channel Id: undefined
Length: 7min 7sec (427 seconds)
Published: Wed Mar 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.