# LiDAR with Arduino - TF-Luna Sensor

Als je iets anders wilt dan Ultrasonic Sensor HC-SR04 kwa afstand namelijk meer afstand met minder ruis kun je kiezen voor de TF-Luna Sensor. Dit is een Lidar sensor die met behulp van een laser de afstand tot een object meet tussen 0,2 en 8 meter.  
&gt; De TF Luna LiDAR-afstandsmeter biedt een bereik van 0,2 m-8 m, een resolutie van 1 cm en een nauwkeurigheid van ±6 cm @ (0,2 m-3 m) &lt;

#### **Materiaal:**  
Arduino Nano + USB kabel  
TF-Luna Sensor  
Breadboard  
Jumper wires male/male  
kabel verbinders voor snel testen zonder solderen  


Ik heb om dit aan de praat te krijgen de volgende bron gebruikt:  
[https://www.diyengineers.com/2022/06/02/lidar-how-to-use-with-arduino/](https://www.diyengineers.com/2022/06/02/lidar-how-to-use-with-arduino/)  
  
Op deze site kun je belangrijke documenten downloaden zoals datasheet en user manual maar die heb ik amper nodig gehad.  
[https://en.benewake.com/TFLuna/index.html](https://en.benewake.com/TFLuna/index.html)

Shop:  
[https://eu.robotshop.com/nl/products/benewake-tf-luna-8m-lidar-afstandssensor](https://eu.robotshop.com/nl/products/benewake-tf-luna-8m-lidar-afstandssensor)

  
Wat ik lastig vond bij deze is dat er geen JST GH1.25 - 4P naar Dupont verloopjes bij zaten dus heb ik de kabel gestript en waco verbindingsklemmen gebruikt om te testen. Solderen kan natuurlijk ook maar ook dat was me even teveel werk.  
Kiwi Electronics verkoopt ze met de handige kabels maar niet op voorraad. [https://www.kiwi-electronics.com/nl/tf-mini-s-lidar-module-10359?search=lidar](https://www.kiwi-electronics.com/nl/tf-mini-s-lidar-module-10359?search=lidar)

[![IMG_4943.JPG](https://bookstack.hku.nl/uploads/images/gallery/2025-01/scaled-1680-/uLYimg-4943.JPG)](https://bookstack.hku.nl/uploads/images/gallery/2025-01/uLYimg-4943.JPG)[![pins-arduino-nano.png](https://bookstack.hku.nl/uploads/images/gallery/2025-01/scaled-1680-/pins-arduino-nano.png)](https://bookstack.hku.nl/uploads/images/gallery/2025-01/pins-arduino-nano.png)

Ik heb een Arduino Nano gebruikt. In de tutorial wordt de Arduino Uno gebruikt en voor data overdracht de poorten ICSP2. Volgens bovenstaand schema kan je daarvoor <span style="background-color: rgb(206, 212, 217);">IC2</span> pins <span style="background-color: rgb(251, 238, 184);">Analog A4 &amp; A5 </span>gebruiken.

De 6e kabel hoef je niet te gebruiken.

Installeer Arduino software  
[https://www.arduino.cc/en/software](https://www.arduino.cc/en/software)

Arduino Library:  
[https://github.com/budryerson/TFLuna-I2C](https://github.com/budryerson/TFLuna-I2C)   
Verbind de arduino met mini usb kabel aan je computer, kies links bovenin de Arduino software venster je type arduino board en de usb poort

[![Screenshot 2025-01-06 at 16.32.39.png](https://bookstack.hku.nl/uploads/images/gallery/2025-01/scaled-1680-/screenshot-2025-01-06-at-16-32-39.png)](https://bookstack.hku.nl/uploads/images/gallery/2025-01/screenshot-2025-01-06-at-16-32-39.png)

Volg de aanwijzingen in de tutorial via link bovenaan dit document om de library via Arduino software te installeren.  
Ga bovenin je menu balk in de arduino software naar Tools &gt; Serial Monitor.  
Zet rechts onderin 9600 Baud om naar 115200 Baud, anders krijg je rare resultaten.

Houd je hand boven de lidar sensor en zie de waardes veranderen.  
  
Als je nog info mist in deze uitleg of iets is niet duidelijk laat het me dan weten dan pas ik het aan.  
<Simone.vandordrecht@hku.nl>

### **Voorbeeld project: intensiteit licht LED pixel ring beïnvloeden door afstandmeting Lidar TF Luna sensor** 

**Materiaal:**  
[Neo pixel ring](https://www.kiwi-electronics.com/nl/neopixel-ring-16-x-ws2812-5050-rgb-led-met-drivers-1425) 12 of 16 pixels  
Arduino Nano + USB kabel  
TF-Luna Sensor  
Breadboard  
Jumper wires male/male  
Resistor 330 Ω tot 470 Ω  
Universal AC adapter 5V  
[Terminal block to 2.1mm DC barrel jack - Female](https://www.kiwi-electronics.com/en/terminal-block-to-2-1mm-dc-barrel-jack-female-747)

Bronnen voor testen:  
[https://www.diyengineers.com/2022/06/02/lidar-how-to-use-with-arduino/](https://www.diyengineers.com/2022/06/02/lidar-how-to-use-with-arduino/)  
[https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation](https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation)  
[https://medium.com/@elonskolnik/arduino-uno-tutorial-neopixel-ring-setup-9fafc099c89a](https://medium.com/@elonskolnik/arduino-uno-tutorial-neopixel-ring-setup-9fafc099c89a)  
[https://chatgpt.com/share/6787fc7b-e388-8006-98cc-3678b193ec44](https://chatgpt.com/share/6787fc7b-e388-8006-98cc-3678b193ec44)  
  
  
**Combineren:**

Soldeer de kabeltjes van de TF Luna aan jumper wires en gebruik krimpkousjes voor bescherming  
Power de Pixelring apart van de Arduino, zorg dat de ground wel doorlust naar de ground van arduino en TF Luna.  
  
[![Screenshot 2025-01-15 at 17.50.41.png](https://bookstack.hku.nl/uploads/images/gallery/2025-01/scaled-1680-/screenshot-2025-01-15-at-17-50-41.png)](https://bookstack.hku.nl/uploads/images/gallery/2025-01/screenshot-2025-01-15-at-17-50-41.png)  
(Duidelijk schema hiervan moet ik nog maken)  
  
Alternatief voor draadloos project: Gebruik een powerbank 5V voor stroom voor de arduino en een batterij pack bijvoorbeeld 4x AA voor de stroom voor pixelring. Foto hiervan volgt..  
Meer info kun je hier vinden:   
https://bookstack.hku.nl/books/arduino-things/page/controlling-ledstrips-with-arduino  
  
**Code:**

<div id="bkmrk-%23include-%3Cadafruit_n" style="color: #dae3e3; background-color: #1f272a; font-family: Menlo, Monaco, 'Courier New', monospace; font-weight: normal; font-size: 12px; line-height: 18px; white-space: pre;"><div><span style="color: #c586c0;">\#include</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">&lt;Adafruit\_NeoPixel.h&gt;</span><span style="color: #dae3e3;"> </span><span style="color: #7f8c8d;">//https://github.com/adafruit/Adafruit\_NeoPixel</span></div><div><span style="color: #c586c0;">\#include</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">&lt;TFLI2C.h&gt;</span><span style="color: #dae3e3;"> </span><span style="color: #7f8c8d;">//https://github.com/budryerson/TFLuna-I2C</span></div><div><span style="color: #c586c0;">\#include</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">&lt;Wire.h&gt;</span><span style="color: #dae3e3;"> </span><span style="color: #7f8c8d;">// Instantiate the Wire library</span></div>  
<div><span style="color: #c586c0;">\#define</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">PIXEL\_PIN</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">6</span><span style="color: #7f8c8d;"> // Pin verbonden met de NeoPixel-ring</span></div><div><span style="color: #c586c0;">\#define</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">NUM\_PIXELS</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">21</span><span style="color: #7f8c8d;"> // Aantal LEDs op de ring</span></div><div><span style="color: #c586c0;">\#define</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">MAX\_BRIGHTNESS</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">255</span><span style="color: #7f8c8d;"> // Maximale helderheid</span></div><div><span style="color: #c586c0;">\#define</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">MIN\_BRIGHTNESS</span><span style="color: #dae3e3;"> </span><span style="color: #7fcbcd;">5</span><span style="color: #7f8c8d;"> // Minimale helderheid</span></div>  
<div><span style="color: #dae3e3;">Adafruit\_NeoPixel strip = </span><span style="color: #f39c12;">Adafruit\_NeoPixel</span><span style="color: #dae3e3;">(NUM\_PIXELS, PIXEL\_PIN, NEO\_RGBW + NEO\_KHZ800);</span><span style="color: #7f8c8d;"> //lees achterop de leds of in de specificaties online welk type het is en pas de informatie hier aan, oorspronkelijk stond GRB in de code maar de ledring is RGBW dus aangepast</span></div><div><span style="color: #dae3e3;">TFLI2C tfLuna;</span></div>  
<div><span style="color: #0ca1a6;">void</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">setup</span><span style="color: #dae3e3;">() {</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Wire</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">begin</span><span style="color: #dae3e3;">();</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Wire</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">setClock</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">100000</span><span style="color: #dae3e3;">);</span><span style="color: #7f8c8d;"> // Stel I2C-snelheid in op 100 kHz</span></div>  
  
<div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">strip</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">begin</span><span style="color: #dae3e3;">();</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">strip</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">show</span><span style="color: #dae3e3;">();</span><span style="color: #7f8c8d;"> // Zorgt ervoor dat alle pixels uit staan</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">begin</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">9600</span><span style="color: #dae3e3;">);</span></div>  
<div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">"Start setup"</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">delay</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">1000</span><span style="color: #dae3e3;">);</span></div>  
<div><span style="color: #7f8c8d;"> // Probeer verbinding met de TF-Luna</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">"Verbinding met TF-Luna..."</span><span style="color: #dae3e3;">);</span></div>  
<div><span style="color: #dae3e3;"> </span><span style="color: #0ca1a6;">uint16\_t</span><span style="color: #dae3e3;"> frameRate;</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #c586c0;">if</span><span style="color: #dae3e3;"> (</span><span style="color: #f39c12;">tfLuna</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">Get\_Frame\_Rate</span><span style="color: #dae3e3;">(frameRate, TFL\_DEF\_ADR)) {</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">print</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">"Frame rate: "</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(frameRate);</span></div><div><span style="color: #dae3e3;"> } </span><span style="color: #c586c0;">else</span><span style="color: #dae3e3;"> {</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">"Kon frame rate niet ophalen. Controleer de verbinding."</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;"> }</span></div><div><span style="color: #dae3e3;">}</span></div>  
<div><span style="color: #0ca1a6;">void</span><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">loop</span><span style="color: #dae3e3;">() {</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #0ca1a6;">int16\_t</span><span style="color: #dae3e3;"> distance = </span><span style="color: #7fcbcd;">0</span><span style="color: #dae3e3;">;</span></div>  
<div><span style="color: #7f8c8d;"> // Haal de afstand op van de TF-Luna</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #c586c0;">if</span><span style="color: #dae3e3;"> (</span><span style="color: #f39c12;">tfLuna</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">getData</span><span style="color: #dae3e3;">(distance, TFL\_DEF\_ADR)) {</span></div><div><span style="color: #7f8c8d;"> // Bepaal helderheid op basis van afstand</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #0ca1a6;">int</span><span style="color: #dae3e3;"> brightness = </span><span style="color: #f39c12;">map</span><span style="color: #dae3e3;">(distance, </span><span style="color: #7fcbcd;">10</span><span style="color: #dae3e3;">, </span><span style="color: #7fcbcd;">100</span><span style="color: #dae3e3;">, MAX\_BRIGHTNESS, MIN\_BRIGHTNESS);</span><span style="color: #7f8c8d;"> // 10 staat voor minimale afstand, 100 staat voor maximale in cm, past dit aan als je meer of minder afstand nodig hebt</span></div><div><span style="color: #dae3e3;"> brightness = </span><span style="color: #f39c12;">constrain</span><span style="color: #dae3e3;">(brightness, MIN\_BRIGHTNESS, MAX\_BRIGHTNESS);</span></div>  
<div><span style="color: #7f8c8d;"> // Stel helderheid in en kleur de LEDs</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">strip</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">setBrightness</span><span style="color: #dae3e3;">(brightness);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #c586c0;">for</span><span style="color: #dae3e3;"> (</span><span style="color: #0ca1a6;">int</span><span style="color: #dae3e3;"> i = </span><span style="color: #7fcbcd;">0</span><span style="color: #dae3e3;">; i &lt; NUM\_PIXELS; i++) {</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">strip</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">setPixelColor</span><span style="color: #dae3e3;">(i, </span><span style="color: #f39c12;">strip</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">Color</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">0</span><span style="color: #dae3e3;">, </span><span style="color: #7fcbcd;">255</span><span style="color: #dae3e3;">, </span><span style="color: #7fcbcd;">255</span><span style="color: #dae3e3;">, </span><span style="color: #7fcbcd;">0</span><span style="color: #dae3e3;">));</span><span style="color: #7f8c8d;"> // Pas hier de kleur aan, volgorde = G, R, B, W (vreemd, klopt niet met de info van de ring maar het werkt)</span></div><div><span style="color: #dae3e3;"> }</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">strip</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">show</span><span style="color: #dae3e3;">();</span></div>  
<div><span style="color: #7f8c8d;"> // Print de afstand en helderheid naar de Serial Monitor</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">print</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">"Afstand: "</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">print</span><span style="color: #dae3e3;">(distance);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">print</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">" cm, Helderheid: "</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(brightness);</span></div><div><span style="color: #dae3e3;"> } </span><span style="color: #c586c0;">else</span><span style="color: #dae3e3;"> {</span></div><div><span style="color: #7f8c8d;"> // Geef een foutmelding als de data niet kan worden opgehaald</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">Serial</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">println</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">"Fout bij ophalen afstandsgegevens."</span><span style="color: #dae3e3;">);</span></div><div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">tfLuna</span><span style="color: #dae3e3;">.</span><span style="color: #f39c12;">printStatus</span><span style="color: #dae3e3;">();</span><span style="color: #7f8c8d;"> // Laat de foutstatus zien</span></div><div><span style="color: #dae3e3;"> }</span></div>  
<div><span style="color: #dae3e3;"> </span><span style="color: #f39c12;">delay</span><span style="color: #dae3e3;">(</span><span style="color: #7fcbcd;">100</span><span style="color: #dae3e3;">);</span><span style="color: #7f8c8d;"> // Kleine pauze voor de volgende meting</span></div><div><span style="color: #dae3e3;">}</span></div>  
</div>