8-Channel IR Line Follower Sensor Guide - UART Debugging with a Serial Terminal
1. Introduction
This tutorial uses a serial terminal to send UART commands and parse the commands returned by the sensor. This helps developers better understand how the sensor works over UART.
2. Wiring
This example uses a 8-Channel IR Line Follower Sensor Module connected to a USB-to-TTL module with Dupont wires. Connect the USB-to-TTL module to the PC.
Cross-connect the
TXandRXpins between the sensor and the USB-to-TTL module.
3. Principle
The UART communication protocol of the 8-channel line follower sensor supports four initialization modes, as shown below:
- Manual configuration:
0 - Auto-send level value:
1 - Auto-send analog value:
2 - Auto-send threshold value:
3
This tutorial focuses on manual mode. The serial terminal is used to parse the commands returned by the sensor. After the controller sends a read command, the sensor returns the corresponding data.
Related read commands:
1: Return the level value. The sensor returns 1 byte of data.
2: Read the analog value. The sensor returns data according to the UART protocol below.
3: Read the threshold value. The sensor returns data according to the UART protocol below.
After the operating mode is configured, it cannot be reconfigured directly. To change the mode, reset the sensor and initialize it again.
The 8-channel sensor UART protocol is shown below:
- Frame header:
0x55 0xAA(2 bytes) - Command: read command sent by the controller
- Data length: valid data length, excluding this byte
- Data:
Data0,Data1, ...,DataN - Checksum: calculated from all bytes except the 2-byte frame header
4. Command Parsing
4.1 Parse the Level Value
Send the 00 command first to enter manual configuration mode, then send 01 to read the level value.
After 01 is sent, the returned value is FF. Convert it to binary first:
1111 1111
The 8 binary bits, from low bit to high bit, indicate the high or low levels returned by sensors S1 through S8. FF means all eight sensors are returning high level at this moment. The indicator LED turns on for high level and turns off for low level. Several additional examples are shown below:
| Returned Data | Binary | Meaning |
|---|---|---|
FF |
1111 1111 |
All 8 sensors return high level. |
00 |
0000 0000 |
All 8 sensors return low level. |
C3 |
1100 0011 |
S3, S4, S5, and S6 return low level; the others return high level. |
D2 |
1101 0010 |
S2, S5, S7, and S8 return high level; the others return low level. |
4.2 Parse the Analog Value
Send 02 in the serial terminal to read the returned analog value command.
In this example, 02 is sent twice, and two commands are returned:
55 AA 02 10 F4 0D 26 0D 08 0B 67 0A D1 0B 57 0B 9F 0B 9F 0C A2
55 AA 02 10 0A 01 5C 02 04 01 56 0D 7A 0D 26 0C D6 0B D7 0C 9F
Using the first returned command as an example, parse the data according to the sensor UART communication protocol:
-
Frame header:
55 AA(fixed 2 bytes) -
Command:
02(the read command sent by the controller) -
Data length:
10(hexadecimal). This equals decimal16, which means theDatasection contains 16 bytes. -
Datasection (16 bytes):
F4 0D 26 0D 08 0B 67 0A D1 0B 57 0B 9F 0B 9F 0C
Split the data into 8 analog channels. Each channel uses 2 bytes as one ADC sample value. Merge each pair in little-endian order into a 16-bit decimal value, then convert it to the corresponding voltage as shown below:
| Channel | Transmitted Bytes (Low Byte + High Byte) | 16-bit Hexadecimal | Decimal ADC Value | Actual Voltage for 0-5 V (Reference) |
|---|---|---|---|---|
| Channel 1 | F4 0D |
0D F4 |
3572 |
(3572/65535)*5 ≈ 0.273 V |
| Channel 2 | 26 0D |
0D 26 |
3366 |
(3366/65535)*5 ≈ 0.257 V |
| Channel 3 | 08 0B |
0B 08 |
2824 |
(2824/65535)*5 ≈ 0.216 V |
| Channel 4 | 67 0A |
0A 67 |
2663 |
(2663/65535)*5 ≈ 0.203 V |
| Channel 5 | D1 0B |
0B D1 |
3025 |
(3025/65535)*5 ≈ 0.231 V |
| Channel 6 | 57 0B |
0B 57 |
2903 |
(2903/65535)*5 ≈ 0.221 V |
| Channel 7 | 9F 0B |
0B 9F |
2975 |
(2975/65535)*5 ≈ 0.227 V |
| Channel 8 | 9F 0C |
0C 9F |
3231 |
(3231/65535)*5 ≈ 0.246 V |
- Checksum:
A2. For verification, remove the frame header, add the remaining bytes, take the low 8 bits, then take the one's complement.
4.3 Parse the Threshold Value
Send 03 in the serial terminal to read the returned threshold value command.
After 03 is sent, the returned UART command is shown below:
55 AA 03 10 00 08 00 08 00 08 00 08 00 08 00 08 00 08 00 08 AC
-
Frame header (2 bytes):
55 AA, which meets the protocol requirement. -
Command (1 byte):
03, which is the response to the controller read command. -
Data length (1 byte):
10. This equals decimal16, which means theDatasection contains 16 bytes, excluding this byte. -
Datasection (16 bytes):
00 08 00 08 00 08 00 08 00 08 00 08 00 08 00 08
The Data section contains 8 groups of low byte + high byte, corresponding to 8 parameters. Split the 16-byte Data section into 8 groups of 2 bytes. Combine each group as low byte + high byte into a 16-bit hexadecimal value, then convert it to the decimal threshold value:
| Channel | Data Bytes (Low Byte + High Byte) | Combined 16-bit Hexadecimal Value | Decimal Threshold Value |
|---|---|---|---|
| Group 1 | 00 08 |
0x0800 |
2048 |
| Group 2 | 00 08 |
0x0800 |
2048 |
| Group 3 | 00 08 |
0x0800 |
2048 |
| Group 4 | 00 08 |
0x0800 |
2048 |
| Group 5 | 00 08 |
0x0800 |
2048 |
| Group 6 | 00 08 |
0x0800 |
2048 |
| Group 7 | 00 08 |
0x0800 |
2048 |
| Group 8 | 00 08 |
0x0800 |
2048 |
- Checksum (1 byte):
AC.