element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
At The Core Design Challenge
  • Challenges & Projects
  • Design Challenges
  • At The Core Design Challenge
  • More
  • Cancel
At The Core Design Challenge
Blog Infineon Beverage Dispenser # 3 - Adding CapSense Buttons
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join At The Core Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: guillengap
  • Date Created: 30 Mar 2023 5:27 AM Date Created
  • Views 1571 views
  • Likes 9 likes
  • Comments 5 comments
  • infineon
  • PSoCTm︎ 62 MCU
  • element14
  • modustoolbox
  • Embedded Systems
  • microcontroller
  • challenge
  • arm
Related
Recommended

Infineon Beverage Dispenser # 3 - Adding CapSense Buttons

guillengap
guillengap
30 Mar 2023
Infineon Beverage Dispenser # 3 - Adding CapSense Buttons

Table of Contents

  • Introduction
  • Getting Started
  • Adding CapSense Buttons
  • How To Configure The OLED Display
  • Device Assembly
  • Programming, Test and Troubleshooting
  • Summary

**********************************************************************************************************************

Schematic Diagram

Now it's time to modify the code from the previous chapter in order to experiment with the flow of water through the CapSense buttons and CapSense slider. Below I show you the schematic diagram:

image

How Does It Work?

First we create the project, just like we saw in the previous chapter. I have assigned the name of Infineon_Beverage_Dispenser

image

Next, we create the file "beverage_dispenser.c" where we will put our code. The global constants are:

led_state_t led_state_cur = LED_OFF;
cyhal_pwm_t pwm_led;
cyhal_pwm_t pwm_dispenser;

We initialize the output ports and the PWM source.

cy_rslt_t initialize_led(void)
{
    cy_rslt_t rslt;

    rslt = cyhal_pwm_init(&pwm_led, CYBSP_USER_LED, NULL);
    rslt = cyhal_pwm_init(&pwm_dispenser, P2_4, NULL); // added
    rslt = cyhal_gpio_init(P0_2, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON);
    rslt = cyhal_gpio_init(P0_3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON);

    if (CY_RSLT_SUCCESS == rslt)
    {
        rslt = cyhal_pwm_set_duty_cycle(&pwm_led,
                                        GET_DUTY_CYCLE(LED_MAX_BRIGHTNESS),
                                        PWM_LED_FREQ_HZ);
        if (CY_RSLT_SUCCESS == rslt)
        {
            rslt = cyhal_pwm_start(&pwm_led);
        }
        
    }

    if (CY_RSLT_SUCCESS == rslt)
    {
        led_state_cur = LED_ON;
    }

    return rslt;
}

The code that controls the PWM signal of the CapSense slider is the one shown below. Remember that this PWM signal comes out of port P2_4, and feeds water pump 1.

    if ((LED_ON == led_state_cur) || ((LED_OFF == led_state_cur) && (ledData->brightness > 0)))
    {
        cyhal_pwm_start(&pwm_led);
        uint32_t brightness = (ledData->brightness < LED_MIN_BRIGHTNESS) ? LED_MIN_BRIGHTNESS : ledData->brightness;
        uint32_t PWM_DUTY_CYCLE_DISPENSER = brightness;

        /* Drive the LED with brightness */
        cyhal_pwm_set_duty_cycle(&pwm_led, GET_DUTY_CYCLE(brightness),
                                 PWM_LED_FREQ_HZ);

        cyhal_pwm_set_duty_cycle(&pwm_dispenser, PWM_DUTY_CYCLE_DISPENSER, PWM_FREQUENCY); 
        cyhal_pwm_start(&pwm_dispenser); // dispenser

        led_state_cur = LED_ON;
    }

Finally, the code that controls the CapSense on and off buttons is used to control the water pump 2 (port P0_2).

    if ((led_state_cur == LED_OFF) && (ledData->state == LED_ON))
    {
        cyhal_pwm_start(&pwm_led);
        led_state_cur = LED_ON;
        ledData->brightness = LED_MAX_BRIGHTNESS;
        cyhal_gpio_write(P0_2, CYBSP_LED_STATE_OFF);
    }
    else if ((led_state_cur == LED_ON) && (ledData->state == LED_OFF))
    {
        cyhal_pwm_stop(&pwm_led);
        led_state_cur = LED_OFF;
        ledData->brightness = 0;
        cyhal_gpio_write(P0_2, CYBSP_LED_STATE_ON);
    }

The complete code of the "beverage dispenser.c" file is shown below:

 // Author: Guillermo Perez

/*******************************************************************************
* Header files includes
*******************************************************************************/
#include "beverage_dispenser.h"
#include "cybsp.h"
#include "cyhal.h"
#include <stdio.h>
#include <math.h>

/*******************************************************************************
* Global constants
*******************************************************************************/
#define PWM_LED_FREQ_HZ    (1000000lu)  /* in Hz */
#define GET_DUTY_CYCLE(x)    (100 - x)

#define DELAY_LONG_MS           (3000)   /* ADD 2 milliseconds */
/* PWM Frequency */
#define PWM_FREQUENCY (50u)

/*******************************************************************************
* Global constants
*******************************************************************************/
led_state_t led_state_cur = LED_OFF;
cyhal_pwm_t pwm_led;
cyhal_pwm_t pwm_dispenser;

