Infrared Tracking Sensor
The infrared tracking sensor uses the TCRT5000 infrared reflective sensor, a photoelectric sensor that integrates emission and reception. It consists of an infrared light-emitting diode and an NPN infrared phototransistor. The detection reflection distance of 1mm-25mm is applicable. The sensor features an M3 mounting hole, making adjustment and fixation convenient and easy to use. It uses a wide-voltage LM393 comparator, with clean signals, good waveform, and strong driving capability exceeding 15mA. It can be applied to robot obstacle avoidance, robot white line or black line tracking. It can detect black lines on a white background as well as white lines on a black background. It is an essential sensor for line-following robots.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a230r.1.14.16.7def2810sScrt7&id=580318855469&ns=1&abbucket=12 Materials download link: https://pan.baidu.com/s/11dDQHyYJfi0nNyC28vkpoA Materials extraction code: qvpm
Specifications
Operating voltage: 3.3V-5V Detection reflection distance: 1mm~25mm applicable Output method: DO interface is digital output; AO interface is analog output Read method: ADC Number of pins: 4 Pin (2.54mm pitch header)
Principle Analysis
The infrared emitting diode of the TCRT5000 sensor continuously emits infrared light. When the emitted infrared light is not reflected back or the reflected intensity is not strong enough, the infrared receiving tube remains in the off state, the module output is at high level, and the indicator LED remains off. When the detected object appears within the detection range, the infrared light is reflected back with sufficient intensity, the infrared receiving tube saturates, the module output goes low, and the indicator LED lights up. The corresponding schematic diagram is shown in Figure 6.3-1. The AO output is the voltage directly output by the infrared tracking sensor, so it is analog. The DO is the high/low level output after voltage comparison by the LM393, so it is digital. For the specific principle, see section 3.3 View Materials in the Photoresistor Light Sensor chapter.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_IRtracking.c and bsp_IRtracking.h, and the folder name to IRtracking.
Write Code
Write the following in the bsp_IRtracking.c file:
#include "bsp_IRtracking.h"
#include "stdio.h"
esp_adc_cal_characteristics_t *adc_chars;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: IRtracking_GPIO_Init
* Function Description: Infrared tracking module pin initialization
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes:
******************************************************************/
void IRtracking_GPIO_Init(void)
{
gpio_config_t DO_config = {
.pin_bit_mask = (1ULL<<DO_GPIO_PIN), //Configure pins
.mode =GPIO_MODE_INPUT, //Input mode
.pull_up_en = GPIO_PULLUP_DISABLE, //Disable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, //Disable pull-down
.intr_type = GPIO_INTR_DISABLE //Disable pin interrupt
};
gpio_config(&DO_config);
adc1_config_width(width);// 12-bit resolution
//ADC_ATTEN_DB_0: indicates reference voltage is 1.1V
//ADC_ATTEN_DB_2_5: indicates reference voltage is 1.5V
//ADC_ATTEN_DB_6: indicates reference voltage is 2.2V
//ADC_ATTEN_DB_11: indicates reference voltage is 3.3V
adc1_config_channel_atten( channel,atten);// Set channel 0 and 3.3V reference voltage
// Allocate memory
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
// Initialize ADC characteristics so that it can correctly calculate conversion results and compensation factors
esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
}
/******************************************************************
* Function Name: Get_ADC_Value
* Function Description: Calculate the average of ADC values and output
* Function Parameters: num number of acquisitions
* Function Return: The corresponding scanned ADC value
* Author: LC
* Notes: None
******************************************************************/
unsigned int Get_ADC_Value(unsigned int num)
{
unsigned int Data=0;
int i = 0;
for( i = 0; i < num; i++ )
{
Data += adc1_get_raw(channel);
delay_ms(1);
}
Data = Data/num;
return Data;
}
/******************************************************************
* Function Name: Get_DO_Num
* Function Description: Read the sensor recognition status
* Function Parameters: None
* Function Return: 1=recognized as black 0=not recognized as black
* Author: LC
* Notes: The threshold for recognizing black can be adjusted via the adjustable resistor on the module
******************************************************************/
unsigned char Get_DO_Num(void)
{
if( IR_DO == 1 )//LED off, recognized as black
{
return 1;
}
else//LED on, not recognized as black
{
return 0;
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Write the following in the bsp_IRtracking.h file:
#ifndef _BSP_IRTRACKING_H_
#define _BSP_IRTRACKING_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_adc/adc_cali.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#define DEFAULT_VREF 1100 //Default reference voltage, unit: mV
#define channel ADC_CHANNEL_0 // ADC measurement channel
#define width ADC_WIDTH_BIT_12 // ADC resolution
#define atten ADC_ATTEN_DB_11 // ADC attenuation
#define unit ADC_UNIT_1 // ADC1
#define AO_GPIO_PIN 1
#define DO_GPIO_PIN 2
#define IR_DO gpio_get_level(DO_GPIO_PIN)
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void IRtracking_GPIO_Init(void);//Initialization
unsigned int Get_ADC_Value(unsigned int num);//Read AO value
unsigned char Get_DO_Num(void);//Read DO value
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
At this point, the porting is complete. Please proceed to section 6.5 for porting verification.
Porting Verification
Enter the following code in main.c:
#include <stdio.h>
#include "bsp_IRtracking.h"
void app_main(void)
{
IRtracking_GPIO_Init();
printf("IRtracking demo start\r\n");
while(1)
{
printf("AO = %d\r\n", Get_ADC_Value(20) );
printf("DO = %d\r\n",Get_DO_Num() );
delay_ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
Power-on example:
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.