Skip to content

In this application, when water is detected, a PIC18F56Q24 will drive a Buzzer Click board and light up an 4x4 NeoPixel array. The onboard button is used to cycle through output modes: Buzzer only, NeoPixel only, or both Buzzer and NeoPixel.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic18f56q24-mtch9010-buzzer-neopixel-mplab-mcc

Repository files navigation

Microchip Technologies Inc.

Water Detection and Response System Using PIC18F56Q24 Microcontroller and MTCH9010 Liquid Detector

This demo features a liquid detection and response system using the PIC18F56Q24 microcontroller and the MTCH9010 liquid detection device. When liquid is detected, the system triggers real-time audio and visual alerts via the 4x4 RGB Click and Buzz 2 Click boards™.

Core Independent Peripherals (CIPs) on the PIC18F56Q24 are used to drive the output logic for each Click board, enabling efficient and responsive operation. The MTCH9010 supports both capacitive and conductive sensing modes for fast, reliable detection with minimal firmware complexity.

This demonstration highlights how the MTCH9010 and a host MCU can deliver efficient, event-driven responses in embedded applications.

Related Documentation

Software Used

Hardware Used

MTCH9010 Configuration

In this demo, the MTCH9010 functions as the system’s dedicated liquid detection front end. Its role is to sense water presence and signal the host MCU through its DETECT output. When liquid is detected, this digital output goes HIGH, triggering an interrupt on the PIC18F56Q24, which then handles the appropriate system response based on the active output mode.

What sets the MTCH9010 apart is that detection is handled entirely in hardware. There is no need for analog tuning, firmware development, or signal processing on the MCU side. This simplifies integration and offloads detection logic from the main application processor.

The MTCH9010 supports both capacitive and conductive sensing modes, making it flexible for a wide range of use cases, from drip detection to surface coverage. It also offers two configuration workflows depending on system complexity:

  • Basic mode is configured using slide switches on the evaluation kit and requires no firmware or software

  • Advanced mode uses a UART connection and allows customization through either a command-line interface or GUI-based tools

In this demo, the device is operating in Basic mode with default slide switch settings. While detection thresholds were not fine-tuned, the setup demonstrates how easily the MTCH9010 can be dropped into a system and used to trigger hardware-driven behavior without burdening the MCU.

For more details on advanced configuration options, sensing modes, and deployment workflows, refer to the Getting Started with the MTCH9010 Evaluation Kit.


MTCH9010 Eval Kit

System Overview and Peripheral Setup

This system uses Core Independent Peripherals (CIPs) and hardware interrupts to manage system behavior directly in hardware. By shifting control away from the CPU, it enables fast and predictable responses without relying on software execution.

The PIC18F56Q24 supports three user-selectable output modes that determine how it responds when a liquid detection signal is received from the MTCH9010.

The output modes are:

typedef enum {
    BUZZ_ONLY,
    RGB_ONLY,   
    BUZZ_RGB    
} MODES; 

MODES WaterDetectState = BUZZ_ONLY;
  • BUZZ_ONLY: Activates only the buzzer, using the MIKROE BUZZ 2 Click board to generate sound
  • RGB_ONLY: Activates only the LED animation, using the MIKROE 4x4 RGB Click board to control the RGB LEDs
  • BUZZ_RGB: Activates both the buzzer and the LED animation simultaneously

The system starts in BUZZ_ONLY mode. The onboard switch on the PIC18F56Q24 Curiosity Nano cycles through modes and the system responds to liquid detection based on the active mode.


System Overview:

Liquid detection from the MTCH9010 triggers a hardware interrupt, allowing the system to respond with minimal latency. The PIC18F56Q24 supports multiple interrupt sources. For this demo, two are used:

  • Interrupt-on-Change (IOC): Triggered by the MTCH9010’s DETECT output, connected to a general-purpose input pin

  • External Interrupt 2 (INT2): Triggered by the onboard switch

Both of these interrupt sources must be mapped to physical pins using Peripheral Pin Select (PPS) in MCC Melody.

Note: The PPS assignments for these interrupts will be shown later, in the MCC configuration section.

Once the pins are assigned, MCC Melody generates setup functions that let you attach your custom interrupt handlers to each source. These are typically called in main.c:

DETECT_SetInterruptHandler(DETECT_PIN);  // Assigns your handler to the IOC event
INT2_SetInterruptHandler(SWITCH);        // Assigns your handler to the INT2 event

