Categories

Distance measurement using Raspberry Pi and sensor E18-D80NK

Distance measurement using a Raspberry Pi and sensor E18-D80NK

 

The sensor allows you to measure distances. He works in digital, that is, sends the so-called zero and one depending on if the object was discovered if the dir. Sensitivity adjustment is via the knob in the module.

 

We will need the following items:

 

Connect to the Raspberry Pi

You need to connect both devices at the table

 

GPIO Raspberry PiSensor E18-D80NK
5 VThe wire is red or brown
GNDWire - green or blue

1

Wire yellow or black

 

 

The scheme of connection sensor E18-D80NK with the Raspberry Pi.

Service

In order to use GPIO pins in the Raspberry we need a library that permits us to do it. For this we will use the library wiringPi. Description how to download and install find here. Simply copy the following command. In the end, to validate the installation:

 

  • gpio readall

After this command we should see the following table:

Now we will write a simple program. Create a file e18_d80.cpp

 

  • nano e18_d80.cpp

 

The file should contain the following programs:

#include  
#include  
using namespace std; 

int sensor = 1; //GPIO1 pin of the Raspberry connected to the output of the sensor 

int main(void) 
{ 
 wiringPiSetup(); 

 pinMode(sensor, INPUT); //setting pin 2 to Raspberry output 
 cout<<endl<<"Test sensor"<<endl<<endl; 

 while(1) 
 { 

 cout<<"object: "; //display results on the screen in a loop for 500 MS 
 if(digitalRead(sensor) == 0) //low state indicates detection of the object 
 { //status is high, and the lack of 
 cout<<" YES"<<endl; 
 } 
 if(digitalRead(sensor) == 1) 
 { 
 cout<<""<<endl; 
 } 
 delay(500); 
 } 
} 

Then save the changes to the file and close it. To compile the file you created, use the command:

 

  • g++ -Wall -o e18_d80.oe18_d80.cpp -lwiringPi

 

The program is run using the following command:

 

  • sudo ./e18_d80.on

 

The result of the program can be viewed on the monitor:

 

 

The screen of the monitor.