Engaged in maker development with a 12-year-old comrade: How to drive various types of sensors?

table of Contents

1. Digital output type sensor

2. Digital input type sensor

3. Analog voltage type sensor

4. Analog current type sensor

5. Protocol type sensor


By chance, I met a 12-year-old young comrade on the Internet and systematically guided his creators to develop thinking and skills from scratch.

Project column: https://blog.csdn.net/m0_38106923/category_11097422.html


The sensor detects the state of the real object and converts and outputs it into an electrical signal. This electrical signal can be in various forms such as voltage, current, and pulse.

Sensors can be classified according to these output types, which mainly include: digital output type sensors, digital input type sensors, analog voltage type sensors, analog current type sensors, and protocol type sensors .

Note: This article focuses on applications, and more complex driving principles will be explained in subsequent projects.

1. Digital output type sensor

The digital output type sensor outputs high and low level signals to indicate the detection status. For example, when a human body sensor passes by, the data port will output a high level, and when there is no one, it will output a low level.

To drive a digital output type sensor, the controller port needs to be configured as a digital input mode to detect changes in sensor output voltage. This is also the principle for driving key components.

2. Digital input type sensor

Digital input type sensors require the controller to output high and low levels to achieve switch control. For example: LED lights, when the controller outputs a high level of 3.3V~5V, the LED is off, and when the controller outputs a low level, the LED is on.

To drive a digital input type sensor, the controller port needs to be configured as a digital output mode, and the output meets the high and low levels of the driving capability to realize the drive.

3. Analog voltage type sensor

The analog voltage type sensor will output the analog voltage within the specified range as the state of the detected object changes. For example, a capacitive soil moisture sensor will output a voltage of 0~3V as the soil moisture changes. Insert the soil moisture sensor into dry sand, and the output will be about 3V. Insert the wet sand and the output will be about 1.5V. The plug is very humid and the output is about 0V voltage.

To drive an analog voltage type sensor, you need to configure the controller port to ADC mode to collect the analog voltage data output by the sensor.

4. Analog current type sensor

The analog current type sensor will output the analog current within the specified range as the state of the detected object changes. For example: 4-20mA current temperature sensor, assuming the temperature range of the temperature sensor is -200~500℃, when the temperature is -200℃, the sensor output current is about 0mA, when the temperature is 500℃, the sensor output current About 20mA.

It should be noted that most controllers cannot directly input current signals. It is necessary to convert the analog current signal into an analog voltage signal of a suitable range, configure the controller port to ADC mode, and collect the analog voltage data output by the sensor.

5. Protocol type sensor

The above four types of sensors can directly collect and output electrical signals, and developers need to convert them to get the sensing results.

After the protocol type sensor collects the data, it directly converts the electrical signal into the data result and stores it in the register. Developers do not need to program the conversion by themselves. They only need to read the communication bus and access the register according to the relevant protocol to read the result.

Commonly used protocol type sensors mainly include: UART communication bus, I2C communication bus, and SPI communication bus.

The protocol type sensor is more complicated to study deeply. Considering the relatively weak foundation of maker, it is recommended to apply practice first, and then study the principle after proficient use.

5.1, UART communication bus

Universal Asynchronous Receiver/Transmitter, usually called UART. UART is a universal serial data bus used for asynchronous communication. This bus has two-way communication and can realize full-duplex transmission and reception .

UART communication requires three interfaces to be connected, as shown below:

  • TXD: data transmission port;
  • RXD: data receiving port;
  • GND: Power ground.

When connecting, you need to cross-connect the controller TXD, RXD and the sensor TXD, RXD , the connection effect is as follows:

For example: HuskyLens AI vision sensor, based on UART communication, the connection effect is as follows:

After the connection is successful, you can use the controller to drive the HuskyLens AI vision sensor.

5.2, I2C communication bus

The I2C bus (Inter-Integrated Circuit) is a simple, bidirectional, two-wire synchronous serial bus that only needs two wires to realize data communication, as shown below:

  • SDA: Serial data line to realize data communication;
  • SCL: Serial clock line to achieve timing synchronization.

When connecting, you need to connect the controller SDA, SCL and the sensor SDA, SCL correspondingly , the connection effect is as follows:

For example, to drive the BH1750 ambient light sensor, you need to connect the controller SDA, SCL and the ambient light sensor SDA, SCL correspondingly.

After the connection is successful, the controller can be used to drive the ambient light sensor.

5.3, SPI communication bus

SPI is the abbreviation of Serial Peripheral Interface. It is a high-speed, full-duplex, and synchronous communication bus. It works in a master-slave mode. This mode usually has a master device and one or more slave devices. At least 4 wires are required. In fact, 3 wires are also available (for one-way transmission), as shown below:

  • SDI/MISO: main device data input, slave device data output;
  • SDO/MOSI: master device data output, slave device data input;
  • SCLK: clock signal, generated by the master device;
  • CS/SS: Chip select, slave device enable signal, controlled by master device.

When connecting, you need to connect the controller SDI/MISO, SDO/MOSI, SCLK and the sensor SDI/MISO, SDO/MOSI, SCLK correspondingly , the master device CS/SS and the slave device CS/SS are respectively connected correspondingly , the connection effect is as follows Show:

For example, to drive the ADXL345 three-axis acceleration sensor, connect the controller SDI/MISO, SDO/MOSI, SCLK, CS/SS and the three-axis acceleration sensor SDI/MISO, SDO/MOSI, SCLK, CS/SS correspondingly

After the connection is successful, the controller can be used to drive the three-axis acceleration sensor.