These setup functions assign your handler to an internal function pointer that is called by the corresponding interrupt service routine (ISR). When the interrupt occurs, the handler function (e.g., DETECT_PIN() or SWITCH()) is automatically executed.


Main Loop Overview:

The main.c loop continuously checks whether the WaterDetected flag has been set by the interrupt handler. This flag indicates that the MTCH9010 has detected liquid. When set, the system evaluates the current output mode and executes the corresponding response.

while (1) {
        if (WaterDetectState == RGB_ONLY || WaterDetectState == BUZZ_RGB) {
            CheckAndRunRGBDemo();
        }
    }
}

Peripheral Setup

4x4 RGB Click: The 4x4 RGB Click uses WS2812 LEDs, which require precisely timed digital pulses to distinguish between ‘0’ and ‘1’ bits. Because of their strict timing requirements, this design uses synchronized hardware peripherals including SPI, CLC, CCP, and Timer to generate accurate waveforms without relying on software-based delays.

SPI1_Host Configuration:

In MCC Melody, SPI1_Host acts as the interface for setting up SPI communication from the host side. It enables key behavior like data sampling, Clock mode, and links directly to the SPI PLIB (Peripheral Library), which provides the low-level driver functions used in your project. While not used directly in code, it’s required to generate the correct SPI setup and link the configuration to the PLIB layer.

  • Config Name: Host_Config
  • Requested Speed (kHz): 800000
  • Mode: Mode 1
  • Data Input Sampled At: Middle
  • SPI Host PLIB Selector: SPI1


SPI1 Configuration

SPI1 Configuration:

To drive the WS2812 LEDs, RGB color data is encoded into a specific pulse-width format that SPI transmits at a consistent rate. With the SPI and CLC modules configured, sending data to the 4x4 RGB Click is handled by simply writing bytes in GRB order using the MCC-generated SPI API.

  • Mode: Host
  • SPI Mode: Mode 1
  • Input Data Sampled At: Middle
  • Clock Source Selection: TMR2_Postscaled


SPI1 Configuration


Capture/Compare/PWM Configuration:

WS2812 LEDs interpret data based on pulse width. In this design, the CCP module generates a stable PWM signal that helps establish precise timing for generating the short and long pulses used to represent ‘0’ and ‘1’ bits.

  • TMR2 Dependency Selector: TMR2
  • CCP Mode: PWM
  • Slect Timer: Timer 2
  • Duty Cycle 60%


CCP Configuration


Timer2 (TMR2) Configuration:

To maintain proper synchronization between data transmission and waveform timing, Timer 2 is used as a shared clock source for both SPI and PWM. This ensures the PWM signal remains aligned with the SPI data stream, enabling accurate generation of WS2812-compatible outputs.

  • Control Mode: Roll over pulse
  • Clock Source: FOSC/4
  • Polarity: Rising Edge
  • Prescaler & Postscaler: 1:1
  • Requested Peroid: 625 ns


TMR2 Configuration


Configurable Logic Cell (CLC) Configuration:

The CLC combines the SPI data stream with a variable-width PWM signal to produce the precise high pulses required by WS2812 LEDs. For each SPI bit, the PWM sets the pulse width: short for a 0 and long for a 1, ensuring the output matches WS2812 timing requirements.


CLC2 Configuration


Driving the 4x4 RGB Click via SPI

The functions used to control the 4x4 RGB Click are defined in NeoPixelArray.c

The NeoPixelArray() function can be used to send data to individual NeoPixels. It writes a single LED’s GRB color over SPI using byte-level transfers. This is useful for basic lighting effects such as static colors or simple status indicators.

void NeoPixelArray(uint8_t r, uint8_t g, uint8_t b) {
    SPI1_ByteExchange(g);
    SPI1_ByteExchange(r);
    SPI1_ByteExchange(b);
}

For this demo, the NeoPixel_DiagonalGradientBlue() function creates a diagonal blue gradient animation by calculating brightness values for each pixel, storing them in a frame buffer, and sending the full frame to the LED array using SPI1_BufferWrite().

SPI1_BufferWrite(frameBuffer, sizeof (frameBuffer));

Buzz 2 Click: The Buzz 2 Click is controlled using two PWM modules, a CLC, and a GPIO input from the MTCH9010. One PWM generates the tone frequency, while the other modulates the buzzer on and off. The CLC combines both signals, using the MTCH9010’s DETECT output as a gating input. The final signal is then routed to the buzzer through a dedicated output pin.


