Few days ago, I have got some of the component that I have ordered online. With that, I'm going to make some improvements on my water dispenser that I have designed in my previous blogpost #8 Controlling water pump using PSoC6 .
I have replaced the 12 NPN transistor that I used in my previous blogpost with a N-channel MOSFET and also added a water sensor to the water dispenser to detect the water level.
BOM: PSoC6 WiFi-BT Prototyping Kit, a water pump, two 18650 battery, 18650 battery socket, 2 N-channel MOSFET, a push button, a buzzer, a red led, a green LED, 6x 10k ohm potentiometers, some jumper wires, a bread board, a water pump and hose, a 1.5L water bottle, a 500ml water bottle, bendable steel wire, a plastic tray, hot glue gun and glue sticks.
GitHub: https://github.com/wanfp97/PSoC6-water-sensor-calibration
https://github.com/wanfp97/PSoC6-water-sensor-calibration
Schematic Diagram:
Device Configurator setting:
red : strong drive input buffer off, Initial Drive State Low
buzzer : strong drive input buffer off, Initial Drive State Low
wtr_sense : strong drive input buffer on, Initial Drive State High
button : strong drive input buffer on, Initial Drive State Low
water_pump: strong drive input buffer off, Initial Drive State Low
green : strong drive input buffer off, Initial Drive State Low
Coding:
#include "cy_pdl.h" // include Peripheral Driver Library #include "cyhal.h" // include Hardware Abstraction Layer library #include "cycfg.h" // include device configurator library /******************************************************************************* * Function Prototypes ********************************************************************************/ static void button_intr_handler(void *handler_arg, cyhal_gpio_event_t event); /******************************************************************************* * Global Variables ********************************************************************************/ volatile bool button_intr_flag = false; // Boolean type variable to store the interrupt flag int main(void) { init_cycfg_all(); // configure pins as done in Device Configurator __enable_irq(); // enable interrupt cyhal_gpio_register_callback(button_HAL_PORT_PIN, button_intr_handler, NULL); // assigning isr handler for button cyhal_gpio_enable_event(button_HAL_PORT_PIN, CYHAL_GPIO_IRQ_RISE, 1u, true); // interrupt on rising edge (button pressed) for (;;) { if (true == button_intr_flag) // if interrupt happens (button pressed) { button_intr_flag = false; // clear interrupt flag for further interrupt if(Cy_GPIO_ReadOut(water_pump_PORT, water_pump_NUM)==0UL) { Cy_GPIO_Write(green_PORT, green_NUM, 1UL); // green LED on Cy_GPIO_Write(water_pump_PORT, water_pump_NUM, 1UL); // on water pump } else { Cy_GPIO_Write(green_PORT, green_NUM, 0UL); // green LED off Cy_GPIO_Write(water_pump_PORT, water_pump_NUM, 0UL); // off water pump } } else; if(0UL == Cy_GPIO_Read(wtr_sense_PORT, wtr_sense_NUM)) { Cy_GPIO_Write(red_PORT, red_NUM, 1UL); // red LED on Cy_GPIO_Write(buzzer_PORT, buzzer_NUM, 1UL); // buzzer on } else { Cy_GPIO_Write(red_PORT, red_NUM, 0UL); // red LED off Cy_GPIO_Write(buzzer_PORT, buzzer_NUM, 0UL); // buzzer off } } } /******************************************************************************* * Function Name: button_intr_handler ******************************************************************************** * Summary: * GPIO interrupt handler. * * Parameters: * void *handler_arg (unused) * cyhal_gpio_irq_event_t (unused) * *******************************************************************************/ static void button_intr_handler(void *handler_arg, cyhal_gpio_irq_event_t event) { button_intr_flag = true; }
Explanation of the circuit:
Water sensor calibration:
The high output for the red led and the buzzer can later on be modify to use to send the stopping signal to the water dispenser to stop it when the water level is high to prevent overflow.