Capacitive Sensor Interfacing with ESP32

Overview of Capacitive Sensor

 

Capacitive Sensor
Capacitive Sensor

 

  • Capacitive sensors can detect the presence or absence of solid or liquid objects without any physical contact.
  • It is an electronic device and emits an electrical field, it will change the property of capacitance based on a change in the electrical field around the active face of the sensor.
  • In solid materials that can be detected by a capacitor sensor are paper, plastic, glass, cloth, and wood. 
  • In liquids materials that can be detected by a capacitor sensor are oil, paint, water, etc.
  • The capacitive sensor operates on +12V to +24V AC or DC power supply.

 

Capacitive Sensor Hardware Connection with ESP32

Capacitive Sensor Hardware Connection with ESP32
ESP32 interfacing with Capacitive Sensor

 

 

Capacitive Proximity Sensor Detection using ESP32

Let’s write the code for the capacitive sensor to detect solid and liquid objects using ESP32.

Sketch for Capacitive Sensor using ESP32

const int SENSOR_OUTPUT_PIN = 16;  /* Capacitive sensor O/P pin */

void setup() {
  pinMode(SENSOR_OUTPUT_PIN, INPUT);
  Serial.begin(9600);             /* Define baud rate for serial communication */
  delay(1000);                    /* Power On Delay */
}

void loop() {
  int sensor_output;
  sensor_output = digitalRead(SENSOR_OUTPUT_PIN);
  if( sensor_output == HIGH)  {
    Serial.print("Object detected\n\n"); 
  }
  delay(100);
}

 

Capacitive Sensor Output on Serial Monitor

 

Let’s understand the code

The sketch begins by defining the ESP32 pin to which the capacitive sensor is connected.

const int SENSOR_OUTPUT_PIN = 16;  /* Capacitive sensor O/P pin */

 

In setup function

We have initiated the serial communication with a 9600 Baud rate and set the sensor pin as an INPUT mode.

void setup() {
  pinMode(SENSOR_OUTPUT_PIN, INPUT);
  Serial.begin(9600);             /* Define baud rate for serial communication */
  delay(1000);                    /* Power On Delay */
}

 

In loop function

In the loop, detect the sensor pin output and print on the serial monitor.

void loop() {
  int sensor_output;
  sensor_output = digitalRead(SENSOR_OUTPUT_PIN);
  if( sensor_output == HIGH)  {
    Serial.print("Object detected\n\n"); 
  }
  delay(100);
}

Components Used

ESP32 WROOM
WiFi Development Tools - 802.11 ESP32 General Development Kit, embeds ESP32-WROOM-32E, 4MB flash.
1

Downloads

ESP32_Capacitive_Sensor Download
Ad