/*******************************************************************************
* Function Name: update_led_state
********************************************************************************
* Summary:
*  This function updates the LED state, based on the touch input.
*
* Parameter:
*  ledData: the pointer to the LED data structure
*
*******************************************************************************/
void update_led_state(led_data_t *ledData)
{
    if ((led_state_cur == LED_OFF) && (ledData->state == LED_ON))
    {
        cyhal_pwm_start(&pwm_led);
        led_state_cur = LED_ON;
        ledData->brightness = LED_MAX_BRIGHTNESS;
        cyhal_gpio_write(P0_2, CYBSP_LED_STATE_OFF);
    }
    else if ((led_state_cur == LED_ON) && (ledData->state == LED_OFF))
    {
        cyhal_pwm_stop(&pwm_led);
        led_state_cur = LED_OFF;
        ledData->brightness = 0;
        cyhal_gpio_write(P0_2, CYBSP_LED_STATE_ON);
    }
    else
    {
    }

    if ((LED_ON == led_state_cur) || ((LED_OFF == led_state_cur) && (ledData->brightness > 0)))
    {
        cyhal_pwm_start(&pwm_led);
        uint32_t brightness = (ledData->brightness < LED_MIN_BRIGHTNESS) ? LED_MIN_BRIGHTNESS : ledData->brightness;
        uint32_t PWM_DUTY_CYCLE_DISPENSER = brightness;

        /* Drive the LED with brightness */
        cyhal_pwm_set_duty_cycle(&pwm_led, GET_DUTY_CYCLE(brightness),
                                 PWM_LED_FREQ_HZ);

        cyhal_pwm_set_duty_cycle(&pwm_dispenser, PWM_DUTY_CYCLE_DISPENSER, PWM_FREQUENCY); 
        cyhal_pwm_start(&pwm_dispenser); // dispenser

        led_state_cur = LED_ON;
    }
}

/*******************************************************************************
* Function Name: initialize_led
********************************************************************************
* Summary:
*  Initializes a PWM resource for driving an LED.
*
*******************************************************************************/
cy_rslt_t initialize_led(void)
{
    cy_rslt_t rslt;

    rslt = cyhal_pwm_init(&pwm_led, CYBSP_USER_LED, NULL);
    rslt = cyhal_pwm_init(&pwm_dispenser, P2_4, NULL); // added
    rslt = cyhal_gpio_init(P0_2, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON);
    rslt = cyhal_gpio_init(P0_3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON);

    if (CY_RSLT_SUCCESS == rslt)
    {
        rslt = cyhal_pwm_set_duty_cycle(&pwm_led,
                                        GET_DUTY_CYCLE(LED_MAX_BRIGHTNESS),
                                        PWM_LED_FREQ_HZ);
        if (CY_RSLT_SUCCESS == rslt)
        {
            rslt = cyhal_pwm_start(&pwm_led);
        }
        
    }

    if (CY_RSLT_SUCCESS == rslt)
    {
        led_state_cur = LED_ON;
    }

    return rslt;
}

/* [] END OF FILE */

The final step is to test and improve the project.

Assembling The Test Module

Below I show you the two mini water pumps with their respective hoses.These water pumps are powered from 3 to 6 VDC.

image

Two USB adapters will also be needed to power the water pumps. Please note that I only need the USB VCC and ground pins. In my case, this device was recycled from old hardware.

image

Finally, according to the schematic diagram we assemble the L298N driver, two 5V batteries, and two water containers. Below I show you the test module already assembled.

image

Test

Below I show you two tests carried out. In the first test I will use the CAPSENSE buttons to pump water into two bottles simultaneously. In the second test I will use the CAPSENSE Slider to pump water to the first bottle, here I will use PWM signal to control the speed of water flow.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Conclusion

  • Please note that I have fed the L298N driver with 5 VDC through the +12V pin, this has helped me to feed the water pumps with 3 VDC. Which is the reason? Experimenting, I found that feeding 5VDC through the +5V pin to the output I got 1.5 VDC, which is insufficient to work the water pumps.    

image

  • I have achieved my goal of using the CAPSENSE buttons and the CAPSENSE slider to pump water. This will help me to make corrections to the design and improve it. I just wanted to go ahead and test my ideas. In the next chapter I will test another module that I intend to integrate into my final design.
  • Sign in to reply

Top Comments

  • guillengap
    guillengap over 2 years ago in reply to Digimorf +1
    Smart observation! I'm trying to go step by step, and I don't know how all this will end, because every time I move forward I experience new things. Its night here! have a nice day.
  • Digimorf
    Digimorf over 2 years ago in reply to guillengap

    I know what you mean, when something catches your interest, the passion takes the controls, and the challenges of the project became personal Sweat smile

    Hundreds of times something starts from a little thing, from a very simple and nice idea, only because you like it. Then it becomes something incredible and everything changes. Wink

    Keep up the good work! 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • guillengap
    guillengap over 2 years ago in reply to javagoza

    Hi javagoza  I thought the same as you, and tried to separate the objects as much as possible! ... I have wood to make a simple piece of furniture, but all the time I'm facing new technical issues and I can't stop myself, because my curiosity is greater.

    Have a nice day!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • javagoza
    javagoza over 2 years ago

    Here suffering when I see the water so close to the electronic boards Cold sweat. I don't have such a good pulse. Good progress on the project.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • guillengap
    guillengap over 2 years ago in reply to Digimorf

    Smart observation! I'm trying to go step by step, and I don't know how all this will end, because every time I move forward I experience new things. Its night here! have a nice day. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Digimorf
    Digimorf over 2 years ago

    This is a great potential project! Any liquid mixer application can benefit from this project. You should design your own device from it. ThumbsupBlush

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube