Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Andre Pereira da Silva
Published © GPL3+

Automated Water Level

This project was made to monitor the level of any liquid that have water.

IntermediateShowcase (no instructions)20 hours1,359
Automated Water Level

Things used in this project

Hardware components

FRDM Board
NXP FRDM Board
FRDM-K82F
×1
General Purpose Quad Op-Amp
Texas Instruments General Purpose Quad Op-Amp
×1
5 mm LED: Red
5 mm LED: Red
four white led were used
×4
Arduino header connector
×4
screw connector 2.5mm 3pin
×1
Resistor 1M ohm
Resistor 1M ohm
×4
Resistor 10k ohm
Resistor 10k ohm
×2
Resistor 330 ohm
Resistor 330 ohm
For leds usage.
×4

Software apps and online services

Microsoft Windows 10 Home

Hand tools and fabrication machines

0.7mm drill tap
Dremel milling machine

Story

Read more

Custom parts and enclosures

Schematic

This is the PDF schematic

Schematics

KICAD files

He we have KICAD, free and open source software, files.

Kicad files

Kicad project

Kicad net file

Kicad sch

Code

modified file "led output"

C/C++
This file can be found on SDK2.0 archive at SDK2.0->boards->frdmk82f->driver_examples->gpio->led_output.
In KDS_V3 uoy need to import the project and find those files to change, I mean, "pin_mux, board and gpio_led_output"
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"

#include "clock_config.h"
#include "pin_mux.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define BOARD_LED_GPIO BOARD_LED_RED_GPIO
#define BOARD_LED_GPIO_PIN BOARD_LED_RED_GPIO_PIN

// relay output
#define RL1 BOARD_rl1_GPIO
#define RL1_PIN BOARD_rl1_PIN
#define RL2 BOARD_rl2_GPIO
#define RL2_PIN BOARD_rl2_PIN

// led definitions.
#define LED1 BOARD_LED1_GPIO
#define PIND1 BOARD_LED1_PIN

#define LED2 BOARD_LED2_GPIO
#define PIND2 BOARD_LED2_PIN

#define LED3 BOARD_LED3_GPIO
#define PIND3 BOARD_LED3_PIN

#define LED4 BOARD_LED4_GPIO
#define PIND4 BOARD_LED4_PIN

// Level definitions
#define LEVEL1  BOARD_LEVEL1_GPIO
#define PINB1 BOARD_LEVEL1_PIN

#define LEVEL2  BOARD_LEVEL2_GPIO
#define PINB2 BOARD_LEVEL2_PIN

#define LEVEL3  BOARD_LEVEL3_GPIO
#define PINB3 BOARD_LEVEL3_PIN

#define LEVEL4  BOARD_LEVEL4_GPIO
#define PINB4 BOARD_LEVEL4_PIN

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
/*!
 * @brief delay a while.
 */
void delay(void);

/*******************************************************************************
 * Variables
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/
void delay(void)
{
    volatile uint32_t i = 0;
    for (i = 0; i < 800000; ++i)
    {
        __asm("NOP"); /* delay */
    }
}

/*!
 * @brief Main function
 */