Pulse Width Modulator(s) (PWM) Configuration:

One PWM drives the buzzer’s 4 kHz resonance tone, while the other modulates when the tone is active. Together, they control both the sound and activation pattern of the Buzz 2 Click. The PWMs are configured as follows:

4 kHz Resonance Tone – PWM Configuration

  • Clock Source: LFINTSOC
  • Clock Prescaler: No prescale
  • Requested Frequency: 4 kHz


PWM1 Configuration


On/Off Modulation – PWM Configuration
  • Clock Source: LFINTOSC
  • Prescaler: No Prescale
  • Output Frequency: 0.0005 kHz


PWM2 Configuration


Configurable Logic Cell (CLC) Configuration:

The CLC combines the two PWM signals (tone and on/off modulation) with the DETECT signal from the MTCH9010.


CLC1 Configuration


Mode Control:

To determine which outputs are active, the system uses a simple state machine controlled by the SWITCH() function. This function is called whenever the onboard switch is pressed.

void SWITCH(void) {
    if (WaterDetectState == BUZZ_ONLY) {
        WaterDetectState = RGB_ONLY;
        CLC1_Disable();
    } else if (WaterDetectState == RGB_ONLY) {
        WaterDetectState = BUZZ_RGB;
        CLC1_Enable();
    } else {
        WaterDetectState = BUZZ_ONLY;
        CLC1_Enable();
    }
}

Hardware and GPIO Settings:

This demo is built on the PIC18F56Q24 Curiosity Nano, mounted on the Curiosity Nano Base for Click Boards™, which includes three mikroBUS™ sockets. Each socket follows a fixed pin layout, making it easy to route signals from the MCU to the connected Click boards using Peripheral Pin Select (PPS). While the base has three available sockets, this demo only uses two: one for the RGB Click board and one for the Buzzer Click board.

  • The 4x4 RGB Click is installed in Socket 1, with its data line connected to RA4, driven by CLC2 through PPS

  • The Buzz 2 Click is installed in Socket 2, with its PWM input connected to RF6, driven by CLC1 via PPS


CNANO Base


All routing is handled internally using Peripheral Pin Select (PPS) so no jumper wires are needed.

  • RA0 receives the DETECT signal from the MTCH9010 and is configured for Interrupt-on-Change (IOC)

  • RF3 is tied to the onboard switch, configured as External Interrupt 2 (INT2) to cycle output modes


Pin Summary:

Pin Function Module Purpose Interrupt Source Custom Name
RA0 CLCIN0 CLC1 MTCH9010 DETECT output (rising edge) IOC (internal) DETECT
RA4 CLC2OUT CLC2 WS2812 data output to RGB Click (Socket 1) None DATA
RF6 CLC1OUT CLC1 Buzzer signal to Buzz 2 Click (Socket 2) None BUZZER
RF3 INT2 EXT_INT Onboard switch to toggle between output modes (falling edge) External Interrupt 2 (external) SW0

The screen capture below shows the MCC configuration for External Interrupt 2 (INT2), which is mapped to the onboard switch on the Curiosity Nano. This interrupt is used to cycle through output modes when the onboard switch is pressed.


External Interrupt


Operation

The GIF below shows the system responding to a water detection event. When the MTCH9010 detects water, its DETECT output goes high, triggering the RGB animation on the 4x4 RGB Click to provide a visual alert.
MTCH9010 Demo GIF


The waveform below shows how the system drives the Buzz 2 Click when water is detected. When the MTCH9010’s `DETECT` output goes high, the buzzer output begins toggling to produce the modulated tone.


Waveform Capture

Summary

This demo highlights how the MTCH9010 and PIC18F56Q24 work together to create a responsive, low-power liquid detection system. The MTCH9010 handles detection entirely in hardware, while the PIC18F56Q24 uses Core Independent Peripherals such as CLCs, PWMs, SPI, and Timers to drive audio-visual alerts. With interrupt-based signaling and no need for complex firmware, the system responds quickly to liquid events while keeping CPU overhead low.

About

In this application, when water is detected, a PIC18F56Q24 will drive a Buzzer Click board and light up an 4x4 NeoPixel array. The onboard button is used to cycle through output modes: Buzzer only, NeoPixel only, or both Buzzer and NeoPixel.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published