int main(void)
{
    /* Define the init structure for the output LED pin*/
    gpio_pin_config_t led_config =
    {
        kGPIO_DigitalOutput,
		0,
    };

    gpio_pin_config_t level_config =
    {
           kGPIO_DigitalInput,
		   1,
    };

    /* Board pin, clock, debug console init */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    /* Print a note to terminal. */
    PRINTF("\r\n GPIO Driver example\r\n");
    PRINTF("\r\n The LED is taking turns to shine.\r\n");

    /* Init output LED GPIO. */
    GPIO_PinInit(BOARD_LED_GPIO, BOARD_LED_GPIO_PIN, &led_config);

    GPIO_PinInit(LED1, PIND1, &led_config);
    GPIO_PinInit(LED2, PIND2, &led_config);
    GPIO_PinInit(LED3, PIND3, &led_config);
    GPIO_PinInit(LED4, PIND4, &led_config);

    GPIO_PinInit(LEVEL1, PINB1, &level_config);
    GPIO_PinInit(LEVEL2, PINB2, &level_config);
    GPIO_PinInit(LEVEL3, PINB3, &level_config);
    GPIO_PinInit(LEVEL4, PINB4, &level_config);

    while (1)
    {
    /*********************************************************************************
     *This part is to read the four levels of liquid and turns on the led accordantly*
     ********************************************************************************/
    	//reading level one and lit led one.

    	if(!GPIO_ReadPinInput(LEVEL1,PINB1)){
    		GPIO_WritePinOutput(LED1,PIND1,1);
    		delay();
    	}
    	else
    	{
    		GPIO_WritePinOutput(LED1,PIND1,0);
    		delay();
    	}

    	//reading level two ant lit the led two.
    	if(!GPIO_ReadPinInput(LEVEL2,PINB2)){
    	    GPIO_WritePinOutput(LED2,PIND2,1);
    	    delay();
    	}
    	else
    	{
    		GPIO_WritePinOutput(LED2,PIND2,0);
    		delay();
    	}

    	//reading level three and lit the led three.
    	if(!GPIO_ReadPinInput(LEVEL3,PINB3)){
    	  	GPIO_WritePinOutput(LED4,PIND4,1);
    	  	delay();
       	}
       	else
       	{
       		GPIO_WritePinOutput(LED4,PIND4,0);
       		delay();
      	}

    	//reading level four and lit the led four.
    	if(!GPIO_ReadPinInput(LEVEL4,PINB4)){
       		GPIO_WritePinOutput(LED3,PIND3,1);
       		delay();
       	}
      	else
       	{
      		GPIO_WritePinOutput(LED3,PIND3,0);
      		delay();
      	}




    	//
       // delay();
       // GPIO_TogglePinsOutput(LED1,1U << PIND1);
       // GPIO_TogglePinsOutput(BOARD_LED_GPIO, 1U << BOARD_LED_GPIO_PIN);

    }
}

Pin_mux modified

C/C++
I modified some lines in the code.
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "fsl_common.h"
#include "fsl_port.h"
#include "pin_mux.h"

/*******************************************************************************
 * Variables
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/
void BOARD_InitPins(void)
{
    /* Debug uart port mux config */
    /* Enable uart port clock */
    CLOCK_EnableClock(kCLOCK_PortC);
    CLOCK_EnableClock(kCLOCK_PortB);
    CLOCK_EnableClock(kCLOCK_PortD);
    /* Affects PORTC_PCR14 register */
    PORT_SetPinMux(PORTC, 14U, kPORT_MuxAlt3);
    /* Affects PORTC_PCR15 register */
    PORT_SetPinMux(PORTC, 15U, kPORT_MuxAlt3);

    port_pin_config_t pinConfig = {0};
    pinConfig.pullSelect = kPORT_PullUp;

    PORT_SetPinConfig(PORTB, 0U, &pinConfig);
    PORT_SetPinConfig(PORTB, 1U, &pinConfig);
    PORT_SetPinConfig(PORTB, 2U, &pinConfig);
    PORT_SetPinConfig(PORTB, 3U, &pinConfig);

    /* Led pin mux Configuration */
    //PORT_SetPinMux(PORTC, 8U, kPORT_MuxAsGpio);

    PORT_SetPinMux(PORTD, 0U, kPORT_MuxAsGpio);
    PORT_SetPinMux(PORTD, 1U, kPORT_MuxAsGpio);
    PORT_SetPinMux(PORTD, 2U, kPORT_MuxAsGpio);
    PORT_SetPinMux(PORTD, 3U, kPORT_MuxAsGpio);

    PORT_SetPinMux(PORTB, 0U, kPORT_MuxAsGpio);
    PORT_SetPinMux(PORTB, 1U, kPORT_MuxAsGpio);
    PORT_SetPinMux(PORTB, 2U, kPORT_MuxAsGpio);
    PORT_SetPinMux(PORTB, 3U, kPORT_MuxAsGpio);
}

board.h modified

C/C++
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _BOARD_H_
#define _BOARD_H_

#include "clock_config.h"
#include "fsl_gpio.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/*! @brief The board name */
#define BOARD_NAME "FRDM-K82F"

/*! @brief The UART to use for debug messages. */
#define BOARD_USE_UART
#define BOARD_DEBUG_UART_TYPE DEBUG_CONSOLE_DEVICE_TYPE_LPUART
#define BOARD_DEBUG_UART_BASEADDR (uint32_t) LPUART4
#define BOARD_DEBUG_UART_CLKSRC kCLOCK_Osc0ErClk
#define BOARD_DEBUG_UART_CLK_FREQ CLOCK_GetOsc0ErClkFreq()
#define BOARD_UART_IRQ LPUART4_IRQn
#define BOARD_UART_IRQ_HANDLER LPUART4_IRQHandler

#ifndef BOARD_DEBUG_UART_BAUDRATE
#define BOARD_DEBUG_UART_BAUDRATE 115200
#endif /* BOARD_DEBUG_UART_BAUDRATE */

/*! @brief The i2c instance used for i2c connection by default */
#define BOARD_I2C_BASEADDR I2C3

/*! @brief The Flextimer instance/channel used for board */
#define BOARD_FTM_BASEADDR FTM3
#define BOARD_FTM_CHANNEL 4U
#define BOARD_FTM_X_CHANNEL 4U
#define BOARD_FTM_Y_CHANNEL 5U
#define BOARD_FTM_PERIOD_HZ 100
#define BOARD_FTM_IRQ_HANDLER FTM3_IRQHandler
#define BOARD_FTM_IRQ_VECTOR FTM3_IRQn

/*! @brief The bubble level demo information */
#define BOARD_FXOS8700_ADDR 0x1C
#define BOARD_ACCEL_ADDR BOARD_FXOS8700_ADDR
#define BOARD_ACCEL_BAUDRATE 100
#define BOARD_ACCEL_I2C_BASEADDR I2C3

/*! @brief The TPM instance/channel used for board */
#define BOARD_TPM_BASEADDR TPM2
#define BOARD_TPM_CHANNEL 0U

/*! @brief The FlexBus instance used for board.*/
#define BOARD_FLEXBUS_BASEADDR FB

#define BOARD_TSI_ELECTRODE_CNT 2U

/*! @brief Indexes of the TSI channels for on board electrodes */
#define BOARD_TSI_ELECTRODE_1 11U
#define BOARD_TSI_ELECTRODE_2 12U

/*! @brief The SDHC instance/channel used for board */
#define BOARD_SDHC_BASEADDR SDHC

/*! @brief The CMP instance/channel used for board. */
#define BOARD_CMP_BASEADDR CMP1
#define BOARD_CMP_CHANNEL 3U

/*! @brief The i2c instance used for sai demo */
#define BOARD_SAI_DEMO_I2C_BASEADDR I2C0

/*! @brief The rtc instance used for rtc_func */
#define BOARD_RTC_FUNC_BASEADDR RTC

/*! @brief If connected the TWR_MEM, this is spi sd card */
#define BOARD_SDCARD_CARD_DETECTION_GPIO GPIOD
#define BOARD_SDCARD_CARD_DETECTION_GPIO_PORT PORTD
#define SDCARD_CARD_DETECTION_GPIO_PIN 15U
#define SDCARD_CARD_WRITE_PROTECTION_GPIO GPIOC
#define SDCARD_CARD_WRITE_PROTECTION_GPIO_PORT PORTC
#define SDCARD_CARD_WRITE_PROTECTION_GPIO_PIN 13U
#define SDCARD_SPI_HW_BASEADDR SPI1
#define SDCARD_CARD_INSERTED 0U

/*! @brief Define the port interrupt number for the board switches */
#define BOARD_SW2_GPIO GPIOA
#define BOARD_SW2_PORT PORTA
#define BOARD_SW2_GPIO_PIN 4U
#define BOARD_SW2_IRQ PORTA_IRQn
#define BOARD_SW2_IRQ_HANDLER PORTA_IRQHandler
#define BOARD_SW2_NAME "SW2"

#define BOARD_SW3_GPIO GPIOC
#define BOARD_SW3_PORT PORTC
#define BOARD_SW3_GPIO_PIN 6U
#define BOARD_SW3_IRQ PORTC_IRQn
#define BOARD_SW3_IRQ_HANDLER PORTC_IRQHandler
#define BOARD_SW3_NAME "SW3"

/* Board led color mapping */
#define LOGIC_LED_ON 0U
#define LOGIC_LED_OFF 1U

#define BOARD_LED_RED_GPIO GPIOC
#define BOARD_LED_RED_GPIO_PORT PORTC
#define BOARD_LED_RED_GPIO_PIN 8U

#define BOARD_LED_GREEN_GPIO GPIOC
#define BOARD_LED_GREEN_GPIO_PORT PORTC
#define BOARD_LED_GREEN_GPIO_PIN 9U

#define BOARD_LED_BLUE_GPIO GPIOC
#define BOARD_LED_BLUE_GPIO_PORT PORTC
#define BOARD_LED_BLUE_GPIO_PIN 10U

//---------------------------------------------------------------------
/* Board level sensor */
#define BOARD_rl1_GPIO GPIOC //Relay1 for actuate any motor as example.
#define BOARD_rl1_PORT PORTC
#define BOARD_rl1_PIN 5U

#define BOARD_rl2_GPIO GPIOC // Relay2 for actuate some auxiliary motor.
#define BOARD_rl2_PORT PORTC
#define BOARD_rl2_PIN 3U
// Led output to indicate which level is on or off.
#define BOARD_LED1_GPIO GPIOD
#define BOARD_LED1_PORT PORTD
#define BOARD_LED1_PIN 0U

#define BOARD_LED2_GPIO GPIOD
#define BOARD_LED2_PORT PORTD
#define BOARD_LED2_PIN 1U

#define BOARD_LED3_GPIO GPIOD
#define BOARD_LED3_PORT PORTD
#define BOARD_LED3_PIN 2U

#define BOARD_LED4_GPIO GPIOD
#define BOARD_LED4_PORT PORTD
#define BOARD_LED4_PIN 3U
// level input to indicate which level is actuated.
#define BOARD_LEVEL1_GPIO GPIOB
#define BOARD_LEVEL1_PORT PORTB
#define BOARD_LEVEL1_PIN 0U

#define BOARD_LEVEL2_GPIO GPIOB
#define BOARD_LEVEL2_PORT PORTB
#define BOARD_LEVEL2_PIN 1U

#define BOARD_LEVEL3_GPIO GPIOB
#define BOARD_LEVEL3_PORT PORTB
#define BOARD_LEVEL3_PIN 2U

#define BOARD_LEVEL4_GPIO GPIOB
#define BOARD_LEVEL4_PORT PORTB
#define BOARD_LEVEL4_PIN 3U
//--------------------------------------------------

/* FLEXIO */
#define BOARD_FLEXIO_I2C_INSTANCE (3)
#define BOARD_FLEXIO_I2C_INSTANCE_BASE (I2C3)
#define BOARD_FLEXIO_INSTANCE (0)
#define BOARD_FLEXIO_INSTANCE_BASE (FLEXIO0)
#define BOARD_FLEXIO_DATA_PINS (8)
#define BOARD_FLEXIO_DATA_PIN_START_INDEX (4)
#define BOARD_FLEXIO_DATA_PIN_END_INDEX (11)
#define BOARD_FLEXIO_PCLK_PIN_INDEX (0)
#define BOARD_FLEXIO_HREF_PIN_INDEX (3)
#define BOARD_FLEXIO_VSYNC_PORT_INDEX (1)
#define BOARD_FLEXIO_VSYNC_PIN_INDEX (2)
#define BOARD_FLEXIO_CLCK_OUT_PORT_INDEX (2)
#define BOARD_FLEXIO_CLCK_OUT_PIN_INDEX (3)
#define BOARD_FLEXIO_RESET_PORT_INDEX (2)
#define BOARD_FLEXIO_RESET_PIN_INDEX (8)

#define TICKLESS_LPTMR_BASE_PTR LPTMR0
#define TICKLESS_LPTMR_IRQn LPTMR0_LPTMR1_IRQn
#define vPortLptmrIsr LPTMR0_LPTMR1_IRQHandler

#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */

/*******************************************************************************
 * API
 ******************************************************************************/

void BOARD_InitDebugConsole(void);

#if defined(__cplusplus)
}
#endif /* __cplusplus */

#endif /* _BOARD_H_ */

Credits

Andre Pereira da Silva

Andre Pereira da Silva

2 projects • 2 followers
I'm from Brazil, live in Rio de Janeiro, one of the most beautiful city in Brazil, have many good persons,. I'm industrial technician.

Comments